<template>
  <a-card :bordered="false" v-if="visible">

    <div class="table-operator">
      <a-button v-if="$auth('purchase-purchase-dispatch-order-lists-add')" type="primary" icon="plus" @click="handleChoose">选择</a-button>
<!--      <a-button style="margin-left: 8px" v-if="$auth('purchase-purchase-dispatch-order-lists-export')" type="primary" icon="download" @click="doExport">导出</a-button>-->
    </div>

    <s-table
      ref="table"
      size="small"
      rowKey="id"
      :columns="columns"
      :data="loadData"
      :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :showSizeChanger="showSizeChanger"
      :showPagination="showSizeChanger"
    >
      <span slot="action" slot-scope="record">
        <template>
          <operation-button
            :showDivider="false"
            v-if="$auth('purchase-purchase-dispatch-order-lists-edit')"
            @click="handleEdit(record)"
          >修改</operation-button>
          <operation-button
            v-if="$auth('purchase-purchase-dispatch-order-lists-del')"
            :type="2"
            title="是否要删除该条数据？"
            @confirm="batchDelete(record)">删除</operation-button>
        </template>
      </span>
    </s-table>
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal"/>
    <purchase-order-list-select-modal :type="'checkbox'" ref="selectModal" @selected="handleSelected"></purchase-order-list-select-modal>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseForm from './modules/BaseForm'
import Detail from './modules/Detail'
import PurchaseOrderListSelectModal from './modules/PurchaseOrderListSelectModal'
import { getPurchaseDispatchOrderListPage, exportPurchaseDispatchOrderList } from '@/api/purchase/purchase-dispatch-order-list'

export default {
  name: 'PurchaseDispatchOrderListListForChoose',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    PurchaseOrderListSelectModal,
    Detail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        pageSize: 9999
      },
      visible: false,
      dispatchOrderId: '-1',
      purchaseOrderId: '-1',
      showSizeChanger: false,
      addFlag: null,
      isCreated: false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '编号',
          dataIndex: 'no',
          checked: true,
          width: 200
        },
        {
          title: '名称',
          dataIndex: 'name',
          checked: true,
          width: 150
        },
        {
          title: '规格',
          dataIndex: 'specs',
          checked: true,
          width: 100
        },
        {
          title: '单位',
          dataIndex: 'unit',
          checked: true,
          width: 90
        },
        {
          title: '币种',
          dataIndex: 'moneyType',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.moneyTypeMap, text)
          }
        },
        {
          title: '单价',
          dataIndex: 'price',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '总额',
          dataIndex: 'totalMoney',
          width: 130,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '税额',
          dataIndex: 'taxMoney',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          width: 90
        },
        {
          title: '适用车型',
          dataIndex: 'suitableModel',
          width: 150
        },
        // {
        //   title: '车架号',
        //   dataIndex: 'vin',
        //   width: 150
        // },
        // {
        //   title: '发动机号',
        //   dataIndex: 'engineNum',
        //   width: 150
        // },
        {
          title: '已发运数量',
          dataIndex: 'dispatchedQuantity',
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '未发运数量',
          dataIndex: 'notDispatchQuantity',
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '产地',
          dataIndex: 'producePlace',
          width: 90,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.areaMap, text)
          }
        },
        {
          title: '重量',
          dataIndex: 'weight',
          width: 100,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '体积',
          dataIndex: 'volume',
          width: 100,
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '外形尺寸',
          dataIndex: 'dimension',
          width: 100
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 100
        },
        {
          title: '创建人名称',
          dataIndex: 'createdUserName',
          width: 100
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 100
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          checked: true,
          fixed: 'right',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 下拉框map
      unitMap: {},
      moneyTypeMap: {},
      areaMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          dispatchOrderId: this.dispatchOrderId,
          purchaseOrderId: this.purchaseOrderId,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseDispatchOrderListPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            const data = res.data
            data.rows = this.BaseTool.ListForm.getOneListHandledRows(this, data.rows)
            data.total = res.data.rows.length
            this.$store.dispatch('setCheckedRows', data.rows)
            return data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.areaMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    this.tableOption()
  },
  methods: {
    base () {
      this.visible = true
      this.addFlag = this.$store.getters.addFlag
      this.dispatchOrderId = this.$store.getters.purchaseDispatchId
      this.purchaseOrderId = this.$store.getters.purchaseOrderId
      console.log(this.dispatchOrderId)
      console.log(this.purchaseOrderId)
      if (this.isCreated) {
        this.handleOk()
      } else {
        this.tableOption()
        this.isCreated = true
      }
      this.visible = true
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: false,
                name: record.id
              }
            })
          }
        }
        this.optionAlertShow = true
      } else {
        this.options = {
          alert: false,
          rowSelection: null
        }
        this.optionAlertShow = false
      }
    },
    batchDelete (record) {
      let rows = []
      if (record == null) {
        const length = this.selectedRows.length
        if (length === 0) {
          this.$message.info('请选择要删除的记录')
          return
        }
        rows = [ ...this.selectedRows ]
      } else {
        rows.push(record)
      }
      this.BaseTool.ListForm.pushOneListDelete(this, rows)
      this.handleOk()
      this.$refs.table.clearSelected()
    },
    handleEdit (record) {
      const modal = this.$refs.baseModal
      modal.base(record)
    },
    handleChoose () {
      this.purchaseOrderId = this.$store.getters.purchaseOrderId
      console.log(this.purchaseOrderId)
      if (!this.purchaseOrderId || this.purchaseOrderId === '-1') {
        this.$message.error('请选择采购单')
        return
      }
      this.$refs.selectModal.base(null, { purchaseOrderId: this.purchaseOrderId, minStatus: this.DictCache.VALUE.PURCHASE_LIST_STATUS.PURCHASE_LIST_STATUS_ORDER, notDispatchQuantity: 0 })
    },
    handleSelected (record, keys, rows) {
      const records = []
      rows.forEach((item) => {
        item.price = item.supplyPrice
        item.totalMoney = item.supplyTotalMoney
        records.push(item)
      })
      this.BaseTool.ListForm.pushOneListAddMore(this, records)
      this.handleOk()
    },
    handleView (record) {
      const modal = this.$refs.detailModal
      modal.base(record)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        dispatchOrderId: this.dispatchOrderId,
        pageSize: 9999
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportPurchaseDispatchOrderList(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleCancel (values) {
      this.visible = false
      this.$emit('ok', values)
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
