<template>
  <a-modal
    centered
    class="satelliteWarehouseInventory-modal"
    :footer="null"
    :maskClosable="false"
    title="补货清单"
    v-model="isShow"
    @cancle="isShow=false"
    :width="960">
    <!-- 补货清单 -->
    <div>
      <h4 style="font-weight: bold;font-size: 16px;">{{ (nowData&&nowData.customerName) || '--' }}</h4>
      <s-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1260 }"
        :defaultLoadData="false"
        bordered>
        <!-- 补货数量 -->
        <template slot="productQty" slot-scope="text, record">
          <a-button
            type="primary"
            @click="handleNumSubtract(record)"
            shape="circle"
            icon="minus"
            size="small"
            id="satelliteWarehouseInventoryList-num-add-btn"></a-button>
          <a-input-number
            v-model="record.productQty"
            :min="0"
            :max="999999"
            :precision="0"
            :step="1"
            @change="e => onChange(e, record)"
            class="stepper" />
          <a-button
            type="primary"
            @click="handleNumAdd(record)"
            shape="circle"
            icon="plus"
            size="small"
            id="satelliteWarehouseInventoryList-num-subtract-btn"></a-button>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button type="link" @click="handleRemove(record)" id="satelliteWarehouseInventoryList-remove-btn">移除</a-button>
        </template>
      </s-table>
      <div class="btn-cont">
        <a-button type="primary" id="satellite-warehouse-inventory-add-modal-add" @click="handleAdd">创建销售单</a-button>
        <a-button id="satellite-warehouse-inventory-add-modal-back" @click="isShow = false" style="margin-left: 15px;">返回列表</a-button>
      </div>
    </div>
  </a-modal>
</template>

<script>
import { STable } from '@/components'
import { satelliteWHSupplyList, satelliteWHSupplyUpdate, satelliteWHSupplyDel, satelliteWHSupplyValidateStock, satelliteWHSupplyAddSales } from '@/api/satelliteWH'
export default {
  name: 'SatelliteWarehouseInventoryAddModal',
  components: { STable },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: null
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '客户名称', dataIndex: 'customer.customerName', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '仓库名称', dataIndex: 'productBrand', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '配件名称', dataIndex: 'dealerProduct.name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'dealerProduct.code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品原厂编码', dataIndex: 'dealerProduct.origCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '补货数量', scopedSlots: { customRender: 'productQty' }, width: 200, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return satelliteWHSupplyList(Object.assign(parameter, { customerSn: this.nowData.customerSn })).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
          }
          return data
        })
      },
      val: 1
    }
  },
  methods: {
    //  创建销售单
    handleAdd () {
      const _this = this
      // 校验库存
      satelliteWHSupplyValidateStock({ sn: this.nowData.customerSn }).then(res => {
        if (res.status == 200) {
          if (res.data.length == 0) {
            // 创建销售单
            _this.createSalesFun()
          } else {
            const str = res.data.join('、')
            _this.$confirm({
              title: '提示',
              content: '产品编码为：【' + str + '】的产品库存不足！是否提交？',
              centered: true,
              onOk () {
                _this.createSalesFun()
              }
            })
          }
        }
      })
    },
    // 创建销售单
    createSalesFun () {
      satelliteWHSupplyAddSales({ sn: this.nowData.customerSn }).then(res => {
        if (res.status == 200) {
          this.$router.push({ name: 'salesEdit', params: { id: res.data.id, sn: res.data.salesBillSn, priceType: res.data.priceType } })
        }
      })
    },
    //  补货数量  加
    handleNumAdd (row) {
      row.productQty < 999999 ? this.handleQty(++row.productQty, row) : row.productQty
    },
    //  补货数量  减
    handleNumSubtract (row) {
      row.productQty > 1 ? this.handleQty(--row.productQty, row) : this.handleRemove(row)
    },
    //  删除配件信息
    handleRemove (row) {
      satelliteWHSupplyDel({ sn: row.satelliteWarehouseSupplySn }).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh(true)
        }
      })
    },
    //  补货数量  change
    onChange (val, row) {
      val > 0 ? this.handleQty(val, row) : this.handleRemove(row)
    },
    handleQty (val, row) {
      const params = {
        satelliteWarehouseSupplySn: row.satelliteWarehouseSupplySn,
        productSn: row.productSn,
        productQty: val
      }
      satelliteWHSupplyUpdate(params).then(res => {
        if (res.status == 200) {
          this.$refs.table.refresh(true)
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        const _this = this
        setTimeout(() => {
          _this.$refs.table.refresh(true)
        }, 100)
      }
    }
  }
}
</script>

<style lang="less">
  .satelliteWarehouseInventory-modal{
    .sTable{
      margin-top: 20px;
    }
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .item-cont {
      margin-bottom: 15px;
      display: flex;
      .item-label {
        width: 115px;
        font-size: 14px;
        color: rgba(0, 0, 0, 0.85);
        flex-shrink: 0;
      }
      .item-main {
        flex-grow: 1;
        word-break: break-all;
      }
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .stepper{
      margin: 0 10px;
      input{
        text-align: center;
      }
    }
  }
</style>
