<template>
  <a-modal
    centered
    class="chooseShelf-modal"
    :footer="null"
    :maskClosable="false"
    :title="mtitle"
    v-model="isShow"
    @cancel="isShow = false"
    width="80%">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-tabs type="card">
        <a-tab-pane :defaultActiveKey="curTab" v-model="curTab" :key="1" tab="待补货产品">
          <!-- 搜索条件 -->
          <div class="table-page-search-wrapper">
            <a-form-model
              ref="ruleForm"
              class="form-model-con"
              layout="inline"
              :model="queryParam"
              @keyup.enter.native="$refs.table.refresh(true)" >
              <a-row :gutter="15">
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品编码">
                    <a-input id="chooseShelf-code" ref="searchProductCode" v-model.trim="queryParam.productCode" allowClear placeholder="请输入产品编码"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="24">
                  <a-form-model-item label="产品名称">
                    <a-input id="chooseShelf-name" v-model.trim="queryParam.productName" allowClear placeholder="请输入产品名称"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="产品分类">
                    <ProductType id="chooseShelf-productType" :isDealer="true" @change="changeProductType" v-model="productType"></ProductType>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="库存情况">
                    <a-checkbox-group v-model.trim="queryParam.stockStateList" :options="options" />
                  </a-form-model-item>
                </a-col>
                <a-col :md="4" :sm="12" style="margin-bottom: 10px;">
                  <a-button type="primary" @click="$refs.table.refresh(true)" id="chooseShelf-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm" id="chooseShelf-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </div>
          <div class="chooseShelf-modal-table">
            <div class="table-operator">
              <div>
                <a-button type="primary" @click="handlePlAdd">批量添加</a-button>
                <span style="margin-left: 10px;" v-if="selectTotal">已选{{ selectTotal }}项</span>
              </div>
              <div>
                <a-icon type="info-circle" />
                此界面仅显示货架上需要补货的产品
              </div>
            </div>
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table"
              size="small"
              :rowKey="(record) => record.id"
              :row-selection="{columnWidth: 40, getCheckboxProps: record => ({ props: { disabled: record.includeFlag == 1 } })}"
              @rowSelection="rowSelectionFun"
              :columns="columns"
              :data="loadData"
              :style="{ height: '500px' }"
              :scroll="{ y: 450 }"
              :showPagination="false"
              bordered>
              <!-- 售价 -->
              <template slot="salePrice" slot-scope="text, record">
                <div style="display: flex;align-items: center;" @dblclick.stop>
                  <a-input-number
                    size="small"
                    v-model="record.price"
                    :precision="2"
                    :min="0"
                    :max="999999"
                    style="width: 100%;"
                    placeholder="请输入" />
                  <span style="color: red;margin: 0 2px;font-size: 14px;" v-if="record.origSalePriceFlag == 1">原</span>
                  <span v-else>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
                </div>
              </template>
              <!-- 销售数量 -->
              <template slot="nums" slot-scope="text, record">
                <div @dblclick.stop>
                  <a-input-number
                    size="small"
                    v-model="record.salesNums"
                    :precision="0"
                    :min="0"
                    :max="999999"
                    style="width: 100%;"
                    placeholder="请输入" />
                </div>
              </template>
              <!-- 操作 -->
              <template slot="action" slot-scope="text, record">
                <a-button
                  size="small"
                  type="link"
                  class="button-primary"
                  @click.stop="handleAdd(record)"
                  v-if="record.includeFlag != 1"
                  id="productInfoList-edit-btn">添加</a-button>
                <span style="color:#999;" v-else>已添加</span>
                <a-button
                  size="small"
                  type="link"
                  class="button-primary"
                  @click.stop="handleDetail(record)"
                  id="productInfoList-detail-btn"
                  style="margin-left: 5px;">销售记录</a-button>
              </template>
            </s-table>
          </div>
        </a-tab-pane>
        <a-tab-pane :key="2" tab="全部货架产品">
          <!-- 搜索条件 -->
          <div class="table-page-search-wrapper">
            <a-form-model
              ref="ruleForm1"
              class="form-model-con"
              layout="inline"
              :model="queryParam1"
              @keyup.enter.native="$refs.table1.refresh(true)" >
              <a-row :gutter="15">
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="产品编码">
                    <a-input id="chooseShelf-code1" v-model.trim="queryParam1.productCode" allowClear placeholder="请输入产品编码"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="产品名称">
                    <a-input id="chooseShelf-name1" v-model.trim="queryParam1.productName" allowClear placeholder="请输入产品名称"/>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="24">
                  <a-form-model-item label="产品分类">
                    <ProductType id="chooseShelf-productType1" :isDealer="true" @change="changeProductType1" v-model="productType1"></ProductType>
                  </a-form-model-item>
                </a-col>
                <a-col :md="6" :sm="12" style="margin-bottom: 10px;">
                  <a-button type="primary" @click="$refs.table1.refresh(true)" id="chooseShelf-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm1" id="chooseShelf-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form-model>
          </div>
          <div class="chooseShelf-modal-table">
            <!-- 列表 -->
            <s-table
              class="sTable"
              ref="table1"
              size="small"
              :rowKey="(record) => record.id"
              :columns="columns1"
              :data="loadData1"
              :style="{ height: '500px' }"
              :scroll="{ y: 450 }"
              :showPagination="false"
              bordered>
            </s-table>
          </div>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
    <!-- 销售记录 -->
    <product-salesRecord-modal ref="salseRecord" :openModal="showRecord" @close="showRecord=false" />
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable } from '@/components'
import ProductType from '../../common/productType.js'
import productSalesRecordModal from './productSalesRecordModal.vue'
import { queryWaitReplenish, queryShelfProduct } from '@/api/shelfReplenish'
export default {
  name: 'OutinDetailModal',
  components: { STable, ProductType, productSalesRecordModal },
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    baseData: {
      type: Object,
      default: function () {
        return null
      }
    }
  },
  data () {
    return {
      spinning: false,
      advanced: false,
      showRecord: false,
      isShow: this.openModal, //  是否打开弹框
      options: [
        { label: '库存充足', value: 'stockEnough' },
        { label: '部分缺货', value: 'portionStockout' },
        { label: '全部缺货', value: 'allStockout' }
      ],
      curTab: 1,
      productType: [],
      queryParam: {
        productCode: '', //  产品编码
        productName: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '', //  产品三级分类
        stockStateList: []
      },
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        this.queryParam.shelfSn = this.baseData.shelfSn
        this.queryParam.customerSn = this.baseData.customerSn
        this.queryParam.salePriceType = this.baseData.salePriceType
        this.queryParam.salesBillSn = this.baseData.salesBillSn

        return queryWaitReplenish(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i] = data[i].shelfProductApi
              data[i].no = no + i + 1
              data[i].salesNums = data[i].replenishBillWaitQty
              data[i].putCost = data[i].cost
            }
          }
          this.spinning = false
          return data
        })
      },
      rowSelectionInfo: null,
      // -------- 全部产品 --------
      productType1: [],
      queryParam1: {
        productCode: '', //  产品编码
        productName: '', //  产品名称
        productTypeSn1: '', //  产品一级分类
        productTypeSn2: '', //  产品二级分类
        productTypeSn3: '' //  产品三级分类
      },
      // 加载数据方法 必须为 Promise 对象
      loadData1: parameter => {
        this.spinning = true
        this.queryParam1.shelfSn = this.baseData.shelfSn
        this.queryParam1.customerSn = this.baseData.customerSn
        this.queryParam1.salePriceType = this.baseData.salePriceType
        this.queryParam1.salesBillSn = this.baseData.salesBillSn
        return queryShelfProduct(Object.assign(parameter, this.queryParam1)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = 0
            for (var i = 0; i < data.length; i++) {
              data[i].no = no + i + 1
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  computed: {
    mtitle () {
      return '货架产品' + (this.baseData && this.baseData.shelfName ? this.baseData.shelfName : '')
    },
    selectTotal () {
      return this.rowSelectionInfo && this.rowSelectionInfo.selectedRows.length
    },
    columns () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '6%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '15%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '最大库容', dataIndex: 'maxQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '货架库存', dataIndex: 'qty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '补货在途', dataIndex: 'replenishBillQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '调回在途', dataIndex: 'recallBillQty', width: '6%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '待补货数量', dataIndex: 'replenishBillWaitQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '门店库存数量', dataIndex: 'qplsStockQty', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: '4%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '售价', dataIndex: 'price', scopedSlots: { customRender: 'salePrice' }, width: '8%', align: 'center' },
        { title: '销售数量', dataIndex: 'salesNums', scopedSlots: { customRender: 'nums' }, width: '8%', align: 'center' },
        { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
      return arr
    },
    columns1 () {
      const arr = [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '货位号', dataIndex: 'shelfPlaceCode', width: '5%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品编码', dataIndex: 'productCode', width: '16%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'productName', width: '25%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '最大库容', dataIndex: 'maxQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '货架库存', dataIndex: 'qty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '补货在途', dataIndex: 'replenishBillQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '调回在途', dataIndex: 'recallBillQty', width: '10%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '单位', dataIndex: 'productUnit', width: '10%', align: 'center', customRender: function (text) { return text || '--' } }
      ]
      return arr
    }
  },
  methods: {
    cansel () {
      this.isShow = false
    },
    // 销售记录
    handleDetail (row) {
      this.showRecord = true
      this.$refs.salseRecord.getDetail(this.baseData.customerSn, row.productSn, row)
    },
    // 表格选中项
    rowSelectionFun (obj) {
      this.rowSelectionInfo = obj || null
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.productTypeSn3 = val[2] ? val[2] : ''
    },
    // 添加
    handleAdd (row) {
      const clist = this.rowSelectionInfo && this.rowSelectionInfo.selectedRows || []
      const blist = this.rowSelectionInfo && this.rowSelectionInfo.selectedRowKeys || []
      const i = clist.findIndex(item => item.id == row.id)
      const k = blist.findIndex(item => item == row.id)
      console.log(i, k)
      if (i >= 0) {
        clist.splice(i, 1)
      }
      if (k >= 0) {
        blist.splice(k, 1)
      }
      this.$emit('add', row)
    },
    // 批量添加
    handlePlAdd () {
      if (this.selectTotal) {
        this.$emit('plAdd', this.rowSelectionInfo && this.rowSelectionInfo.selectedRows || [])
      } else {
        this.$message.info('请选择产品')
      }
    },
    resetSearchForm () {
      this.queryParam.productCode = ''
      this.queryParam.productName = ''
      this.queryParam.productTypeSn1 = ''
      this.queryParam.productTypeSn2 = ''
      this.queryParam.productTypeSn3 = ''
      this.productType = []
      this.queryParam.stockStateList = []
      this.$refs.ruleForm.resetFields()
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(true)
    },
    reload () {
      // this.$refs.table.clearSelected()
      this.$refs.table.refresh()
    },
    // ---------- 全部产品----------
    //  产品分类  change
    changeProductType1 (val, opt) {
      this.queryParam1.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam1.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam1.productTypeSn3 = val[2] ? val[2] : ''
    },
    resetSearchForm1 () {
      this.queryParam1.productCode = ''
      this.queryParam1.productName = ''
      this.queryParam1.productTypeSn1 = ''
      this.queryParam1.productTypeSn2 = ''
      this.queryParam1.productTypeSn3 = ''
      this.productType1 = []
      this.$refs.ruleForm1.resetFields()
      this.$refs.table1.refresh(true)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.$nextTick(() => {
          this.curTab = 1
        })
      }
    }
  }
}
</script>

<style lang="less">
.chooseShelf-modal {
	.ant-modal-body {
		padding: 0;
    .chooseShelf-modal-table{
      padding: 0 20px 10px;
      .table-operator{
        padding:0 10px 10px 0;
        display:flex;
        justify-content: space-between;
        align-items: center;
      }
    }
    .table-page-search-wrapper{
      padding: 5px 20px 0;
    }
	}
}
</style>
