<template>
  <a-card :bordered="false">
    <div v-show="visible">
      <div class="table-page-search-wrapper" @keyup.enter="handleEnter">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-item label="设备名称">
                <a-input v-model.trim="queryParam.sbName" placeholder="请输入设备名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="设备位号">
                <a-input v-model.trim="queryParam.positionNo" placeholder="请输入设备位号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="开始时间">
                <a-date-picker
                  :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                  v-model="queryParam.createdTimeStart"
                  placeholder="开始时间"
                />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="结束时间">
                <a-date-picker
                  :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                  v-model="queryParam.createdTimeEnd"
                  placeholder="结束时间"
                />
              </a-form-item>
            </a-col>
            <a-col :md="8 || 24" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <!--  <div class="table-operator" style="margin-bottom: 8px;">
          <a-button v-if="$auth('purchase-purchase-orders-add')" type="primary" icon="plus" @click="$refs.baseModal.base()">新增</a-button>
        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0 && $auth('purchase-purchase-orders-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"
        :scroll="{x: 1, y: BaseTool.Constant.scrollY }"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        showPagination="auto"
      >
        <span slot="action" slot-scope="record">
          <template>
          </template>
        </span>
      </s-table>
    </div>
    <!--    <purchase-dispatch-order ref="dispatchOrder" @ok="handleOk"/>-->
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import { getpurchaseRecord, deletePurchaseOrders } from '@/api/purchase/purchase-order'

export default {
  name: 'PurchaseOrderList',
  components: {
    STable,
    Ellipsis
  },
  data () {
    return {
      // 查询参数
      queryParam: {
      },
      visible: true,
      user: this.$store.getters.userInfo,
      dollarExchangeRate: this.$store.getters.rmbRate,
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          checked: true,
          width: 70,
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '存货编码',
          dataIndex: 'no',
          checked: true,
          width: 200
        },
        {
          title: '规格型号',
          dataIndex: 'ggxh',
          checked: true,
          width: 150
        },
        {
          title: '设备信息',
          dataIndex: 'sbInfo',
          checked: true,
          width: 150
        },
        {
          title: '备件名称',
          dataIndex: 'spareName',
          checked: true,
          width: 150
        },
        {
          title: '仓库名称',
          dataIndex: 'storeName',
          checked: true,
          width: 120
        },
        {
          title: '货架号',
          dataIndex: 'storePosition',
          checked: true,
          width: 100
        },
        {
          title: '采购数量',
          dataIndex: 'num',
          checked: true,
          width: 150
        },
        {
          title: '已采购数量',
          dataIndex: 'purchasedNum',
          checked: true,
          width: 150
        },
        {
          title: '已入库数量',
          dataIndex: 'inStoreNum',
          checked: true,
          width: 150
        },
        {
          title: '明细状态',
          width: 100,
          dataIndex: 'detailStatus',
          checked: true,
          customRender: (text, record, index) => {
            return this.BaseTool.Table.getMapText(this.detailStatusMap, text)
          }
        },

        {
          title: '创建人',
          dataIndex: 'createdUserName',
          checked: true,
          width: 120
        },
        {
          title: '创建时间',
          dataIndex: 'createdTime',
          checked: true,
          width: 120
        }
        // {
        //   title: '操作',
        //   key: 'action',
        //   width: '200px',
        //   fixed: 'right',
        //   checked: true,
        //   align: 'center',
        //   scopedSlots: { customRender: 'action' }
        // }
      ],
      // 下拉框map
      typeMap: {},
      statusMap: {},
      detailStatusMap: {},
      payTypeMap: {},
      invoiceTypeMap: {},
      moneyTypeMap: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.queryParam.createdTimeStart = this.queryParam.createdTimeStart ? this.BaseTool.Date.formatter(this.queryParam.createdTimeStart, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN) + ' 00:00:00' : null
        this.queryParam.createdTimeEnd = this.queryParam.createdTimeEnd ? this.BaseTool.Date.formatter(this.queryParam.createdTimeEnd, this.BaseTool.Date.PICKER_NORM_DATE_PATTERN) + ' 23:59:59' : null
        parameter = {
          ...parameter,
          ...this.queryParam,
          dataScope: {
            sortBy: 'desc',
            sortName: 'update_time'
          }
        }
        return getpurchaseRecord(Object.assign(parameter, this.queryParam))
          .then(res => {
            return res.data
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: false
    }
  },
  created () {
    // 下拉框map
    this.typeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_APPLY_TYPE)
    this.statusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_ORDER_STATUS)
    this.payTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_PAY_TYPE)
    this.invoiceTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.PURCHASE_INVOICE_TYPE)
    this.moneyTypeMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.MONEY_TYPE)
    this.detailStatusMap = this.DictCache.getLabelByValueMapByType(this.DictCache.TYPE.LONG_YAN_PURCHASE_DETAIL_STATUS)

    this.tableOption()
  },
  methods: {
    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
      }
    },
    handleOk () {
      this.visible = true
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    batchDelete (id) {
      let ids = []
      if (this.BaseTool.String.isBlank(id)) {
        const length = this.selectedRows.length
        if (length === 0) {
          this.$message.info('请选择要删除的记录')
          return
        }
        ids = this.selectedRows.map(item => item.id)
      } else {
        ids = [id]
      }
      deletePurchaseOrders(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    },
    handleEnter () {
      this.$refs.table.refresh(true)
    }
  }
}
</script>
