<template>
  <a-modal
    centered
    class="choosePosPriceProducts-modal"
    :footer="null"
    :maskClosable="false"
    title="选择产品"
    v-model="isShow"
    @cancle="isShow=false"
    width="80%">
    <div class="table-operator">
      <a-button v-if="!(ruleType=='PROMO_PROD') && !((ruleType=='ADD_PRICE_PURCH') && (goodsType == 'promo'))" id="choosePosPriceProducts-brand" type="primary" class="button-error" @click="choosePModal('BRAND')">选择产品品牌</a-button>
      <a-button v-if="!(ruleType=='PROMO_PROD') && !((ruleType=='ADD_PRICE_PURCH') && (goodsType == 'promo'))" id="choosePosPriceProducts-type" type="primary" class="button-info" @click="choosePModal('CATEGORY')">选择产品分类</a-button>
      <a-button id="choosePosPriceProducts-product" type="primary" class="button-success" @click="choosePModal('PRODUCT')">选择产品</a-button>
    </div>
    <!-- 参与产品 -->
    <div class="products-con">
      <!-- 标题栏 -->
      <div class="products-header">
        <div class="header-con">
          <div class="products-tit">
            参与产品：
            <!-- <a-button id="choosePosPriceProducts-import" type="primary" class="button-info" @click="openGuideModal=true">导入产品</a-button> -->
          </div>
          <div class="products-operation" v-if="goodsType=='normal'">
            <a-row :gutter="15">
              <a-col :span="9">
                <v-select
                  code="ORDER_GOODS_UNIT"
                  id="choosePosPriceProducts-orderGoodsUnit"
                  v-model="orderGoodsUnit"
                  allowClear
                  placeholder="请选择"
                  style="width: 100%;"
                ></v-select>
              </a-col>
              <a-col :span="10">
                <a-input-number
                  v-if="orderGoodsUnit=='PROD_UNIT'"
                  id="choosePosPriceProducts-orderGoodsQty"
                  v-model="orderGoodsQty"
                  :min="1"
                  :max="999999"
                  :precision="0"
                  style="width: 85%;margin-right: 5px;"
                  placeholder="请输入限制数量(1~999999)"
                  allowClear />
              </a-col>
              <a-col :span="5">
                <a-button id="choosePosPriceProducts-import" type="primary" class="button-info" @click="handlerBulkCopy">批量复制</a-button>
              </a-col>
            </a-row>
          </div>
        </div>
        <!-- <p><a-icon type="exclamation-circle" style="color: #f90;margin-right: 5px;" />提示:导入时会清空下方已选内容，只展示导出成功的产品，请谨慎使用。</p> -->
      </div>
      <!-- 表格 -->
      <a-table
        class="sTable"
        ref="table"
        size="small"
        :rowKey="(record, index) => index"
        :columns="nowColumns"
        :dataSource="loadData"
        :scroll="{ x: goodsType == 'normal' ? 1380 : null, y: 500 }"
        :pagination="false"
        bordered>
        <!-- 序号 -->
        <template slot="no" slot-scope="text, record, index">
          {{ index+1 }}
        </template>
        <!-- 起订量限制 -->
        <template slot="orderGoodsUnit" slot-scope="text, record">
          <v-select
            code="ORDER_GOODS_UNIT"
            id="choosePosPriceProducts-orderGoodsUnit"
            v-model="record.orderGoodsUnit"
            allowClear
            placeholder="请选择"
            style="width: 40%;"
          ></v-select>
          <a-input-number
            v-if="record.orderGoodsUnit=='PROD_UNIT'"
            id="choosePosPriceProducts-orderGoodsQty"
            v-model="record.orderGoodsQty"
            :precision="0"
            :min="1"
            :max="999999"
            placeholder="请输入限制数量(1~999999)"
            style="width: 50%;margin-left: 5px;" />
        </template>
        <!-- 特惠单价 / 换购单价 -->
        <template slot="goodsPrice" slot-scope="text, record">
          <a-input-number
            id="choosePosPriceProducts-goodsPrice"
            v-model="record.goodsPrice"
            :precision="2"
            :min="0.1"
            :max="999999"
            placeholder="请输入限制数量(0.1~999999)"
            style="width: 100%;" />
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record, index">
          <a-button size="small" type="primary" class="button-error" @click="handleDel(record, index)" id="choosePosPriceProducts-del-btn">删除</a-button>
        </template>
      </a-table>
      <!-- 按钮 -->
      <div class="btn-con">
        <a-button
          type="primary"
          id="choosePosPriceProducts-submit"
          size="large"
          class="button-primary"
          @click="handleSave"
          style="padding: 0 60px;">保存</a-button>
        <a-button
          id="choosePosPriceProducts-cancel"
          size="large"
          class="button-cancel"
          @click="isShow=false"
          style="padding: 0 60px;margin-left: 15px;">取消</a-button>
      </div>
    </div>
    <!-- 选择产品品牌 -->
    <chooseBrandModal :openModal="openBrandModal" :chooseData="chooseBrand" @close="openBrandModal=false" @ok="handleBrandOk" />
    <!-- 选择产品分类 -->
    <chooseTypeModal :openModal="openTypeModal" :chooseData="chooseType" @close="openTypeModal=false" @ok="handleTypeOk" />
    <!-- 选择产品 -->
    <chooseProductsModal :openModal="openProductsModal" :chooseData="chooseProducts" @close="openProductsModal=false" @ok="handleProductsOk" />
    <!-- 导入产品 -->
    <importGuideModal :openModal="openGuideModal" @close="openGuideModal=false" />
  </a-modal>
