<template>
  <a-modal
    v-model="opened"
    :title="title"
    centered
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    :width="total?'450px':'300px'"
    :footer="null"
    @cancel="cancel"
  >
    <a-spin :spinning="spinning" tip="Loading...">
      <div style="padding: 0 30px;" v-if="total">
        <aRadioGroup v-model="upActiveVal">
          <aRadio :style="radioStyle" value="0" v-if="promoRuleSn">
            不参加促销
          </aRadio>
          <div v-for="(item,idx) in activeList" :key="idx">
            <div>
              <strong>{{ item.title }}</strong>
            </div>
            <aRadio :style="radioStyle" v-for="(sitem,sindex) in item.promotionRuleList" :key="sindex" :value="item.promotionSn+'-'+sitem.promotionRuleSn+'-'+sitem.promotionProductType">
              {{ sindex+1 }}、{{ sitem.description }}
            </aRadio>
          </div>
        </aRadioGroup>
      </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]
  },
  data () {
    return {
      opened: this.show,
      spinning: false,
      confirmLoading: false,
      upActiveVal: null,
      radioStyle: "display:block;height: '30px';lineHeight: '30px';padding:5px 0;",
      activeList: [],
      editRow: null,
      promoRuleSn: null
    }
  },
  computed: {
    title () {
      if (this.total == 0) {
        return '提示'
      }
      return this.promoRuleSn ? '确定换促销活动?' : '确定参加促销活动?'
    },
    total () {
      return this.promoRuleSn ? 1 : this.activeList.length
    }
  },
  methods: {
    getActiveList (data, record) {
      this.promoRuleSn = data.promoRuleSn
      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.confirmLoading = true
        _this.$emit('ok', _this.upActiveVal, _this.editRow)
      } else {
        _this.$message.info('请选择活动规则!')
      }
    },
    cancel () {
      this.opened = false
      this.upActiveVal = null
      this.$emit('cancel')
    }
  },
  watch: {
    show (newValue, oldValue) {
      this.opened = newValue
      if (!newValue) {
        this.upActiveVal = null
      }
    }
  }
}
</script>
