<template>
  <a-card size="small" :bordered="false" class="backorderList-wrap">
    <!-- 搜索条件 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
        <a-row :gutter="15">
          <a-col :md="6" :sm="24">
            <a-form-item label="创建时间">
              <rangeDate ref="rangeDate" @change="dateChange" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-form-item label="客户名称">
              <custList ref="custList" id="backorderList-buyerSn" @change="custChange"></custList>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="backorderList-refresh">查询</a-button>
            <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="backorderList-reset">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 列表 -->
    <s-table
      class="sTable"
      ref="table"
      size="small"
      :rowKey="(record) => record.id"
      :columns="columns"
      :data="loadData"
      :scroll="{ x: 1120, y: tableHeight }"
      bordered>
      <!-- 操作 -->
      <template slot="action" slot-scope="text, record">
        <a-button size="small" type="link" @click="handleDetail(record)" v-if="$hasPermissions('B_oosDetail')" id="backorderList-detail-btn">详情</a-button>
        <span v-else>--</span>
      </template>
    </s-table>
    <!-- 详情 -->
    <backorder-detail-modal :openModal="openModal" :itemSn="itemSn" @close="closeModal" />
  </a-card>
</template>

<script>
import rangeDate from '@/views/common/rangeDate.vue'
import custList from '@/views/common/custList.vue'
import { STable, VSelect } from '@/components'
import backorderDetailModal from './detailModal.vue'
import { oosList } from '@/api/oos'
export default {
  components: { STable, VSelect, custList, backorderDetailModal, rangeDate },
  data () {
    return {
      tableHeight: 0, // 表格高度
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        buyerSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 160, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '客户名称', dataIndex: 'dealerName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '缺货款数', dataIndex: 'totalCategory', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货数量', dataIndex: 'totalQty', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货金额', dataIndex: 'totalAmount', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 315
        }
        return oosList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false,
      itemSn: ''
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    custChange (obj) {
      this.queryParam.buyerSn = obj.key
    },
    //  重置
    resetSearchForm () {
      this.$refs.rangeDate.resetDate()
      this.queryParam.beginDate = ''
      this.queryParam.endDate = ''
      this.queryParam.buyerSn = undefined
      this.$refs.table.refresh(true)
    },
    // 关闭
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  详情
    handleDetail (row) {
      this.itemSn = row.oosBillSn
      this.openModal = true
    }
  }
}
</script>
