<template>
  <el-card class="content-z">
    <!-- 查询区域 -->
    <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-select v-model="queryParam.testResult" placeholder="选择实验结果" style="width:150px">
            <a-select-option value="">
              请选择
            </a-select-option>
            <a-select-option value="0">
              不合格
            </a-select-option>
            <a-select-option value="1">
              合格
            </a-select-option>
          </a-select>
        </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>
    <!-- 操作按钮区域 -->
    <div class="table-operator" style="margin: 15px 0">
      <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
      <a-button ghost type="danger" @click="handleExportXls()">导出数据（含报废）</a-button>

      <a-dropdown type="danger">
        <a-menu slot="overlay">
          <a-menu-item key="0" @click="handleExportCode()">
            导出选中
          </a-menu-item>
          <a-menu-item key="1" @click="handleExportCodeAll()">
            导出全部
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量导出二维码
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <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 size="small" type="primary" @click="perview(record)">
            二维码
          </a-button>
          <a-divider type="vertical" />
          <a-button size="small" type="primary" @click="handleEdit(record)">
            编辑
          </a-button>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.toolId)">
            <a-button size="small" type="danger">
              删除
            </a-button>
          </a-popconfirm>
        </span>
      </a-table>
      <check-and-edit-model ref="modalForm" @ok="modalFormOk" />
      <a-modal :visible="previewVisible" :footer="null" @cancel="handleCancel">
        <img alt="example" style="width: 100%" :src="previewImage">
      </a-modal>
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
  </el-card>
</template>
<script>
import { listMixin } from '@/mixin/listMixin'
import columns from './indexColumns'
import CheckAndEditModel from './CheckAndEditModel'
import { getAction, downFile } from '@/api/request'

export default {
  components: {
    CheckAndEditModel
  },
  mixins: [listMixin],
  data() {
    return {
      // 查询条件
      previewVisible: false,
      previewImage: '',
      queryParam: {},
      // 表头
      columns: columns(this),
      treeData: [],
      url: {
        list: '/business/safetool/base/safety/tool/list',
        delete: '/business/safetool/base/safety/tool/',
        exportXlsUrl: '/business/safetool/base/safety/tool/export',
        qrcode: '/st/export',
        qrcodeAll: '/st/exportAll',
        tree: '/system/dept/treeSelect',
        importExcelUrl: `${process.env.VUE_APP_BASE_API}/importExcelUrl`
      }
    }
  },
  created() {
    this.loadTree()
  },
  methods: {
    handleCancel() {
      this.previewImage = ''
      this.previewVisible = false
    },
    perview(record) {
      this.previewImage = record.qcodeUrl
      this.previewVisible = true
    },
    loadTree() {
      this.treeData = []
      getAction(this.url.tree).then((res) => {
        if (res.code === 200) {
          this.treeData = res.data
        }
      })
    },
    handleExportCode() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      }
      const param = this.getQueryParams()
      if (this.selectedRowKeys && this.selectedRowKeys.length > 0) {
        param['id'] = this.selectedRowKeys.join(',')
      }
      downFile(this.url.qrcode, param).then((data) => {
        if (data.code !== 200) {
          this.$message.warning('文件下载失败')
        } else {
          window.location.href = data.url
        }
      })
    },
    handleExportCodeAll() {
      downFile(this.url.qrcodeAll).then((data) => {
        if (data.code !== 200) {
          this.$message.warning('文件下载失败')
        } else {
          window.location.href = data.url
        }
      })
    }
  }
}
</script>
<style scoped>
  @import '~@/assets/less/common.less'
</style>
