<template>
  <div>
    <Row>
      <!--查询-->
      <Col span="24" />
      <Form
        v-show="searchOpen"
        inline
        onsubmit="return false"
        :label-width="70"
        class="search-form"
      >
        <label class="label-sign" />
        <Form-item class="width-48" label="审核目的:">
          <Input v-model="formObj.checkPlanPurpose" placeholder="请输入审核目的" @on-enter="_search" /></Input>
        </Form-item>
        <Form-item class="search-btn">
          <Button type="primary" @click="_formSearch">
            搜索
          </Button>
        </Form-item>
      </Form>
      </Col>
      <!--按钮-->
      <Col span="24" />
      <btn-list :msg="btn" :open="searchOpen" show-search-btn="true" @on-result-change="_btnClick" />
      </Col>
      <!--table-->
      <Col span="24" />
      <element-table
        ref="pageTable"
        :page-columns="pageColumns"
        :table-height="tableHeight"
        :get-page="getPage"
        :opt-col-width="80"
        :click="true"
        @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"
          :width="item.width"
          :min-width="200"
          :fixed="item.fixed?item.fixed:undefined"
        >
          <template slot-scope="scope">
            <div v-if="item.dateTime">
              {{ scope.row[item.key]?$dateformat(scope.row[item.key],'yyyy-mm-dd HH:MM:ss'):'' }}
            </div>
            <div v-if="item.key==='status'">
              <div v-if="scope.row.status===0" class="green-color">
                草稿
              </div>
              <div v-else-if="scope.row.status===10" class="green-color">
                审批中
              </div>
              <div v-else-if="scope.row.status===12" class="green-color">
                审批驳回
              </div>
              <div v-else-if="scope.row.status===1" class="green-color">
                待实施
              </div>
            </div>
            <span v-else-if="item.type" class="blue-color">
              {{ scope.row[item.key] === undefined ? '': scope.row[item.key].display }}
            </span>
            <div v-else>
              {{ scope.row[item.key] }}
            </div>
          </template>
        </el-table-column>
      </element-table>
      </Col>
    </Row>
  </div>
</template>
<script>
import { internalAudit } from '../../../api'
export default {
  components: {},
  data() {
    return {
      formObj: {
        checkPlanPurpose: ''
      },
      selectData: [],
      searchOpen: true,
      btn: [
        // { type: 'success', id: '', name: '自动分配' }
      ],
      pageColumns: [
        { title: '状态', key: 'status', width: 160, status: true },
        {
          title: '审核目的',
          key: 'checkPlanPurpose',
          width: 160,
          detail: true
        },
        { title: '审核时间', key: 'checkPlanTime', width: 160, date: true },
        { title: '审核依据', key: 'checkPlanBasis', width: 160 },
        { title: '审核方法', key: 'checkPlanMethod', width: 160 },
        { title: '审核类型', key: 'checkPlanType', width: 160 },
        { title: '审核范围', key: 'checkPlanRange', width: 160 },
        { title: '审核组长', key: 'checkGroupLeaderName', width: 160 },
        { title: '审核组成员', key: 'checkGroupMemberNames', width: 160 },
        { title: '编制人', key: 'applyName', width: 160 },
        { title: '编制日期', key: 'applyTime', width: 160, date: true },
        { title: '批准人', key: 'auditName', width: 160 },
        { title: '批准日期', key: 'auditTime', width: 160, date: true },
        { title: '备注', key: 'remark', width: 160 }
      ],
      getPage: {},
      iconMsg: [
        // { type: 'pt-a-edit', id: '', name: '编辑' },
        // { type: 'pt-a-trash', id: '', name: '删除' }
      ],
      iconMsgDis: [
        { type: 'pt-a-edit', id: '', name: '编辑' },
        { type: 'pt-a-trash', id: '', name: '删除', disabled: true }
      ]
    }
  },
  computed: {
    tableHeight: function() {
      if (this.searchOpen) {
        return this.$tableHeight('search') - 60
      } else {
        return this.$tableHeight('noSearch') - 60
      }
    }
  },
  mounted() {
    this._page()
  },
  methods: {
    _formSearch() {
      this.$refs.pageTable._pageChange(1)
    },
    _page: async function() {
      Object.assign(this.formObj, this.$refs.pageTable._searchParams())
      const result = await internalAudit.page(this.$serializeForm(this.formObj))
      if (result) {
        this.getPage = result
        this.$refs.pageTable._initTable()
      }
    },
    _searchParams(formId) {
      return this.$refs.pageTable._searchParams(formId)
    },
    _btnClick(msg, data) {
      switch (msg) {
        case '添加':
          this.$refs.editModal._open()
          break
        case 'search':
          this.searchOpen = !this.searchOpen
          break
      }
    },
    _tableResultChange(msg, data) {
      switch (msg) {
        case 'page':
          this._page()
          break
        case 'selectIds':
          this.$emit('on-result-change', data)
          break
        case 'click':
          this.$emit('on-result-change', data.id)
          break
      }
    },
    _iconClick(res, data) {
      switch (res) {
        case '编辑':
          this._editModal(data.id)
          break
        case '删除':
          this._deleteById(data.id)
          break
      }
    },
    _open() {
      this._page()
    },
    // 删除
    _deleteById(id) {
      this.$Modal.confirm({
        title: '提示',
        content: '确定删除该信息？',
        onOk: () => {
          this._delOk(id)
        }
      })
    },
    _delOk: async function(id) {
      const result = await internalAudit.deleteById(id)
      if (result) {
        this.$Message.success('删除成功')
        this._page()
      }
    },
    _editModal: async function(id) {
      const result = await internalAudit.getById(id)
      if (result) {
        this.$refs.editModal._open(result)
      }
    }
  }
}
</script>
