<template>
  <div>
    <a-card :bordered="false" style="margin-bottom: 10px;">
      <!-- 条件搜索 -->
      <div class="table-page-search-wrapper">
        <a-form :labelCol="labelCol" :wrapperCol="wrapperCol" ref="queryForm">
          <a-row :gutter="32">
            <a-col :span="6" >
              <a-form-item label="关键词">
                <a-input v-model="queryParam.keywords" placeholder="请输入关键词" allow-clear @keyup.enter.native="handleQuery"/>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="特货代码">
                   <a-select placeholder="特货代码" v-model="queryParam.specialCode" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in thdmOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6">
              <a-form-item label="数据来源">
                 <a-select placeholder="数据来源" v-model="queryParam.source" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in sourceOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col>
              <span class="table-page-search-submitButtons" style="float: right;">
                <a-button type="primary" @click="handleQuery"><a-icon type="search" />查询</a-button>
                <a-button style="margin-left: 8px" @click="resetQuery"><a-icon type="redo" />重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <cargo-detail-add-form
        v-if="showAddModal"
        ref="cargoDetailAddForm"
        :typeOptions="typeOptions"
        :thdmOptions="thdmOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <cargo-detail-edit-form
        v-if="showEditModal"
        ref="cargoDetailEditForm"
        :typeOptions="typeOptions"
        :thdmOptions="thdmOptions"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showEditModal = false"
      />
      <advance-table
        title="含隐含性危险品货物清单"
        :pagination="{
          current: queryParam.pageNum,
          pageSize: queryParam.pageSize,
          total: total,
          showSizeChanger: true,
          showLessItems: true,
          showQuickJumper: true,
          showTotal: (total, range) => `第 ${range[0]}-${range[1]} 条，总计 ${total} 条`,
          onChange: changeSize,
          onShowSizeChange: onShowSizeChange
        }"
        tableKey="base-cargoDetail-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="cargoDetailList"
        :loading="loading"
        :format-conditions="true"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <div class="table-operations" slot="button">
          <a-button type="primary" @click="handleAdd">
            <a-icon type="plus" />新增
          </a-button>
          <!-- <a-button type="primary" :disabled="multiple" @click="handleCheck">
            <a-icon type="check" />审核
          </a-button> -->
          <a-dropdown>
            <a-menu slot="overlay" :disabled="multiple" @click="handleCheck">
              <a-menu-item :key=1> <a-icon type="check" />通过</a-menu-item>
              <a-menu-item :key=2> <a-icon type="close" />不通过</a-menu-item>
            </a-menu>
            <a-button type="primary">
              <a-icon type="check" />审核
            </a-button>
          </a-dropdown>
          <a-button type="" @click="$refs.importExcel.importExcelHandleOpen()">
            <a-icon type="import" />导入
          </a-button>
          <a-button type="" @click="handleExport">
            <a-icon type="download" />导出
          </a-button>
          <a-button type="danger" v-if="!multiple" :disabled="multiple" @click="handleDelete">
            <a-icon type="delete" />删除
          </a-button>
        </div>
        <span slot="type"  slot-scope="{record}">
          {{ typeFormat(record) }}
        </span>
        <span slot="status"  slot-scope="{record}">
          {{ statusFormat(record) }}
        </span>
        <span slot="source"  slot-scope="{record}">
          {{ sourceFormat(record) }}
        </span>
        <span slot="operation"  slot-scope="{text, record}">
          <a @click="handleUpdate(record)">
            修改
          </a>
          <a-divider type="vertical"/>
          <a @click="handleDelete(record)">
            删除
          </a>
        </span>
      </advance-table>
    </a-card>
    <!-- 上传文件 -->
    <import-excel
      title="导入含隐含性危险品货物清单"
      upload-action="/system/cargoDetail/3/importData"
      ref="importExcel"
      @ok="getList"
      @importTemplate="importTemplate"
    />
  </div>
