<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="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">
              {{ sindex+1 }}、{{ item.description }}
            </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">
              {{ sindex+1 }}、{{ item.description }}
            </a-checkbox>
          </a-checkbox-group>
        </div>
      </div>
      <div style="padding: 10px 30px;text-align:center;" v-else>
        没有可参与的【启用状态】的促销规则
      </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="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:block;height: '30px';lineHeight: '30px';padding:5px 0;margin:0;",
      activeList: [],
      editRow: null
    }
  },
  computed: {
    title () {
      if (this.total == 0) {
        return '提示'
      }
      return this.pageType ? '确定换促销活动?' : '确定参加促销活动?'
    },
    total () {
      return this.pageType ? 1 : this.activeList.length
    },
    // 叠加活动
    stackActiveList () {
      return this.activeList.filter(item => item.stackFlag == 1)
    },
    // 非叠加活动
    noStackActiveList () {
      return this.activeList.filter(item => item.stackFlag == 0)
    }
  },
  methods: {
    getActiveList (data, record) {
      this.editRow = record
      this.spinning = true
      salesPromoMatchProduct(data).then(res => {
        this.activeList = res.data || []
        this.spinning = false
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      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('请选择活动规则!')
      }
    },
    cancel () {
      if (!this.confirmLoading) {
        this.opened = false
        this.upActiveVal = null
        this.upActiveValArr = []
        this.$emit('cancel')
      } else {
        this.$message.info('正在更换促销，请勿关闭!')
      }
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (!newValue) {
        this.upActiveVal = null
      }
    }
  }
}
</script>
