<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" @on-enter="_search" name="expName" placeholder="请输入项目名称"
                       style="width: 200px" clearable/>
              </Form-item>
              <Form-item class="search-btn">
                <Button @click="_search" type="primary">搜索</Button>
              </Form-item>
            </Form>
          </Col>
          <Col span="24">
            <btn-list :msg="btn" :open="searchOpen" @on-result-change="_btnClick" show-search-btn="true" />
          </Col>
          <!-- 表格 -->
          <Col span="24">
            <PTVXETable ref="pageTable" :table-height="tableHeight"
                        :get-page="getPage" :icon-msg="iconMsg" @on-result-change="_tableResultChange" show-check-box>
              <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>
    <Reason ref="reasonModal" @on-result-change="_reasonResult"></Reason>
  </div>
</template>
<script>
/**
 * 操作日志-委托
 */
import { meterEntrust, soilReport, soilStatistics } from '../../../api'
import Global from '../../../api/config'
import Reason from '../../../components/base/Reason'
export default {
  components: { Reason },
  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: [
        { id: 'issue-pass', type: 'primary', name: '通过' },
        { id: '', type: 'primary', name: '驳回' }
      ],
      iconMsg: [
        {
          type: 'ios-book',
          id: 'issue-view',
          name: '查看项目报告'
        },
        {
          type: 'md-albums',
          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 '驳回':
          this._goBack()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _goBack() {
      // 退回
      if (this.selectIds.length === 0) {
        this.$Message.warning('请至少选择一条委托！')
      } else {
        this.$refs.reasonModal._open('退回原因')
      }
    },
    _reasonResult(data) {
      this.$refs.pageTable._showLoading()
      if (undefined !== data && data !== '') {
        this._reportCheckBack(data)
      }
    },
    _reportCheckBack: async function(data) {
      const result = await soilReport.backExpReportIssueBack({
        ids: this.selectIds.join(','),
        remark: data
      })

      if (result) {
        this.$Message.success('退回成功！')
        await this._page()
        this.$emit('on-result-change')
        this.$refs.pageTable._hideLoading()
      }
    },

    _iconClick(res, data) {
      this.$nextTick(() => {
        switch (res) {
          case '查看项目报告':
            this._reportView(data)
            // this._recordView(data.originalRecordId)
            break
          case '查看试验项目':
            this._itemView(data.id)
            break
          case '查看附录':
            this._viewAppendix(data)
            break
          case '删除':
            this._deleteByIds([data.id])
            break
        }
      })
    },
    _viewAppendix(data) {
      if (data.appendixKey) {
        this.$openWindowModeless({
          objectKey: data.appendixKey,
          idType: 13,
          id: data.id,
          isReport: 5
        })
      } else {
        this.$Message.warning('未生成附录')
      }
    },
    _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.pageGroupExpReportIssue(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>
