<template>
  <j-modal
    :title="title"
    width="70%"
    :visible="visible"
    :mask-closable="false"
    cancel-text="关闭"
    @close="close"
  >
    <!-- 操作按钮区域 -->
    <div class="table-operator" style="float: right">
      <a-button ghost type="danger" @click="handleExportXls()">导出数据</a-button>
    </div>
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-form-item>
          <j-dict-select-tag
            v-model="queryParam.toolType"
            style="width: 150px"
            placeholder="选择工具名称"
            dict-code="tool_type"
          />
        </a-form-item>
        <a-form-item>
          <a-tree-select
            v-model="queryParam.deptId"
            style="width: 150px"
            :show-search="true"
            allow-clear
            placeholder="选择车间"
            :tree-data="treeData"
            tree-node-filter-prop="label"
            :replace-fields="{children:'children', title:'label', key:'id', value: 'id' }"
          />
        </a-form-item>
        <a-form-item>
          <a-input
            v-model="queryParam.storePlace"
            style="width: 150px"
            placeholder="输入存放处"
          />
        </a-form-item>
        <a-form-item>
          <a-date-picker
            v-model="queryParam.lastTestDate"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
            placeholder="上次检验日期"
          />
        </a-form-item>
        <a-form-item>
          <a-input
            v-model="queryParam.toolCode"
            style="width: 150px"
            placeholder="输入编号"
          />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="searchQuery">查询</a-button>
          <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
        </a-form-item>
      </a-form>
    </div>

    <!-- table区域-begin -->
    <div style="margin-top: 10px">
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon" /> 已选择&nbsp;<a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        bordered
        row-key="toolId"
        :columns="columns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :row-selection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a-button v-if="record.state!=='1'" size="small" type="primary" @click="handleEdit(record)">
            更换工具
          </a-button>
          <a-button v-else size="small"  type="primary" @click="handleCheck(record)">
            查看替换工具
          </a-button>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
    <check-model ref="checkForm" />
    <change-model ref="changeModel" />
  </j-modal>
</template>
<script>
import { getAction, downFile } from '@/api/request'
import JModal from '@/components/JModal'
import { listMixin } from '@/mixin/listMixin'
import columns from './indexColumns'
import CheckModel from './CheckAndEditModel'
import ChangeModel from '../checkList/CheckAndEditModel'

export default {
  name: 'BfList',
  components: {
    JModal,
    CheckModel,
    ChangeModel
  },
  mixins: [listMixin],
  data() {
    return {
      title: '报废安全工具台账',
      visible: false,
      queryParam: {},
      // 表头
      columns: columns(this),
      treeData: [],
      url: {
        list: '/business/safetool/base/safety/tool/listScrapTool',
        tree: '/system/dept/treeSelect',
        exportXlsUrl: '/business/safetool/base/safety/tool/exportScrap'
      }

    }
  },
  created() {
  },
  methods: {
    show() {
      this.visible = true
      this.loadData()
      this.loadTree()
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    handleCheck: function(record) {
      this.$refs.checkForm.edit(record)
      this.$refs.checkForm.title = '查看'
      this.$refs.checkForm.disabled = true
    },
    handleEdit: function(record) {
      this.$refs.changeModel.edit(record)
      this.$refs.changeModel.title = '更换安全工具'
      this.$refs.changeModel.disableSubmit = false
    },

    close() {
      this.visible = false
    }
  }
}
</script>
