<template>
  <a-drawer
    :zIndex="zIndex"
    title="导入缺货"
    placement="right"
    :visible="isShow"
    :width="width"
    :get-container="false"
    :wrap-style="{ position: 'absolute' }"
    @close="isShow=false"
    wrapClassName="outStock-modal"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="outStock-con">
        <div>
          <div ref="tableSearch" class="table-page-search-wrapper">
            <a-form layout="inline" @keyup.enter.native="searchForm">
              <a-row type="flex" :gutter="12" justify="start">
                <a-col :md="10" :sm="24">
                  <a-form-model-item label="创建时间" prop="time">
                    <rangeDate ref="rangeDate" :value="time" @change="dateChange" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="8" :sm="24">
                  <a-form-item label="采购单号">
                    <a-input id="outStockList-purchaseBillNo" v-model.trim="queryParam.purchaseBillNo" allowClear placeholder="请输入采购单号"/>
                  </a-form-item>
                </a-col>
                <a-col flex="auto">
                  <a-button type="primary" @click="searchForm" :disabled="disabled" id="productInfoList-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="productInfoList-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <!-- <p style="font-weight: bold;">当前已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys?rowSelectionInfo.selectedRowKeys.length:0 }}个</p> -->
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.purchaseBillSn"
            rowKeyName="purchaseBillSn"
            :row-selection="{ columnWidth: 40 }"
            @rowSelection="rowSelectionFun"
            :data="loadData"
            :columns="columns"
            :style="{ height: tableHeight+84.5+'px' }"
            :scroll="{ y:tableHeight, x: 1000 }"
            bordered>
          </s-table>
        </div>
        <!-- 按钮 -->
        <div class="btn-con" ref="btnBox">
          <a-button
            id="outStock-cancel"
            size="large"
            class="button-cancel"
            @click="isShow=false"
            style="padding: 0 30px;margin-right: 30px;">取消</a-button>
          <a-button
            type="primary"
            id="outStock-save"
            size="large"
            class="button-primary"
            @click="handleSave"
            style="padding: 0 30px;">确定</a-button>
        </div>
      </div>
    </a-spin>
  </a-drawer>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { outOfStockList } from '@/api/oos'
import ProductType from '../../common/productType.js'
import ProductBrand from '../../common/productBrand.js'
import rangeDate from '@/views/common/rangeDate.vue'
import getDate from '@/libs/getDate.js'
export default {
  name: 'OutStockModal',
  components: { STable, ProductType, ProductBrand, rangeDate, getDate },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    const _this = this
    return {
      isShow: this.openModal, //  是否打开弹框
      zIndex: 90,
      width: '70%',
      tableHeight: 0,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '8%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '采购单号', dataIndex: 'purchaseBillNo', width: '20%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '缺货款数', dataIndex: 'totalCategory', width: '16%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货数量', dataIndex: 'totalQty', width: '16%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '缺货金额', dataIndex: 'totalAmount', width: '16%', align: 'right', customRender: function (text) { return ((text || text == 0) ? _this.toThousands(text, 2) : '--') } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return outOfStockList(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
        })
      },
      spinning: false,
      rowSelectionInfo: null,
      time: [
        getDate.getCurrMonthDays().starttime,
        getDate.getCurrMonthDays().endtime
      ],
      queryParam: { //  查询条件
        purchaseBillNo: '',
        beginDate: getDate.getCurrMonthDays().starttime,
        endDate: getDate.getCurrMonthDays().endtime
      }
    }
  },
  methods: {
    //  时间  change
    dateChange (date) {
      this.queryParam.beginDate = date[0]
      this.queryParam.endDate = date[1]
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 保存
    handleSave () {
      const _this = this
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      _this.$emit('ok', this.rowSelectionInfo.selectedRowKeys)
    },
    searchForm () {
      this.$refs.table.refresh(true)
    },
    resetSearchForm () {
      this.$refs.rangeDate.resetDate(this.time)
      this.queryParam.beginDate = getDate.getCurrMonthDays().starttime
      this.queryParam.endDate = getDate.getCurrMonthDays().endtime
      this.queryParam.purchaseBillNo = ''
      this.$refs.table.refresh(true)
    },
    setTableH () {
      this.$nextTick(() => {
        const searchBoxH = this.$refs.tableSearch.offsetHeight
        const tabBoxH = this.$refs.btnBox.offsetHeight
        this.tableHeight = window.innerHeight - searchBoxH - tabBoxH - 300
      })
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    },
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$refs.table.clearSelected() // 清空表格选中项
        this.$emit('close')
      } else {
        setTimeout(() => {
          this.setTableH()
        }, 200)
        this.resetSearchForm()
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .outStock-modal{
    .outStock-con{
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
