<template>
  <div>
    <Modal v-model="showModal" v-drag width="1050" class="modal-footer-none zIndex-1100">
      <p slot="header">项目试验报告</p>
      <div>
        <Row>
          <!--查询-->
          <Col span="24">
            <Form id="search-contract-record" :label-width="70" inline onsubmit="return false">
              <label class="label-sign"></label>
              <Form-item label="项目名称:">
                <Input v-model="formObj.expName" name="expName" placeholder="请输入项目名称" style="width: 200px"
                       clearable @on-enter="_search"/>
              </Form-item>
              <Form-item class="search-btn">
                <Button type="primary" @click="_search">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :table-height="tableHeight"
                        :get-page="getPage" :icon-msg="iconMsg" show-check-box @on-result-change="_tableResultChange">
              <vxe-table-column
                v-for="item in pageColumns"
                :key="item.key"
                :field="item.key"
                :title="item.title"
                :min-width="item.width?item.width:200"
                :fixed="item.fixed?item.fixed:undefined">
                <template slot-scope="scope">
                <span v-if="item.key==='status'">
                  {{scope.row[item.key].display}}
                </span>
                  <span v-else-if="item.key==='ctime'">{{scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):''}}</span>
                  <span v-else>{{scope.row[item.key]}}</span>
                </template>
              </vxe-table-column>
            </PTVXETable>
          </Col>
        </Row>
      </div>
    </Modal>

  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import { meterEntrust, soilReport, soilStatistics } from '../../../api'
import Global from '../../../api/config'
export default {
  data() {
    return {
      showModal: false,
      getPage: {},
      searchOpen: false,
      selectIds: [],
      pageColumns: [
        { title: '项目名称', key: 'expName' },
        { title: '检测科室', key: 'groupName' },
        { title: '状态', key: 'status' },
        { title: '备注', key: 'remark' },
        { title: '创建人', key: 'uname' },
        { title: '创建时间', key: 'ctime', dateTime: true }
      ],
      btn: [],
      iconMsg: [
        {
          type: 'ios-book',
          id: '',
          name: '查看项目报告'
        }
      ],
      formObj: {
        entrustId: undefined,
        expName: undefined,
        groupId: undefined
      }
    }
  },
  computed: {
    tableHeight: function() {
      return this.$tableHeight('tableModal')
    }
  },
  methods: {
    _open(data) {
      this.showModal = true
      this.formObj.entrustId = data.id
      this.formObj.groupId = data.groupId
      this._page()
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '通过':
          this._submitExpReportIssue()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },

    _iconClick(res, data) {
      this.$nextTick(() => {
        switch (res) {
          case '查看项目报告':
            this._reportView(data)
            // this._recordView(data.originalRecordId)
            break
          case '查看试验项目':
            this._itemView(data.id)
            break
          case '删除':
            this._deleteByIds([data.id])
            break
        }
      })
    },
    _reportView(data) {
      if (data.objectKey) {
        this._reportMakeLook(data)
      } else {
        this._recordView(data.originalRecordId)
      }
    },
    _reportMakeLook: async function(data) {
      console.log(data)
      const result = await soilReport.expReportGetById(data.id)
      if (result) {
        console.log('result')
        this._viewReport(result)
      }
    },
    _viewReport(data) {
      if (data) {
        this.$openWindowModeless({
          objectKey: data.objectKey,
          idType: 13,
          id: data.id,
          isReport: 4
        })
      }
    },
    // 查看原始记录
    _recordView(originalRecordId) {
      let recordUrl = ''
      if (process.env.NODE_ENV === 'production') {
        recordUrl = 'http://record.patzn.com'
      } else {
        recordUrl = Global.recordURL
      }
      // eslint-disable-next-line no-undef
      layx.iframe(
        'labRecordWriteOriView',
        '原始记录预览',
        recordUrl + '/print/v1/form/' + originalRecordId + '?type=ENVTESTMAKE',
        {
          event: {
            onload: {
              after: function(layxWindow, winform) {
                // eslint-disable-next-line no-undef
                layx.max(winform.id)
              }
            }
          }
        }
      )
    },
    _submitExpReportIssue() {
      const ids = this.selectIds
      if (ids.length === 0) {
        this.$Message.warning('请至少选择一条数据!')
      } else {
        this._submitExpReportIssueConfirm(
          ids,
          `确认要提交 ${ids.length} 条数据吗?`
        )
      }
    },
    _submitExpReportIssueConfirm(ids, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content || `确定要处理该条数据吗?`,
        onOk: () => {
          this.__submitExpReportIssueOk(ids)
        }
      })
    },
    async __submitExpReportIssueOk(ids) {
      const result = await soilStatistics.expReportIssueSubmit(ids)
      if (result) {
        this._resultChange('提交成功!')
      }
    },
    _resultChange(msg) {
      this.$Message.success(msg)
      this._page()
      this.$emit('on-result-change')
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await meterEntrust.pageGroupExpReportEnd(this.formObj)
      console.log(result)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._hideLoading()
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'changeSize':
          this._page()
          break
        case 'selectIds':
          this.selectIds = data
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _search() {
      this._page()
    }
  }
}
</script>
