<template>
  <a-card :bordered="false" v-if="visible">
    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :columns="columns"
      :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      :showSizeChanger="showSizeChanger"
      :showPagination="showSizeChanger"
    >
      <span slot="action" slot-scope="record">
        <template>
          <operation-button
            :showDivider="false"
            @click="handleEdit(record)"
          >修改</operation-button>
          <operation-button
            :type="2"
            title="是否要删除该条数据？"
            @confirm="batchDelete(record)">删除</operation-button>
        </template>
      </span>
    </s-table>
    <audit-base-form ref="baseModal" @ok="handleOk"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import AuditBaseForm from './list-modules/AuditBaseForm'
import { getPurchaseApplyListAudit, exportPurchaseApplyList } from '@/api/purchase/purchase-apply-list'

export default {
  name: 'PurchaseApplyListAudit',
  components: {
    STable,
    Ellipsis,
    AuditBaseForm
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        pageSize: 9999
      },
      visible: false,
      purchaseApplyId: '-1',
      showSizeChanger: false,
      addFlag: null,
      isCreated: false,
      columns: [],
      // 表头
      columnItems: [
        {
          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: 'price',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '库存数量',
          dataIndex: 'storeQuantity',
          checked: true,
          width: 90,
          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: 'totalMoney',
          checked: true,
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '品牌',
          dataIndex: 'brand',
          checked: true,
          width: 120
        },
        {
          title: '适用车型',
          dataIndex: 'suitableModel',
          width: 120
        },
        // {
        //   title: '车架号',
        //   dataIndex: 'vin',
        //   width: 150
        // },
        // {
        //   title: '发动机号',
        //   dataIndex: 'engineNum',
        //   width: 150
        // },
        {
          title: '已购数量',
          dataIndex: 'boughtQuantity',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已到数量',
          dataIndex: 'arrivedQuantity',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '未到数量',
          dataIndex: 'notArriveQuantity',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已用金额',
          dataIndex: 'usedMoney',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '已发运数量',
          dataIndex: 'dispatchedQuantity',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '未发运数量',
          dataIndex: 'notDispatchQuantity',
          width: 90,
          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: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '体积',
          dataIndex: 'volume',
          width: 90,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '外形尺寸',
          dataIndex: 'dimension',
          width: 90
        },
        {
          title: '供应商名称',
          dataIndex: 'supplierName',
          width: 150
        },
        {
          title: '供应商单价',
          dataIndex: 'supplyPrice',
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '供应商总额',
          dataIndex: 'supplyTotalMoney',
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '备注',
          dataIndex: 'remark',
          width: 100

        },
        // {
        //   title: '状态',
        //   dataIndex: 'status',
        //   width: 100,
        //   checked: true,
        //   customRender: (text, record, index) => {
        //     return this.BaseTool.Table.getMapText(this.statusMap, text)
        //   }
        // },
        {
          title: '创建人名称',
          dataIndex: 'createdUserName',
          width: 120
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          width: 120
        },
        {
          title: '操作',
          key: 'action',
          width: '200px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 下拉框map
      unitMap: {},
      statusMap: {},
      areaMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          purchaseApplyId: this.purchaseApplyId,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseApplyListAudit(Object.assign(parameter, this.queryParam))
          .then(res => {
            const rows = this.BaseTool.ListForm.getOneListHandledRows(this, res.data)
            this.$emit('loadOk', {})
            return {
              rows: rows,
              total: rows.length,
              pageNum: 1,
              pageSize: 999
            }
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  props: {
    viewFlag: {
      type: Boolean,
      default: false
    }
  },
  created () {
    // 下拉框map
    this.unitMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.SBINFO_UNIT)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_LIST_STATUS)
    this.areaMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PRODUCER_AREA)
    if (!this.viewFlag) {
      this.columns = [
        ...this.columnItems,
        {
          title: '操作',
          key: 'action',
          width: '200px',
          fixed: 'right',
          checked: true,
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ]
      this.optionAlertShow = true
    } else {
      this.columns = [
        ...this.columnItems
      ]
      this.optionAlertShow = false
    }
    this.tableOption()
  },
  methods: {
    base (purchaseApplyId) {
      this.BaseTool.ListForm.clearOneList(this)
      this.visible = true
      this.purchaseApplyId = purchaseApplyId
      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)
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        purchaseApplyId: this.purchaseApplyId,
        pageSize: 9999
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportPurchaseApplyList(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
