<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :width="total?'450px':'300px'"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 批量操作 -->
      <div style="padding: 0 30px;" v-if="salesBillDetailSnList.length">
        <div>
          <span>产品范围：</span>
          <aRadioGroup v-model="productType">
            <aRadio :style="radioStyle0" value="0">选中产品</aRadio>
            <aRadio :style="radioStyle0" value="1">全部产品</aRadio>
          </aRadioGroup>
        </div>
        <div>
          <span>规则范围：</span>
          <aRadioGroup v-model="ruleType" @change="(e)=>{upActiveValArr=[];upActiveVal=null}">
            <aRadio :style="radioStyle0" value="0">选中规则</aRadio>
            <aRadio :style="radioStyle0" value="1">全部规则(优先叠加规则)</aRadio>
          </aRadioGroup>
        </div>
      </div>
      <div v-if="ruleType==0">
        <div style="padding: 10px 30px;" v-if="total">
          <!-- 非叠加活动 -->
          <div>
            <aRadioGroup v-model="upActiveVal" @change="(e)=>{upActiveValArr=[]}">
              <aRadio :style="radioStyle" value="-1" v-if="pageType">
                不参加促销
              </aRadio>
              <div v-if="noStackActiveList&&noStackActiveList.length">
                <strong >非叠加活动</strong>
              </div>
              <aRadio
                :style="radioStyle"
                v-for="(item,sindex) in noStackActiveList"
                :key="item.id"
                :value="item.salesPromoSn">
                <div style="text-wrap: auto;display:inline-block;">
                  {{ sindex+1 }}、{{ item.promotionRule.promotionRuleTypeDictValue }} — {{ item.promotion.title }}（{{ item.promotionRule.description }}）
                </div>
              </aRadio>
            </aRadioGroup>
          </div>
          <!-- 叠加活动 -->
          <div v-if="stackActiveList&&stackActiveList.length">
            <div>
              <strong >叠加活动</strong>
            </div>
            <a-checkbox-group v-model="upActiveValArr" @change="(e)=>{upActiveVal=null}">
              <a-checkbox
                :style="radioStyle"
                v-for="(item,sindex) in stackActiveList"
                :value="item.salesPromoSn"
                :key="item.id">
                <div style="text-wrap: auto;display:inline-block;">
                  {{ sindex+1 }}、{{ item.promotionRule.promotionRuleTypeDictValue }} — {{ item.promotion.title }}（{{ item.promotionRule.description }}）
                </div>
              </a-checkbox>
            </a-checkbox-group>
          </div>
        </div>
        <div style="padding: 10px 30px;text-align:center;" v-else>
          没有可参与的【启用状态】的促销规则
        </div>
      </div>
      <div style="padding: 30px 0 0;text-align: center;">
        <a-button @click="cancel" style="margin-right: 15px" :type="total?'default':'primary'" id="chooseCustom-btn-back">{{ total?'取消':'知道了' }}</a-button>
        <a-button v-if="total" type="primary" :loading="confirmLoading" @click="salesBillDetailSnList.length?handleBatchSubmit():handleSubmit()" id="chooseCustom-btn-submit">确定</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { salesPromoMatchProduct } from '@/api/salesNew'
