<template>
  <a-modal
    centered
    class="outStock-modal"
    :footer="null"
    :maskClosable="false"
    title="上次采购缺货产品明细"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="outStock-con">
        <div>
          <p style="font-weight: bold;">当前已选：{{ rowSelectionInfo&&rowSelectionInfo.selectedRowKeys.length }}个</p>
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.productSn"
            rowKeyName="productSn"
            :row-selection="{ columnWidth: 40 }"
            @rowSelection="rowSelectionFun"
            :data="loadData"
            :columns="columns"
            :scroll="{ y: 450 }"
            bordered>
          </s-table>
        </div>
        <!-- 按钮 -->
        <div class="btn-con">
          <a-button
            type="primary"
            id="outStock-save"
            size="large"
            class="button-primary"
            @click="handleSave"
            style="padding: 0 36px;">加入本次采购</a-button>
          <a-button
            id="outStock-cancel"
            size="large"
            class="button-cancel"
            @click="isShow=false"
            style="padding: 0 60px;margin-left: 15px;">取消</a-button>
        </div>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import { purchaseDetailCancelList, purchaseDetailCancelSave } from '@/api/purchaseDetail'
export default {
  name: 'OutStockModal',
  components: { STable },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    chooseData: {
      type: Array,
      default: () => {
        return []
      }
    },
    paramsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 20 }
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '产品编码', dataIndex: 'dealerProductEntity.code', width: '24%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'dealerProductEntity.name', width: '32%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '上次采购数量', dataIndex: 'qty', width: '18%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '上次缺货数量', dataIndex: 'cancelQty', width: '18%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'dealerProductEntity.unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return purchaseDetailCancelList(parameter).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      spinning: false,
      rowSelectionInfo: null
    }
  },
  methods: {
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    // 保存
    handleSave () {
      const _this = this
      if (!this.rowSelectionInfo || (this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys.length < 1)) {
        this.$message.warning('请在列表勾选后再进行操作！')
        return
      }
      const params = {
        confirm: false,
        purchaseBillSn: this.paramsData.purchaseBillSn,
        purchaseBillNo: this.paramsData.purchaseBillNo,
        purchaseBillDetailEntityList: this.rowSelectionInfo && this.rowSelectionInfo.selectedRows
      }
      purchaseDetailCancelSave(params).then(res => {
        if (res.status == 200 && res.data) {
          let content = ''
          if ((res.data.productSizeRepeat != res.data.productSize) && (res.data.productSizeRepeat != 0)) {
            content = `您已经选择 ${res.data.productSize} 个产品，其中 ${res.data.productSizeRepeat} 个产品已经存在于采购单中。本次只会加入不重复的产品，上次缺货数量将作为本次采购数量。确认加入本次采购吗？`
            this.confirmFun(content)
          } else if (res.data.productSizeRepeat == res.data.productSize) {
            content = `抱歉，您选择的 ${res.data.productSize} 个产品，采购单中已经全部存在，无需加入！`
            this.$info({
              title: '提示',
              content: content,
              okText: '好的',
              centered: true,
              onOk () {
                _this.$refs.table.clearSelected() // 清空表格选中项
              }
            })
          } else {
            content = `您已经选择 ${res.data.productSize} 个产品，上次缺货数量将作为本次采购数量。确认加入本次采购吗？`
            this.confirmFun(content)
          }
        }
      })
    },
    confirmFun (content) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: content,
        okText: '确认加入',
        cancelText: '重新选择',
        centered: true,
        closable: true,
        onOk () {
          _this.spinning = true
          const params = {
            confirm: true,
            purchaseBillSn: _this.paramsData.purchaseBillSn,
            purchaseBillNo: _this.paramsData.purchaseBillNo,
            purchaseBillDetailEntityList: _this.rowSelectionInfo && _this.rowSelectionInfo.selectedRows
          }
          purchaseDetailCancelSave(params).then(res => {
            if (res.status == 200) {
              _this.$emit('ok')
              _this.isShow = false
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        },
        onCancel () {
          _this.$refs.table.clearSelected() // 清空表格选中项
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.$refs.table.clearSelected() // 清空表格选中项
      }
    }
  }
}
</script>

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