</template>
<script>
import { listCargoDetail, importTemplate, delCargoDetail, exportCargoDetail, apply } from '@/api/system/cargoDetail'
import { listAll } from '@/api/system/specialCode'
import ImportExcel from '@/components/pt/import/ImportExcel'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import CargoDetailAddForm from '@/views/system/dangerouscargodetail/modules/CargoDetailAddForm'
import CargoDetailEditForm from '@/views/system/dangerouscargodetail/modules/CargoDetailEditForm'
export default {
  name: 'CargoDetail',
  components: {
    AdvanceTable,
    CargoDetailAddForm,
    CargoDetailEditForm,
    ImportExcel
  },
  data () {
    return {
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 货品清单表格数据
      cargoDetailList: [],
      // 1 X光机难以辨识 2 疑似风险 3 含隐含性危险品字典
      typeOptions: [],
      // 状态 0 未审核 1 审核通过 2 审核拒绝字典
      statusOptions: [],
      thdmOptions: [],
      sourceOptions: [
        {
          dictLabel: '人工维护',
          dictValue: '1'
        },
        {
          dictLabel: '优化建议',
          dictValue: '2'
        }
      ],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        type: 3,
        keywords: undefined,
        specialCode: undefined,
        source: undefined
      },
      columns: [
               {
          title: '序号',
          dataIndex: 'id',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '货品关键词',
          dataIndex: 'keywords',
          ellipsis: true,
          align: 'left',
          width: '20%'
        },
        {
          title: '特货代码',
          dataIndex: 'specialCode',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '数据来源',
          dataIndex: 'source',
          scopedSlots: { customRender: 'source' },
          align: 'left',
          width: '10%'
        },
        {
          title: '备注',
          dataIndex: 'remark',
          align: 'left',
          width: '30%'
        },
        {
          title: '审核状态',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' },
          align: 'center',
          width: '10%'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          width: '155px'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '20%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
    this.getDicts('cargo_list_type').then(response => {
      this.typeOptions = response.data
    })
    this.getDicts('cargo_detail_status').then(response => {
      this.statusOptions = response.data
    })
    listAll().then(response => {
      const data = response.data
      const thdm = []
      if (data) {
        for (const x in data) {
          thdm.push({
            dictLabel: data[x],
            dictValue: data[x]
          })
        }
      }
      this.thdmOptions = thdm
    })
  },
  methods: {
    importTemplate (expanded, record) {
      importTemplate().then(response => {
        this.download(response.msg)
      })
    },
    /** 查询货品清单列表 */
    getList () {
      this.loading = true
      this.queryParam.type = 3
      listCargoDetail(this.queryParam).then(response => {
        this.cargoDetailList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    // 1 X光机难以辨识 2 疑似风险 3 含隐含性危险品字典翻译
    typeFormat (row) {
        if (row.type || row.type === 0) {
            return this.selectDictLabel(this.typeOptions, row.type + '')
        } else {
            return ''
        }
    },
    // 状态 0 未审核 1 审核通过 2 审核拒绝字典翻译
    statusFormat (row) {
        if (row.status || row.status === 0) {
            return this.selectDictLabel(this.statusOptions, row.status + '')
        } else {
            return ''
        }
    },
    sourceFormat (row) {
        if (row.source || row.source === 0) {
            return this.selectDictLabel(this.sourceOptions, row.source + '')
        } else {
            return ''
        }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        type: undefined,
        keywords: undefined,
        specialCode: undefined,
        source: undefined
      }
      this.handleQuery()
    },
    /** 翻页操作 */
    onShowSizeChange (current, pageSize) {
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSizeChange (current, size) {
        this.queryParam.pageNum = 1
        this.queryParam.pageSize = size
        this.getList()
    },
    /** 翻页操作 */
    changeSize (current, pageSize) {
      this.queryParam.pageNum = current
      this.queryParam.pageSize = pageSize
      this.getList()
    },
    /** 翻页操作 */
    onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
        this.ids = this.selectedRows.map(item => item.id)
        this.single = selectedRowKeys.length !== 1
        this.multiple = !selectedRowKeys.length
    },
    /** 查询折叠和展开操作 */
    toggleAdvanced () {
        this.advanced = !this.advanced
    },
    handleAdd () {
          this.showAddModal = true
          this.$nextTick(() => (
                  this.$refs.cargoDetailAddForm.handleAdd()
          ))
    },
    handleUpdate (record, ids) {
          this.showEditModal = true
          this.$nextTick(() => (
                  this.$refs.cargoDetailEditForm.handleUpdate(record, ids)
          ))
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const cargoDetailIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          return delCargoDetail(cargoDetailIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    handleCheck (e) {
      var that = this
      this.$confirm({
        title: '审核',
        content: '请确认您的操作',
        okText: '确认',
        cancelType: 'danger',
        cancelText: '取消',
        onOk () {
          return apply({
            status: e.key,
            ids: that.ids
          }).then(res => {
            that.onSelectChange([], [])
            that.getList()
          })
        }
        // onCancel () {
        //    return apply({
        //     status: 2,
        //     ids: that.ids
        //   }).then(res => {
        //     that.onSelectChange([], [])
        //     that.getList()
        //   })
        // }
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportCargoDetail(that.queryParam)
            .then(response => {
              // that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>
