<template>
  <div>
    <!--内容-->
    <div class="layout-content-padding">
      <div class="layout-content-main">
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-form-make-contract" :label-width="90" v-show="searchOpen" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item class="search-item" label="委托单位:">
                <Input @on-enter="_formSearch" v-model="formObj.client" name="client" placeholder="请输入委托单位" clearable/>
              </Form-item>
              <Form-item class="search-item" label="委托编号:">
                <Input @on-enter="_formSearch" v-model="formObj.entrustCode" name="entrustCode" placeholder="请输入委托编号" clearable/>
              </Form-item>
              <Form-item class="search-btn">
                <Button @click="_formSearch" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :open="searchOpen" @on-result-change="_btnClick" :showSearchBtn="true"
                     :msg="btn" class="contHide">
<!--              <template slot="processTask">-->
<!--                <div class="fr process-task">-->
<!--                  <el-checkbox v-model="personal" @change="_page" :true-label="1" :false-label="0" size="medium">-->
<!--                    个人任务-->
<!--                  </el-checkbox>-->
<!--                  <el-checkbox v-model="onlyReportMake" @change="_page" :true-label="1"-->
<!--                               :false-label="0" size="medium">只显示待编报告-->
<!--                  </el-checkbox>-->
<!--                </div>-->
<!--              </template>-->
            </btn-list>
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable
              ref="pageTable"
              :tableHeight="tableHeight"
              :getPage="getPage"
              :iconMsg="iconMsg"
              @on-result-change="_tableResultChange"  select-data>
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :min-width="100"
                :width="item.width"
                :fixed="item.fixed?item.fixed:undefined"
                sortable
              >
                <template slot-scope="scope">
                  <a @click.stop="_detailModal(scope.row.id,scope.row.type)"
                     v-if="item.detail">{{scope.row[item.key]}}</a>
                  <div v-else-if="item.type">{{scope.row[item.key]?'政府':'企业'}}</div>
                  <div v-else-if="item.date">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'): ''}}
                  </div>
                  <div v-else-if="item.key==='havePhoto'">{{scope.row[item.key]===1?'是':
                    scope.row[item.key]===0?'否':''}}
                  </div>
                  <div v-else-if="item.status">
                    <span v-if="scope.row[item.key].display === '报告审核退回'||'报告签发退回' ===scope.row[item.key].display " style="color: red;">{{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}</span>
                    <span v-else>{{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}</span>
                  </div>
                  <div v-else-if="item.judge">
                    {{Number(scope.row[item.key]) === 0? '否':'是'}}
                  </div>
                  <div v-else>{{scope.row[item.key]}}</div>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </div>
    <keep-alive>
      <!-- eslint-disable-next-line vue/require-component-is -->
      <component ref="refModal" :is="currentComponent" @on-result-change="_componentResult"></component>
    </keep-alive>
  </div>
</template>
<script>
import { soilSample, soilStatistics } from '../../../../api'
import SampleByMakeModal from '../SampleByMakeModal'
import Operation from '../../../../components/operation/Operation'
import OriginalRecordView from '../OriginalRecordView'
import UploadByReport from '../UploadByReport'
import ItemReportView from '../ItemReportView'
export default {
  components: {
    // eslint-disable-next-line vue/no-unused-components
    SampleByMakeModal,
    // eslint-disable-next-line vue/no-unused-components
    Operation,
    // eslint-disable-next-line vue/no-unused-components
    OriginalRecordView,
    // eslint-disable-next-line vue/no-unused-components
    UploadByReport,
    // eslint-disable-next-line vue/no-unused-components
    ItemReportView
  },
  data() {
    return {
      typeList: [{ name: '企业', value: 0 }, { name: '政府', value: 1 }],
      currentComponent: '',
      getPage: {},
      formObj: {},
      pageColumns: [
        {
          title: '委托商',
          key: 'client',
          width: 100,
          fixed: 'left'
        },
        { title: '委托编号', key: 'entrustCode', width: 100 },
        { title: '报告编号', key: 'reportCode', width: 100 },
        { title: '进度', key: 'progress', width: 110, status: true },
        { title: '状态', key: 'status', width: 100, status: true },
        { title: '委托日期', key: 'entrustDate', width: 100, date: true },
        { title: '钻孔位置', key: 'boreholeLocation', width: 100 },
        { title: '钻孔名称', key: 'boreholeName', width: 100 },
        { title: '水深', key: 'waterDepth', width: 100 },
        { title: '制表人', key: 'tabulater', width: 110 },
        { title: '制表日期', key: 'tabulateDate', width: 110, date: true }
      ],
      iconMsg: [
        {
          type: 'ios-flask-outline',
          id: '',
          name: '管理样品和报告',
          currentComponent: SampleByMakeModal
        },
        {
          type: 'ios-beaker',
          id: '',
          name: '查看原始记录',
          currentComponent: OriginalRecordView
        },
        {
          type: 'ios-list-box',
          id: '',
          name: '查看试验项目报告'
        },
        {
          type: 'ios-cloud-upload',
          id: '',
          name: '上传报告'
        },
        {
          type: 'md-image',
          id: '',
          name: '预览报告'
        },
        {
          type: 'ios-book',
          id: '',
          name: '编辑报告'
        },
        // {
        //   type: 'ios-document-outline',
        //   id: '',
        //   name: '检测项目'
        // },
        { type: 'ios-cloud-outline', id: '', name: '附件' },
        {
          type: 'ios-clock-outline',
          id: '',
          name: '操作记录',
          componentName: 'FoodContractRecord'
        }
      ],
      onlyReportMake: 1,
      searchOpen: false,
      btn: [
        {
          type: 'primary',
          id: '',
          name: '提交'
        }
      ],
      selectIds: [],
      selectData: [],
      personal: 0
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('tabSearchTwo')
      } else {
        return this.$tableHeight('tabNoSearch')
      }
    }
  },
  mounted() {
    // this._dicSearch()
  },
  methods: {
    _open() {
      // this._dicSearch()
    },
    _reportDateChange(data) {
      $('input[name="reportDueDateBegin"]').val(data[0])
      $('input[name="reportDueDateEnd"]').val(data[1])
    },
    // 从字典查预警期
    _dicSearch() {
      // 食品报告编制个人任务是否勾选
      const data = ['食品报告编制个人任务是否勾选']
      this.$store.dispatch('LmsBaseDict/listDict', data).then(() => {
        const result = this.$store.state.LmsBaseDict.list
        const personalList = result[0]
        // 食品报告编制个人任务是否勾选
        if (personalList.length && personalList[0].name === '是') {
          this.personal = 1
        } else {
          this.personal = 0
        }
        this._page()
      })
    },
    _btnClick(msg) {
      switch (msg) {
        case '信息维护':
          this._maintainInfo()
          break
        case '提交':
          this._submitToCheck()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _submitToCheck() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据')
      } else {
        console.log(this.selectData)
        for (let i = 0; i < this.selectIds.length; i++) {
          if (this.selectData[i].reported !== '是') {
            this.$Message.warning('报告未编制')
          } else {
            this.$Modal.confirm({
              title: '提示',
              content: '确定提交这' + this.selectIds.length + '条数据？',
              onOk: () => {
                this._submitReportOk()
              }
            })
          }
        }
      }
    },
    _submitReportOk: async function() {
      const result = await soilSample.reportMakeSubmit(this.selectIds.join(','))
      if (result) {
        this.$Message.success('提交成功')
        this._page()
      }
    },
    // 信息维护
    _maintainInfo() {
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条数据！')
      } else {
        this.currentComponent = 'MaintainInfoModal'
        this.$nextTick(() => {
          this.$refs.refModal._open(this.selectIds, 1)
        })
      }
    },
    _componentResult(data, msg) {
      switch (this.currentComponent) {
        default:
          this._page()
      }
    },
    _iconClick(res, data, componentName) {
      this.currentComponent = componentName
      this.$nextTick(function() {
        switch (res) {
          case '管理样品和报告':
            this._sampleManage(data.id)
            break
          case '查看原始记录':
            this._viewRecord(data.id)
            break
          case '查看试验项目报告':
            this._viewItemRecord(data.id)
            break
          case '上传报告':
            this._uploadReport(data.id)
            break
          case '预览报告':
            this._viewReport(data.id)
            break
          case '编辑报告':
            this._viewReport(data.id)
            break
          case '检测项目':
            this.$refs.refModal._open(data)
            break
          case '操作记录':
            this._operationRecord(data.id)
            break
          case '附件':
            this._upload(data.id)
            break
        }
      })
    },
    _viewRecord(id) {
      this.currentComponent = 'OriginalRecordView'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _viewItemRecord(id) {
      this.currentComponent = 'ItemReportView'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _sampleManage(id) {
      this.currentComponent = 'SampleByMakeModal'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
    },
    _uploadReport(id) {
      this.currentComponent = 'UploadByReport'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
      // this.$refs.uploadReport._open(id)
    },
    _viewReport: async function(id) {
      const result = await soilSample.getReport(id)
      if (result) {
        this._reportEdit(result, id)
      }
    },
    _reportEdit(data, id) {
      this.$openWindowModeless({
        objectKey: data.objectKey,
        id: id,
        idType: 11,
        isReport: 4
      })
    },
    _operationRecord(id) {
      this.currentComponent = 'Operation'
      this.$nextTick(() => {
        this.$refs.refModal._open(id)
      })
      // 操作日志
      // this.$refs.operation._open(id)
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this.getPage = this.$store.state.FoodContract.page
          this.selectIds = []
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'selectData':
          this.selectData = data
          this.selectIds = []
          for (let i = 0; i < this.selectData.length; i++) {
            this.selectIds.push(this.selectData[i].id)
          }
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData, data.currentComponent)
          break
        case 'changeSize':
          this._page()
          break
      }
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await soilStatistics.pageReportMake(
        this.$serializeForm(this.formObj)
      )
      if (result) {
        this.$refs.pageTable._hideLoading()
        this.getPage = result
      }
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _detailModal(id, type) {
      // 查看
      this.$store.dispatch('FoodContract/getById', id).then(() => {
        if (type === 0) {
          // 企业委托 ’0‘
          this.currentComponent = 'FoodContractCompanyDetail'
        } else {
          // 政府委托 ’1‘
          this.currentComponent = 'FoodContractGovernDetail'
        }
        this.$nextTick(function() {
          this.$refs.refModal._open(this.$store.state.FoodContract.model)
        })
      })
    },
    _upload(id) {
      this.currentComponent = 'FileManage'
      this.$nextTick(() => {
        this.$refs.refModal._open(id, 'entrustId')
      })
      // 上传文件
      // this.$refs.FileManage._open(id, 'entrustId')
    }
  }
}
</script>
