<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">

           <a-col :md="6" :sm="24">
              <a-form-item label="单据起始日期">
                 <a-date-picker
                  v-model="queryParam.searchStartTime"
                  style="width: 100%"
                  :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                  v-decorator="['searchStartTime']"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="单据结束日期">
                   <a-date-picker
                    v-model="queryParam.searchEndTime"
                    style="width: 100%"
                    :format="BaseTool.Date.PICKER_NORM_DATE_PATTERN"
                    v-decorator="['searchEndTime']"/>
              </a-form-item>
            </a-col>
          <a-col :md="4 || 24" :sm="24">
            <span class="table-page-search-submitButtons">
              <a-button type="primary" @click="handleOk">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button style="margin-left: 8px" v-if="$auth('store-spare-pick-details-export')" type="primary" icon="download" @click="doExport">导出</a-button>
    </div>
    <s-table
      ref="table"
      size="default"
      rowKey="spareId"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
    </s-table>
    <base-form ref="baseModal" @ok="handleOk"/>
    <detail ref="detailModal"/>
  </a-card>
</template>

<script>
import { STable, Ellipsis } from '@/components'
import BaseForm from './modules/BaseForm'
import Detail from './modules/Detail'
import { getSparePickDetailPage, deleteSparePickDetails, fetchSparePickDetail, exportSparePickDetail } from '@/api/report/sparepickdetail'

export default {
  name: 'SparePickDetailList',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    Detail
  },
  data () {
    return {
      // 查询参数
      queryParam: {
       searchStartTime:null,
       searchEndTime:null
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '备件编号',
          dataIndex: 'spareNo',
        },
        {
          title: '备件名称',
          dataIndex: 'spareId',
          customRender: (text, record, index) => {
            return record.spareName
          }
        },
        {
          title: '领用总数',
          dataIndex: 'totalNum'
        },
      ],
      // 下拉框map
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        parameter = {
          ...parameter,
          ...this.queryParam,
           searchStartTime: this.queryParam.searchStartTime ? this.queryParam.searchStartTime.format(this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN) : null,
                    searchEndTime: this.queryParam.searchEndTime ? this.queryParam.searchEndTime.format(this.BaseTool.Date.PICKER_NORM_DATETIME_PATTERN) : null,

          dataScope: {
          }
        }
        return getSparePickDetailPage(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.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
      }
    },
    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]
      }
      deleteSparePickDetails(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    },
    handleEdit (record) {
      fetchSparePickDetail({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleView (record) {
      fetchSparePickDetail({ id: record.id }).then(res => {
        const modal = this.$refs.detailModal
        modal.base(res.data)
      })
    },
    handleOk () {
      this.$refs.table.refresh()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    resetSearchForm () {
      this.queryParam = {
      }
      this.$refs.table.refresh(true)
    },
    doExport () {
      const parameter = {
        ...this.queryParam
      }
      exportSparePickDetail(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    }
  }
}
</script>
