<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.waybillNo" 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.dataSource" style="width: 100%" allow-clear>
                  <a-select-option value="CFPS">CFPS</a-select-option>
                  <a-select-option value="ALMS">ALMS</a-select-option>
                  <a-select-option value="CSIS">CSIS</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6" >
              <a-form-item label="航班号">
                <a-input v-model="queryParam.flightNo" 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.status" style="width: 100%" allow-clear>
                  <a-select-option v-for="(d, index) in statusOptions" :key="index" :value="d.dictValue">{{ d.dictLabel }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <!-- <a-col :span="6" v-if="advanced">
              <a-form-item label="航班类型">
                <a-input v-model="queryParam.flightType" placeholder="请输入航班类型" allow-clear @keyup.enter.native="handleQuery"/>
              </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>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false" class="table-card">
      <!-- 增加 -->
      <waybill-add-form
        v-if="showAddModal"
        ref="waybillAddForm"
        :statusOptions="statusOptions"
        @ok="getList"
        @close="showAddModal = false"
      />
      <!-- 编辑 -->
      <waybill-edit-form
        v-if="showEditModal"
        ref="waybillEditForm"
        :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-waybill-index-table"
        rowKey="id"
        size="middle"
        @refresh="getList"
        :columns="columns"
        :data-source="waybillList"
        :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="" @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="riskClassificationStatus" :style="{ color: riskClassificationColor[record.riskClassification] }" slot-scope="{record}">
          {{ record.riskClassification }}
        </span>
        <span slot="riskRatingStatus" :style="{ color: riskRatingColor[record.riskRating] }" slot-scope="{record}">
          {{ record.riskRating }}
        </span>
        <span slot="status"  slot-scope="{record}">
          {{ statusFormat(record) }}
        </span>
        <span slot="sendStatus"  slot-scope="{record}">
          {{ sendStatusFormat(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>
  </div>
</template>
<script>
import { listWaybill, delWaybill, exportWaybill } from '@/api/system/waybill'
import AdvanceTable from '@/components/pt/table/AdvanceTable'
import WaybillAddForm from '@/views/system/waybill/modules/WaybillAddForm'
import WaybillEditForm from '@/views/system/waybill/modules/WaybillEditForm'
export default {
  name: 'Waybill',
  components: {
    AdvanceTable,
    WaybillAddForm,
    WaybillEditForm
  },
  data () {
    return {
      riskRatingColor: {
        '严控': '	#8B0000',
        '高风险': 'red',
        '普通': 'black',
        '优先': 'green',
        '低风险': 'green'
      },
      riskClassificationColor: {
        '高风险': 'red',
        '普通': 'black',
        '低风险': 'green'
      },
      showAddModal: false,
      showEditModal: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中的主键集合
      selectedRowKeys: [],
      // 选中的数据集合
      selectedRows: [],
      // 高级搜索 展开/关闭
      advanced: false,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // label的百分比
      labelCol: { span: 6 },
      // 内容区域的百分比
      wrapperCol: { span: 18 },
      // 运单管理表格数据
      waybillList: [],
      // 是否已处理 0 未处理 1 已处理字典
      statusOptions: [
        {
          dictLabel: '处理异常',
          dictValue: '2'
        },
        {
          dictLabel: '已处理',
          dictValue: '1'
        },
        {
          dictLabel: '未处理',
          dictValue: '0'
        }
      ],
       // 是否已处理 0 未推送 1 已推送
      sendStatusOptions: [
        {
          dictLabel: '已推送',
          dictValue: '1'
        },
        {
          dictLabel: '未推送',
          dictValue: '0'
        }
      ],
      // 查询参数
      queryParam: {
        pageNum: 1,
        pageSize: 10,
        waybillNo: undefined,
        dataSource: undefined,
        flightNo: undefined,
        flightType: undefined
      },
      columns: [
        {
          title: '运单号',
          dataIndex: 'waybillNo',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '运单来源',
          dataIndex: 'dataSource',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '航班号',
          dataIndex: 'flightNo',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '警卫/重点航班或航线',
          dataIndex: 'isImportantFlights',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '航班日期',
          dataIndex: 'flightDate',
          align: 'center',
          width: '120px'
        },
        {
          title: '货品名',
          dataIndex: 'goodsName',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        // {
        //   title: '代理人名称',
        //   dataIndex: 'organization',
        //   ellipsis: true,
        //   align: 'left',
        //   width: '10%'
        // },
        {
          title: '代理人编码',
          dataIndex: 'agentCode',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '代理人信用等级',
          dataIndex: 'agentLevel',
          ellipsis: true,
          align: 'left',
          width: '10%'
        },
        {
          title: '货物风险等级',
          dataIndex: 'riskClassification',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'riskClassificationStatus' }
        },
        {
          title: '综合风险等级',
          dataIndex: 'riskRating',
          ellipsis: true,
          align: 'left',
          width: '10%',
          scopedSlots: { customRender: 'riskRatingStatus' }
        },
        {
          title: '处理状态',
          dataIndex: 'status',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '推送状态',
          dataIndex: 'sendStatus',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'sendStatus' }
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          ellipsis: false,
          align: 'left',
          width: '150px'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          align: 'center',
          width: '10%',
          scopedSlots: { customRender: 'operation' }
        }
      ]
    }
  },
  created () {
    this.getList()
  },
  methods: {
    /** 查询运单管理列表 */
    getList () {
      this.loading = true
      listWaybill(this.queryParam).then(response => {
        this.waybillList = response.data.list
        this.total = response.data.total
        this.loading = false
      })
    },
    // 是否已处理 0 未处理 1 已处理字典翻译
    statusFormat (row) {
        if (row.status || row.status === 0) {
            return this.selectDictLabel(this.statusOptions, row.status + '')
        } else {
            return ''
        }
    },
    sendStatusFormat (row) {
        if (row.sendStatus || row.sendStatus === 0) {
            return this.selectDictLabel(this.sendStatusOptions, row.sendStatus + '')
        } else {
            return ''
        }
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParam.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.queryParam = {
        pageNum: 1,
        pageSize: 10,
        waybillNo: undefined,
        dataSource: undefined,
        flightNo: undefined,
        flightType: 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 () {
		console.log(this.showAddModal)
          this.showAddModal = true
          this.$nextTick(() => (
                  this.$refs.waybillAddForm.handleAdd()
          ))
    },
    handleUpdate (record, ids) {
          this.showEditModal = true
          this.$nextTick(() => (
                  this.$refs.waybillEditForm.handleUpdate(record, ids)
          ))
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      var that = this
      const waybillIds = row.id || this.ids
      this.$confirm({
        title: '确认删除所选中数据?',
        onOk () {
          return delWaybill(waybillIds)
            .then(() => {
              that.onSelectChange([], [])
              that.getList()
              that.$message.success(
                '删除成功',
                3
              )
          })
        },
        onCancel () {}
      })
    },
    /** 导出按钮操作 */
    handleExport () {
      var that = this
      this.$confirm({
        title: '是否确认导出?',
        content: '此操作将导出当前条件下所有数据而非选中数据',
        onOk () {
          return exportWaybill(that.queryParam)
            .then(response => {
              that.download(response.msg)
              that.$message.success(
                '导出成功',
                3
              )
          })
        },
        onCancel () {}
      })
    }
  }
}
</script>