export default {
  name: 'SetWarehouse',
  mixins: [commonMixin],
  props: {
    show: [Boolean],
    pageType: {// 判断是转促销弹窗
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      opened: this.show,
      spinning: false,
      confirmLoading: false,
      upActiveVal: null,
      upActiveValArr: [],
      radioStyle: 'display:flex;align-items:center;padding:5px 0;margin:0;',
      radioStyle0: "height: '30px';lineHeight: '30px';padding:5px 0;margin:0;",
      activeList: [],
      editRow: null,
      salesBillDetailSnList: [],
      productType: '0',
      ruleType: '0'
    }
  },
  computed: {
    title () {
      if (this.total == 0) {
        return '提示'
      }
      if (this.salesBillDetailSnList.length) {
        return '批量匹配促销'
      }
      return this.pageType ? '确定换促销活动?' : '确定参加促销活动?'
    },
    total () {
      return this.pageType ? 1 : this.activeList.length
    },
    // 叠加活动
    stackActiveList () {
      if (this.salesBillDetailSnList.length) {
        return this.activeList.filter(item => item.promotionRule && item.promotionRule.stackFlag == 1 && item.enabledFlag == 1)
      }
      return this.activeList.filter(item => item.stackFlag == 1 && item.enabledFlag == 1)
    },
    // 非叠加活动
    noStackActiveList () {
      if (this.salesBillDetailSnList.length) {
        return this.activeList.filter(item => item.promotionRule && item.promotionRule.stackFlag == 0 && item.enabledFlag == 1)
      }
      return this.activeList.filter(item => item.stackFlag == 0 && item.enabledFlag == 1)
    }
  },
  methods: {
    getActiveList (data, record) {
      this.editRow = record
      this.spinning = true
      salesPromoMatchProduct(data).then(res => {
        this.activeList = res.data || []
        // 回显选中项
        if (record && record.promoRuleSnList) {
          if (record.stackFlag == 0) { // 非叠加
            const pos = res.data.findIndex(item => item.promoRuleSn == record.promoRuleSnList[0])
            this.upActiveVal = pos >= 0 ? res.data[pos].salesPromoSn : '-1'
          } else { // 叠加
            const resultSnArr = res.data.filter(item => record.promoRuleSnList.includes(item.promoRuleSn)) // 过滤出promoRuleSn在promoRuleSnList中的对象
              .map(item => item.salesPromoSn) // 映射出这些对象的salesPromoSn属性
            this.upActiveValArr = resultSnArr
          }
        } else {
          this.upActiveVal = this.pageType ? '-1' : null
        }
        this.spinning = false
      })
    },
    // 批量操作
    setBatchData (productSn, activeList) {
      this.activeList = activeList
      this.salesBillDetailSnList = productSn
    },
    //  保存
    handleSubmit (e) {
      const _this = this
      console.log(_this.upActiveVal, this.upActiveValArr.length)
      if (_this.upActiveVal || this.upActiveValArr.length) {
        let salesPromoSnList = []
        // 不参与
        if (this.upActiveVal == '-1') {
          salesPromoSnList = []
        } else {
          salesPromoSnList = this.upActiveVal ? [this.upActiveVal] : this.upActiveValArr
        }
        _this.confirmLoading = true
        _this.$emit('ok', salesPromoSnList, _this.editRow)
      } else {
        _this.$message.info('请选择活动规则!')
      }
    },
    // 批量操作
    handleBatchSubmit (e) {
      const _this = this
      // 选中规则
      if (_this.ruleType == 0) {
        if (_this.upActiveVal || this.upActiveValArr.length) {
          let salesPromoSnList = []
          // 不参与
          if (this.upActiveVal == '-1') {
            salesPromoSnList = []
          } else {
            salesPromoSnList = this.upActiveVal ? [this.upActiveVal] : this.upActiveValArr
          }
          _this.confirmLoading = true
          _this.$emit('batchOk', { salesPromoSnList, salesBillDetailSnList: this.productType == 0 ? this.salesBillDetailSnList : undefined })
        } else {
          _this.$message.info('请选择活动规则!')
        }
      } else {
        // 全部规则
        _this.confirmLoading = true
        const salesPromoSnList = this.activeList.filter(item => item.enabledFlag == 1).map(item => item.salesPromoSn)
        _this.$emit('batchOk', { salesPromoSnList, salesBillDetailSnList: this.productType == 0 ? this.salesBillDetailSnList : undefined })
      }
    },
    resetData () {
      this.upActiveVal = null
      this.upActiveValArr = []
      this.salesBillDetailSnList = []
      this.productType = '0'
      this.ruleType = '0'
    },
    cancel () {
      if (!this.confirmLoading) {
        this.opened = false
        this.resetData()
        this.$emit('cancel')
      } else {
        this.$message.info('正在更换促销，请勿关闭!')
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (!newValue) {
        this.upActiveVal = null
      }
    }
  }
}
</script>
