<template>
  <a-card size="small" :bordered="false" class="backorderList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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="客户名称">
                <dealerSubareaScopeList ref="custList" id="backorderList-buyerSn" @change="custChange"></dealerSubareaScopeList>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="6" :sm="24">
              <a-form-model-item label="仓库">
                <chooseWarehouse ref="warehouse" v-model="queryParam.warehouseSn"></chooseWarehouse>
              </a-form-model-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 fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="salesBillNo" slot-scope="text, record">
          <span style="padding-right: 15px;">{{ text }}</span>
          <a-badge count="促" v-if="record.promoFlag==1" :number-style="{ backgroundColor: '#52c41a', zoom:'80%' }"></a-badge>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-success"
            @click="handleDetail(record)"
            v-if="$hasPermissions('B_oosDetail')"
            id="backorderList-detail-btn">详情</a-button>
          <span v-else>--</span>
        </template>
      </s-table>
    </a-spin>
    <!-- 详情 -->
    <backorder-detail-modal v-drag :openModal="openModal" :itemSn="itemSn" @close="closeModal" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import rangeDate from '@/views/common/rangeDate.vue'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
import { STable, VSelect } from '@/components'
import backorderDetailModal from './detailModal.vue'
import chooseWarehouse from '@/views/common/chooseWarehouse'
import { oosList } from '@/api/oos'
export default {
  name: 'BackorderList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerSubareaScopeList, backorderDetailModal, rangeDate, chooseWarehouse },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      queryParam: { //  查询条件
        beginDate: '',
        endDate: '',
        buyerSn: undefined,
        warehouseSn: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return oosList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            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
          }
          this.spinning = false
          return data
        })
      },
      openModal: false,
      itemSn: ''
    }
  },
  computed: {
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '12%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '销售单号', dataIndex: 'salesBillNo', width: '20%', align: 'center', scopedSlots: { customRender: 'salesBillNo' }},
        { title: '客户名称', dataIndex: 'dealerName', width: '20%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '仓库', dataIndex: 'warehouseName', width: '15%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '缺货款数', dataIndex: 'totalCategory', width: '11%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货数量', dataIndex: 'totalQty', width: '11%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        // { title: '缺货金额', dataIndex: 'totalAmount', width: '11%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '11%', align: 'center' }
      ]
      if (this.$hasPermissions('M_backorderList_salesPrice')) { //  售价权限
        arr.splice(6, 0, { title: '缺货金额', dataIndex: 'totalAmount', width: '11%', align: 'right', customRender: text => ((text || text == 0) ? this.toThousands(text) : '--') })
      }
      return arr
    }
  },
  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.queryParam.warehouseSn = undefined
      this.$refs.custList.resetForm()
      this.$refs.table.refresh(true)
    },
    // 关闭
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  详情
    handleDetail (row) {
      this.itemSn = row.oosBillSn
      this.openModal = true
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 203
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
