<template>
  <a-card :bordered="false" >
    <div v-if="visible">
      <div class="table-operator">
        <a-button v-if="$auth('purchase-purchase-contract-main-lists-add')" type="primary" icon="plus" @click="handleAdd">填写明细</a-button>
        <a-button style="margin-left: 8px" v-if="$auth('purchase-purchase-contract-main-lists-export')" type="primary" icon="download" @click="doExport">导出</a-button>
        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('purchase-purchase-contract-main-lists-del')">
          <a-menu slot="overlay">
            <a-popconfirm title="是否要删除所选数据？" @confirm="batchDelete()">
              <a-menu-item key="1"><a-icon type="delete" /><a>删除</a></a-menu-item>
            </a-popconfirm>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <s-table
        ref="table"
        size="default"
        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-->
        <!--              :show-divider="false"-->
        <!--              v-if="$auth('purchase-purchase-contract-main-lists-edit')"-->
        <!--              @click="handleEdit(record)"-->
        <!--            >修改</operation-button>-->
        <!--            <operation-button-->
        <!--              v-if="$auth('purchase-purchase-contract-main-lists-del')"-->
        <!--              :type="2"-->
        <!--              title="是否要删除该条数据？"-->
        <!--              @confirm="batchDelete(record)">删除</operation-button>-->
        <!--          </template>-->
        <!--        </span>-->
      </s-table>
    </div>
    <base-form ref="baseModal" @ok="handleOk"/>
    <base-form-for-sb ref="baseModalForSb" @ok="handleOk"/>
    <detail ref="detailModal"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseForm from './main-list-modules/BaseForm'
import BaseFormForSb from './main-list-modules/BaseFormForSb'
import Detail from './main-list-modules/Detail'
import { getPurchaseContractMainListPage, deletePurchaseContractMainLists, fetchPurchaseContractMainList, exportPurchaseContractMainList } from '@/api/purchase/purchase-contract-main-list'

export default {
  name: 'PurchaseContractMainListList',
  components: {
    STable,
    Ellipsis,
    BaseFormForSb,
    BaseForm,
    Detail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
        pageSize: 9999
      },
      visible: false,
      contractId: '-1',
      showSizeChanger: false,
      addFlag: null,
      isCreated: false,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${index + 1}`
          }
        },
        {
          title: '申购明细',
          dataIndex: 'name',
          checked: true,
          width: 120
        },
        {
          title: '型号',
          dataIndex: 'model',
          checked: true,
          width: 120
        },
        {
          title: '数量',
          dataIndex: 'quantity',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '单位',
          dataIndex: 'unit',
          checked: true,
          width: 100
        },
        {
          title: '单价',
          dataIndex: 'price',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '总价',
          dataIndex: 'totalMoney',
          checked: true,
          width: 100,
          customRender: (text, record, index) => {
            return this.BaseTool.Amount.formatter(text)
          }
        },
        {
          title: '整机编号',
          dataIndex: 'machineNo',
          checked: true,
          width: 180
        },
        // {
        //   title: '发动机型号',
        //   dataIndex: 'engineModel',
        //   checked: true,
        //   width: 180
        // },
        // {
        //   title: '发动机编号',
        //   dataIndex: 'engineNo',
        //   checked: true,
        //   width: 180
        // },
        {
          title: '备注',
          dataIndex: 'remark',
          checked: true,
          width: 150
        }
        // {
        //   title: '操作',
        //   key: 'action',
        //   width: '200px',
        //   checked: true,
        //   fixed: 'right',
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      // 下拉框map
      unitMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
          contractId: this.contractId,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getPurchaseContractMainListPage(Object.assign(parameter, this.queryParam))
          .then(res => {
            const data = res.data
            const oneListAdd = this.$store.getters.oneListAdd
            if (oneListAdd && oneListAdd.length > 0) {
              data.rows = oneListAdd
              data.total = data.rows.length
            }
            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.CONTRACT_PROJECT_UNIT)
    this.tableOption()
  },
  methods: {
    base (contractId) {
      this.visible = true
      this.addFlag = this.$store.getters.addFlag
      this.contractId = contractId
      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()
    },
    handleEdit (record) {
      const modal = this.$refs.baseModal
      modal.base(record)
    },
    handleView (record) {
      const modal = this.$refs.detailModal
      modal.base(record)
    },
    handleAdd () {
      this.visible = false
      const type = this.$store.getters.purchaseType
      if (type > this.DictCache.VALUE.PURCHASE_TYPE.PURCHASE_TYPE_SBL) {
        this.$refs.baseModal.base(null, this.contractId)
      } else {
        this.$refs.baseModalForSb.base(null, this.contractId)
      }
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
        contractId: this.contractId,
        pageSize: 9999
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportPurchaseContractMainList(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