</template>

<script>
import ChooseProductsModal from '@/views/common/chooseProductsModal.vue'
import ChooseBrandModal from '@/views/common/chooseBrandModal.vue'
import ChooseTypeModal from '@/views/common/chooseTypeModal.vue'
import ImportGuideModal from './importGuideModal.vue'
import { VSelect } from '@/components'
export default {
  name: 'ChoosePriceProductsModal',
  components: { VSelect, ChooseProductsModal, ChooseBrandModal, ChooseTypeModal, ImportGuideModal },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    ruleType: { //  买产品送产品...
      type: String,
      default: ''
    },
    goodsType: { //  正价/促销
      type: String,
      default: ''
    },
    nowData: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      orderGoodsUnit: undefined, //  起订量限制 批量
      orderGoodsQty: '', //  限制数量
      // 表头
      columns: [ //  正品
        { title: '序号', scopedSlots: { customRender: 'no' }, width: 80, align: 'center' },
        { title: '类型', dataIndex: 'goodsTypeName', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类型名称', dataIndex: 'goodsName', width: 220, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品名称', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'goodsCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '起订量限制', scopedSlots: { customRender: 'orderGoodsUnit' }, width: 400, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      promoColumns: [ //  促销品
        { title: '序号', scopedSlots: { customRender: 'no' }, width: 80, align: 'center' },
        { title: '类型', dataIndex: 'goodsTypeName', width: 140, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '类型名称', dataIndex: 'goodsName', width: 220, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品名称', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'goodsCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      promoSpecialColumns: [ //  特价产品/加价促销品
        { title: '序号', scopedSlots: { customRender: 'no' }, width: 80, align: 'center' },
        { title: '产品名称', dataIndex: 'name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '产品编码', dataIndex: 'goodsCode', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: this.ruleType == 'PROMO_PROD' ? '特惠单价(￥)' : '换购单价(￥)', scopedSlots: { customRender: 'goodsPrice' }, width: 200, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 100, align: 'center', fixed: 'right' }
      ],
      loadData: [],
      openBrandModal: false, //  选择产品品牌
      openTypeModal: false, //  选择产品分类
      openProductsModal: false, //  选择产品
      openGuideModal: false, //  导入产品引导
      chooseBrand: [], //  所选品牌
      chooseProducts: [], //  所选产品
      chooseType: [] //  所选分类
    }
  },
  computed: {
    nowColumns () {
      let columns = this.columns
      if ((this.ruleType == 'BUY_PROD_GIVE_PROD') || (this.ruleType == 'BUY_PROD_GIVE_MONEY')) { //  买产品送产品/买产品送采购额
        if (this.goodsType == 'normal') { //  正品
          columns = this.columns
        } else if (this.goodsType == 'promo') { //  促销
          columns = this.promoColumns
        }
      } else if (this.ruleType == 'PROMO_PROD') { //  特价产品
        if (this.goodsType == 'promo') { //  促销
          columns = this.promoSpecialColumns
        }
      } else if (this.ruleType == 'ADD_PRICE_PURCH') { //  加价换购
        if (this.goodsType == 'normal') { //  正品
          columns = this.columns
        } else if (this.goodsType == 'promo') { //  促销
          columns = this.promoSpecialColumns
        }
      }
      return columns
    }
  },
  methods: {
    // 选择产品
    choosePModal (type) {
      const productList = []
      const brandList = []
      const typeList = []
      this.loadData.map(item => {
        if (item.goodsType == 'PRODUCT') {
          productList.push(item)
        } else if (item.goodsType == 'BRAND') {
          brandList.push(item)
        } else if (item.goodsType == 'CATEGORY') {
          typeList.push(item)
        }
      })
      if (type == 'PRODUCT') { //  选择产品
        this.chooseProducts = productList
        this.openProductsModal = true
      } else if (type == 'BRAND') { //  选择品牌
        this.chooseBrand = brandList
        this.openBrandModal = true
      } else if (type == 'CATEGORY') { //  选择分类
        this.chooseType = typeList
        this.openTypeModal = true
      }
    },
    // 品牌
    handleBrandOk (obj) {
      this.chooseBrand = obj
      this.changeData()
    },
    // 产品
    handleProductsOk (obj) {
      this.chooseProducts = obj
      this.changeData()
    },
    // 分类
    handleTypeOk (obj) {
      this.chooseType = obj
      this.changeData()
    },
    //  保存
    handleSave () {
      const _this = this
      const arr = []
      if (this.goodsType == 'normal') { //  正品
        _this.loadData.map((item, index) => {
          if ((item.orderGoodsUnit == 'PROD_UNIT') && (!item.orderGoodsQty)) {
            arr.push(index)
          }
        })
        if (arr.length > 0) {
          this.$message.warning('请完善起订量限制后再保存！')
          return
        }
      } else if (this.goodsType == 'promo') { //  促销
        let num = 0
        if ((this.ruleType == 'PROMO_PROD') || (this.ruleType == 'ADD_PRICE_PURCH')) { //  特价产品 / 加价换购
          _this.loadData.map((item, index) => {
            if (!item.goodsPrice) {
              num++
            }
          })
          if (num > 0) {
            this.$message.warning('存在未填写的内容，请填写完整后再提交！')
            return
          }
        }
      }
      this.$emit('ok', this.loadData)
      this.isShow = false
    },
    changeData () {
      const _this = this
      _this.loadData = []
      if (_this.chooseBrand.length > 0) {
        _this.chooseBrand.map(item => {
          const obj = {
            goodsType: 'BRAND',
            goodsTypeName: '品牌',
            goodsName: item.brandName,
            goodsSn: item.brandSn,
            orderGoodsUnit: undefined,
            orderGoodsQty: ''
          }
          delete item.id
          _this.loadData.push(Object.assign(obj, item))
        })
      }
      if (_this.chooseType.length > 0) {
        _this.chooseType.map(item => {
          const obj = {
            goodsType: 'CATEGORY',
            goodsTypeName: '分类',
            goodsName: item.productTypeName,
            goodsSn: item.productTypeSn,
            orderGoodsUnit: undefined,
            orderGoodsQty: ''
          }
          delete item.id
          _this.loadData.push(Object.assign(obj, item))
        })
      }
      if (_this.chooseProducts.length > 0) {
        _this.chooseProducts.map(item => {
          const obj = {
            goodsType: 'PRODUCT',
            goodsTypeName: '产品',
            goodsName: item.name,
            name: item.name,
            goodsCode: item.code,
            goodsSn: item.productSn,
            orderGoodsUnit: undefined,
            orderGoodsQty: ''
          }
          delete item.id
          _this.loadData.push(Object.assign(obj, item))
        })
      }
    },
    // 批量复制
    handlerBulkCopy () {
      const _this = this
      if ((!this.orderGoodsUnit) || ((this.orderGoodsUnit == 'PROD_UNIT') && !this.orderGoodsQty)) {
        this.$message.warning('请选择需要批量复制的选项值后再操作！')
        return
      }
      if (this.loadData.length == 0) {
        this.$message.warning('请先添加产品后再批量复制起订限制规则！')
        return
      }
      this.$confirm({
        title: '提示',
        content: '批量复制会覆盖原单独设置的规则，确定要批量复制吗？',
        centered: true,
        onOk () {
          _this.loadData.map(item => {
            item.orderGoodsUnit = _this.orderGoodsUnit
            item.orderGoodsQty = _this.orderGoodsQty
          })
        }
      })
    },
    // 删除
    handleDel (record, ind) {
      this.loadData.splice(ind, 1)
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.chooseBrand = []
        this.chooseProducts = []
        this.chooseType = []
        this.orderGoodsUnit = undefined //  起订量限制 批量
        this.orderGoodsQty = '' //  限制数量
        this.$emit('close')
      }
    },
    nowData: {
      handler (newValue, oldValue) {
        if (this.isShow && newValue) {
          this.loadData = newValue
        }
      },
      deep: true
    }
  }
}
</script>

<style lang="less" scoped>
  .choosePosPriceProducts-modal{
    // 参与产品
    .products-con{
      .products-header{
        padding: 10px 15px;
        color: rgba(0,0,0,.85);
        background-color: #fafafa;
        border-radius: 6px;
        margin: 10px 0;
        .header-con{
          display: flex;
          justify-content: space-between;
          align-items: center;
          .products-operation{
            width: 50%;
          }
        }
        p{
          margin: 8px 0 0;
          color: #909399;
          font-size: 12px;
        }
      }
      .btn-con{
        text-align: center;
        margin: 30px 0 20px;
        .button-cancel{
          font-size: 12px;
        }
      }
    }
  }
</style>
