<template>
  <div>
    <!--1.技术主管审批-->
    <div>
      <!-- 执行顺序 -->
      <Alert class="equip_alert">
        <div>
          <table class="tableClass table1Class">
            <tbody>
              <tr>
                <th>计划名称</th>
                <td>{{ getObj.name }}</td>
                <th>申请人</th>
                <td>{{ getObj.applyName }}</td>
              </tr>
              <tr>
                <th>申请原因</th>
                <td>{{ getObj.applyReason }}</td>
                <th>申请时间</th>
                <td>{{ this.$dateformat(getObj.applyTime, 'yyyy-mm-dd HH:MM') }}</td>
              </tr>
              <tr>
                <th>备注</th>
                <td colspan="3">
                  {{ getObj.remark }}
                </td>
              </tr>
            </tbody>
          </table>
        </div>
        <Form inline>
          <Form-item label="技术主管意见" prop="firstStepAdvice" style="width: 100%;">
            <Input name="firstStepAdvice" :rows="2" />
          </Form-item>
        </Form>
      </Alert>
      <!--操作-->
      <div>
        <Button type="success" @click="_addMaintain" style="margin-bottom: 10px">
          添加
        </Button>
      </div>
    </div>
    <!--内容-->

    <div style="margin-bottom: 10px">
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :get-page="getPage"
        :table-height="360"
        :icon-msg="iconMsg"
        :opt-col-width="80"
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          :prop="item.key"
          :label="item.title"
          :min-width="item.width"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.date">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
    </div>
    <Form id="edit-form" :label-width="90" inline>
      <Form-item label="附件报告单" style="width: 98.6%;">
        <div v-for="item in lmsEquipFiles" :key="item.id" class="file-upload-list">
          <div>
            <div class="file-upload-list-cover">
              <Icon type="ios-cloud-download-outline" @click.native="_downloadFile(item)" />
            </div>
          </div>
          {{ item.orginName }}
        </div>
      </Form-item>
    </Form>
    <!--操作-->
    <div slot="footer" align="center" style="margin-top: 50px">
      <Button type="success" @click="_approve('success','确定通过？')">
        同意
      </Button>
      <Button type="error" @click="_approve(3,'确定驳回？')">
        驳回
      </Button>
      <Button type="error" @click="_approve(5,'确定终止？')">
        终止
      </Button>
    </div>
    <!--添加-->
    <equipMaintainEdit ref="addModal" @on-result-change="_page" />
  </div>
</template>
<script>
import { equipMaintain, equipMaintainDetail, equipFile } from '../../../../api'
import global from '../../../../api/config'
import equipMaintainEdit from './equipMaintainEdit'

export default {
  components: {
    equipMaintainEdit
  },
  data() {
    return {
      id: '',
      pageColumns: [
        {
          title: '仪器名称',
          key: 'name',
          width: 160,
          fixed: true
        },
        { title: '仪器编号', key: 'equipNum', width: 160 },
        { title: '规格型号', key: 'spec', width: 160 },
        { title: '实验室编号', key: 'labNum', width: 160 },
        { title: '使用科别', key: 'departmentName', width: 160 },
        {
          title: '购买日期',
          key: 'buyDate',
          width: 160,
          date: true
        },
        {
          title: '有效日期',
          key: 'effectDate',
          width: 160,
          date: true
        },
        {
          title: '维保项目名称',
          key: 'projectName',
          width: 160
        },
        {
          title: '维保内容',
          key: 'maintainContent',
          width: 160
        },
        { title: '备注', key: 'remark', width: 160 }
      ],
      iconMsg: [{ type: 'pt-trash', id: '', name: '删除' }],
      getPage: {},
      lmsEquipFiles: [],
      getObj: {}
    }
  },

  mounted() {
    this.id = this.$route.query.id
    this._page()
    this._maintain()
    this._getEquipFileList()
  },
  methods: {
    _page: async function() {
      const obj = Object.assign(
        { maintainId: this.id },
        this.$refs.pageTable._searchParams()
      )
      const result = await equipMaintainDetail.page(obj)
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'iconClick':
          this._iconClick(data.name, data.rowData)
          break
      }
    },
    _maintain: async function() {
      this.getObj = await equipMaintain.get(this.id)
    },
    _approve(allow, content) {
      this.$Modal.confirm({
        title: '提示',
        content: content,
        onOk: () => {
          const data = {}
          data.id = $('#flowFrame', parent.document).attr('data-tid')
          data.status = allow
          data.reason = $('input[name="firstStepAdvice"]').val()
          this._approveOk(data)
        }
      })
    },
    _approveOk: async function(data) {
      const result = await equipMaintain.approve(data)
      if (result) {
        this.$Message.success('审批成功！')
        setTimeout(() => {
          parent.location.replace(global.webURL + '/sys/workflow/flow_task')
        }, 200)
      }
    },
    _addMaintain() {
      this.$refs.addModal._open(this.id) // 打开添加操作
    },
    _iconClick(res, data) {
      switch (res) {
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await equipMaintainDetail.deleteByIds(id)
      if (result) {
        this._page()
        this.$Message.success('删除成功！')
      }
    },
    _getEquipFileList: async function() {
      this.lmsEquipFiles = await equipFile.getFileList({ relId: this.id })
    },
    _downloadFile(file) {
      // 下载附件
      this.$Modal.confirm({
        title: '提示',
        content: '确定要下载？',
        onOk: () => {
          const url =
            global.baseURL + '/res/v1/equip_maintain/downLoadFile?id=' + file.id
          window.open(url)
        }
      })
    }
  }
}
</script>
