<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-tree-select
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '400px', overflow: 'auto' }"
                :treeData="storeTreeData"
                :treeNodeFilterProp="'name'"
                :showSearch="true"
                v-decorator="['storeId']"
                v-model="queryParam.storeId"
                placeholder="请选择"
              >
              </a-tree-select>
            </a-form-item>
          </a-col>
           <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-out-store-details-export')" type="primary" icon="download" @click="doExport">导出</a-button>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="id"
      :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 {fetchStoreTree} from '@/api/store/store'
import { getOutStoreDetailPage, deleteOutStoreDetails, fetchOutStoreDetail, exportOutStoreDetail } from '@/api/store/outstoredetail'

export default {
  name: 'OutStoreDetailList',
  components: {
    STable,
    Ellipsis,
    BaseForm,
    Detail
  },
  props: {
    filter: {
      type: Number,
      default: -1
    }
  },
  data () {
    return {
      storeTreeData: [],
      // 查询参数
      queryParam: {
        storeId: null,
        searchStartTime: null,
        searchEndTime: null,
        filter: this.filter
      },
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          customRender: (text, record, index) => {
            return `${(this.$refs.table.localPagination.current - 1) * this.$refs.table.localPagination.pageSize + index + 1}`
          }
        },
        {
          title: '出库单号',
          dataIndex: 'outNo'
        },
        {
          title: '备件规格',
          dataIndex: 'ggxh'
        },
        {
          title: '原厂编号',
          dataIndex: 'initNo'
        },
        {
          title: '备件名称',
          dataIndex: 'spareId',
          customRender: (text, record, index) => {
            return record.spareName
          }
        },
        {
          title: '出库数量',
          dataIndex: 'num'
        },
        {
          title: '出库日期',
          dataIndex: 'createdTime'
        },
        {
          title: '出库仓库',
          dataIndex: 'storeId',
          customRender: (text, record, index) => {
            return record.storeName
          }
        }
      ],
      // 下拉框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: {
            sortBy: 'desc',
            sortName: 'created_time'
          }
        }
        return getOutStoreDetailPage(parameter)
          .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()
    // 设置仓库选项
    this.setTree()
  },
  methods: {
    setTree () {
      fetchStoreTree().then(res => {
        this.storeTreeData = res.data
      })
    },
    tableOption () {
      if (!this.optionAlertShow) {
        this.options = {
          alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
          rowSelection: {
            selectedRowKeys: this.selectedRowKeys,
            onChange: this.onSelectChange,
            getCheckboxProps: record => ({
              props: {
                disabled: true,
                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]
      }
      deleteOutStoreDetails(ids).then(res => {
        this.$message.info('删除成功')
        this.handleOk()
        this.$refs.table.clearSelected()
      })
    },
    handleEdit (record) {
      fetchOutStoreDetail({ id: record.id }).then(res => {
        const modal = this.$refs.baseModal
        modal.base(res.data)
      })
    },
    handleView (record) {
      fetchOutStoreDetail({ 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
      }
      exportOutStoreDetail(parameter).then(file => {
        this.BaseTool.UPLOAD.downLoadExportExcel(file)
      })
    }
  }
}
</script>
