<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.maker }}</td>
                <th>编制日期</th>
                <td>{{ this.$dateformat(getObj.makeDate, "yyyy-mm-dd") }}</td>
              </tr>
              <tr>
                <th>批准人</th>
                <td>{{ getObj.approver }}</td>
                <th>批准日期</th>
                <td>{{ this.$dateformat(getObj.approveDate, "yyyy-mm-dd") }}</td>
                <th>备注</th>
                <td>{{ getObj.remark }}</td>
              </tr>
            </tbody>
          </table>
        </div>
        <Form inline>
          <Form-item label="项目负责人审批意见" prop="reason" style="width: 98.6%;">
            <Input name="reason" type="textarea" :rows="2" />
          </Form-item>
        </Form>
      </Alert>
    </div>
    <!-- 表格 -->
    <el-col :span="24">
      <element-table
        ref="pageTable"
        :table-height="300"
        :get-page="getPage"
        :icon-msg="iconMsg"
        opt-col-width="100"
        @on-result-change="_tableResultChange"
      >
        <el-table-column
          v-for="item in pageColumns"
          :key="item.key"
          show-overflow-tooltip
          sortable
          :prop="item.key"
          :label="item.title"
          :min-width="item.width"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <a v-if="item.detail" @click="_detailModal(scope.row.id)">
              {{ scope.row[item.key] }}
            </a>
            <div v-else-if="item.date">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd'):'' }}
            </div>
            <div v-else-if="item.display">
              {{ scope.row[item.key]?scope.row[item.key].display:'' }}
            </div>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
    </el-col>
    <!--操作-->
    <div slot="footer" align="center">
      <btn-list :msg="btn" @on-result-change="_btnClick" />
    </div>

    <abilityPlanDetail ref="detailModal" />

    <qcDownLoad ref="fileModal" />
  </div>
</template>
<script>
import { abilityMainPlan, abilitySubPlan } from '../../../api'
import global from '../../../api/config'
import qcDownLoad from './check-files/qcDownLoad'
import abilityPlanDetail from './abilityPlanDetail'

export default {
  components: {
    abilityPlanDetail,
    qcDownLoad
  },
  data() {
    return {
      id: {},
      getObj: {},
      getPage: {},
      pageColumns: [
        { title: '状态', key: 'status', width: 130, display: true },
        { title: '子领域', key: 'subfield', width: 130 },
        { title: '项目名称', key: 'name', width: 140, detail: true },
        { title: '检测方法', key: 'method', width: 140 },
        { title: '频次', key: 'frequency', width: 140 },
        { title: '计划开始日期', key: 'beginDate', width: 130, date: true },
        { title: '检测室', key: 'testRoom', width: 130 },
        { title: '备注', key: 'remark', width: 130 }
      ],
      iconMsg: [{ type: 'pt-cloud', id: '', name: '附件' }],
      btn: [
        { type: 'danger', id: '', name: '驳回' },
        { type: 'success', id: '', name: '通过' }
      ]
    }
  },
  mounted() {
    this.id = this.$route.query.id
    this._getData()
    this._formSearch()
  },
  methods: {
    _getData: async function() {
      this.getObj = await abilityMainPlan.getById(this.id)
    },
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      const formObj = Object.assign(
        { mainPlanId: this.id },
        this.$refs.pageTable._searchParams()
      )
      const result = await abilitySubPlan.pageCheck(
        this.$serializeForm(formObj)
      )
      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
      }
    },
    _btnClick(msg) {
      switch (msg) {
        case '驳回':
          this._approve(false)
          break
        case '通过':
          this._approve(true)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '附件':
          this._checkFile(data.id)
          break
      }
    },
    _checkFile(id) {
      this.$refs.fileModal._open(id)
    },
    _detailModal(id) {
      this.$refs.detailModal._open(id)
    },
    _approve(allow) {
      this.$Modal.confirm({
        title: '提示',
        content: allow ? '确定审批通过监测方案？' : '确定驳回该项目？',
        onOk: () => {
          const data = {}
          data.id = $('#flowFrame', parent.document).attr('data-tid')
          data.status = allow ? 'pass' : 'back'
          data.reason = $('textarea[name="reason"]').val()
          this._approveOk(data)
        }
      })
    },
    _approveOk: async function(data) {
      const result = await abilityMainPlan.approve(data)
      if (result) {
        this.$Message.success('审批成功！')
        setTimeout(() => {
          parent.location.replace(global.webURL + '/sys/workflow/flow_task')
        }, 200)
      }
    }
  }
}
</script>
