<template>
  <a-modal
    centered
    class="replenishmentManagement-confirm-modal"
    :footer="null"
    :maskClosable="false"
    :title="modalTit"
    v-model="isShow"
    @cancel="isShow = false"
    :width="800">
    <div v-if="nowData">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 基础信息 -->
        <div v-if="detailData">
          <a-descriptions size="small" :column="3">
            <a-descriptions-item label="补货单号">{{ detailData && detailData.replenishBillNo || '--' }}</a-descriptions-item>
            <a-descriptions-item label="创建时间">{{ detailData && detailData.createDate || '--' }}</a-descriptions-item>
            <a-descriptions-item label="状态">{{ detailData&&detailData.billStateDictValue || '--' }}</a-descriptions-item>
            <a-descriptions-item label="出库时间">{{ detailData&&detailData.outStockTime || '--' }}</a-descriptions-item>
            <a-descriptions-item label="签收时间">{{ detailData&&detailData.putStockTime || '--' }}</a-descriptions-item>
            <a-descriptions-item label="出库备注">
              <div style="word-break: break-all;">
                {{ detailData&&detailData.remarks || '--' }}
              </div>
            </a-descriptions-item>
            <a-descriptions-item label="关联销售单" :span="3" v-if="detailData.billState=='WAIT_CHECK' || detailData.billState=='WAIT_OUT_STOCK' || detailData.billState=='FINISH'">
              <div style="word-break: break-all;">
                <a
                  style="margin-right:10px;color:#00aaff;"
                  v-for="item in salesOrderList"
                  :key="item.id"
                  @click="viewSales(item)">
                  {{ item.salesBillNo }};
                </a>
                <a-button v-if="totalNums" type="primary" class="button-info" @click="showPlModal=true">生成销售单</a-button>
                <span v-else>--</span>
              </div>
            </a-descriptions-item>
          </a-descriptions>
        </div>
        <div class="pages-wrap" style="margin-top:10px;">
          <!-- 列表 -->
          <s-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
            :scroll="{ y: 400 }"
            :showPagination="false"
            bordered>
          </s-table>
        </div>
      </a-spin>
    </div>
    <div class="btn-cont">
      <a-button @click="isShow = false">关闭</a-button>
    </div>
    <!-- 生成销售单 -->
    <commonModal modalTit="" :openModal="showPlModal" @close="showPlModal=false" @ok="createSales">
      <div style="display: flex;justify-content: center;margin-bottom: 15px;">
        <div style="padding:5px 20px;">产品款数：<span style="color:red;">{{ listData&&listData.length }}</span></div>
        <div style="padding:5px 20px;">产品总件数：<span style="color:red;">{{ totalNums }}</span></div>
      </div>
      <div style="text-align: center;" v-if="salesOrderList.length">
        <div style="margin-bottom: 5px;font-size: 14px;">此补货单已经存在对应的销售单</div>
        <div style="margin-bottom: 15px;font-size: 14px;"><strong>确认再次生成销售单吗？</strong></div>
        <div style="line-height: 24px;">
          <div
            style="padding:5px;border-bottom: 1px solid #eee;color:#00aaff;cursor: pointer;display: flex;justify-content: space-between;align-items: center;"
            v-for="item in salesOrderList"
            :key="item.id"
            @click="viewSales(item)"
          >
            <div class="flex-grow:1;padding:0 5px;">{{ item.salesBillNo }};</div>
            <div class="padding:0 5px;"><a-icon type="right" style="color:#00aaff;" /></div>
          </div>
        </div>
      </div>
      <div style="text-align: center;" v-else>
        <div style="margin-bottom: 15px;font-size: 14px;"><strong>确认生成销售单吗？</strong></div>
      </div>
    </commonModal>
    <!-- 销售单详情 -->
    <a-modal
      centered
      class="outboundOrderDetail-modal"
      :footer="null"
      :maskClosable="false"
      :bodyStyle="{padding:'12px'}"
      :zIndex="1001"
      title="销售单详情"
      v-model="openDetailModal"
      @cancel="openDetailModal=false"
      width="80%">
      <div style="max-height: 700px;overflow-y: auto;">
        <salesDetailModal v-if="openDetailModal" :outBizSn="orderSn" />
      </div>
    </a-modal>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import commonModal from '@/views/common/commonModal.vue'
import salesDetailModal from '@/views/salesManagement/salesQuery/detail.vue'
import { salesDetailBySn } from '@/api/sales'
import { shelfReplenishDetailList, shelfReplenishDetail, queryRelationSalesBill, createSalesBill } from '@/api/shelfReplenish'
export default {
  name: 'DetailModal',
  components: { STable, VSelect, commonModal, salesDetailModal },
  mixins: [commonMixin],
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    nowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      disabled: false,
      showPlModal: false,
      openDetailModal: false,
      orderSn: null,
      listData: [],
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '22%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', width: '28%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '补货数量', dataIndex: 'qty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '签收数量', dataIndex: 'putQty', width: '12%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'product.unit', width: '8%', align: 'center', customRender: function (text) { return text || '--' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfReplenishDetailList({ replenishBillSn: this.nowData.replenishBillSn }).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            for (var i = 0; i < data.length; i++) {
              data[i].no = i + 1
            }
            this.disabled = false
            this.listData = data || []
          }
          this.spinning = false
          return data
        })
      },
      localDataSource: [],
      detailData: null, //  详情数据
      salesOrderList: []
    }
  },
  computed: {
    modalTit () {
      const hjName = this.nowData && this.nowData.shelfInfo.shelfName ? this.nowData.shelfInfo.shelfName : ''
      return '补货单详情 —— ' + hjName
    },
    totalNums () {
      let ret = 0
      const isFin = this.detailData && this.detailData.billState == 'FINISH'
      this.listData.map(item => {
        ret = ret + (isFin ? item.putQty : item.qty)
      })
      return ret
    }
  },
  methods: {
    // 基本信息
    getDetail () {
      shelfReplenishDetail({ sn: this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
        } else {
          this.detailData = null
        }
      })
    },
    // 查询关联销售单
    getSalesOrder () {
      queryRelationSalesBill({ replenishBillSn: this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.salesOrderList = res.data
        } else {
          this.salesOrderList = []
        }
      })
    },
    // 生成销售单
    createSales () {
      this.spinning = true
      this.showPlModal = false
      createSalesBill({ replenishBillSn: this.nowData.replenishBillSn }).then(res => {
        if (res.status == 200) {
          this.$message.info(res.message)
          this.getSalesOrder()
        }
        this.spinning = false
      })
    },
    // 查看销售单详情
    viewSales (item) {
      salesDetailBySn({ salesBillSn: item.salesBillSn }).then(res => {
        if (res.data) {
          this.orderSn = item.salesBillSn
          this.openDetailModal = true
        } else {
          this.$message.info('此销售单已被删除！')
        }
      })
    },
    pageInit () {
      const vm = this
      vm.$nextTick(() => {
        vm.spinning = false
        vm.disabled = false
        vm.getSalesOrder()
        vm.getDetail()
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.pageInit()
      }
    }
  }
}
</script>

<style lang="less">
.replenishmentManagement-confirm-modal {
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
}
</style>
