<template>
  <a-modal
    centered
    class="promotionList-basicInfo-modal"
    :footer="null"
    :maskClosable="false"
    title="规则设置"
    v-model="isShow"
    @cancel="isShow=false"
    width="70%">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="promotionList-basicInfo-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol" >
        <a-form-model-item label="规则门槛" prop="gateFlag">
          <a-radio-group v-model="form.gateFlag" button-style="solid" size="small" @change="changeGateFlag">
            <a-radio-button value="1">
              有
            </a-radio-button>
            <a-radio-button value="0">
              无
            </a-radio-button>
          </a-radio-group>
          <div class="ruleDescList" v-if="form.gateFlag && form.gateFlag === '1'">
            购买满
            <a-input-number v-model="form.gateAmount" :min="0" :precision="2" size="small"/>
            元门槛产品，可使用
            <a-input-number v-model="form.quotaAmount" :min="0" :precision="2" size="small"/>
            元配额，采购规则中的正价商品（配额算销售额)
          </div>
        </a-form-model-item>
        <a-form-model-item label="满赠规则" prop="regularSameFlag">
          <div class="ruleDescList">
            <a-select default-value="0" :value="form.regularSameFlag" style="width: 100px" size="small">
              <a-select-option value="1">
                同款
              </a-select-option>
              <a-select-option value="0">
                不同款
              </a-select-option>
            </a-select>
            产品购买满
            <a-input-number v-model="form.regularQty" :min="0" :step="1" :precision="0" size="small"/>
            个正价产品，送
            <a-input-number v-model="form.promotionQty" :min="0" :step="1" :precision="0" size="small"/>
            个促销产品
          </div>
          <div class="ruleDescList">
            <a-checkbox size="small" :value="form.restrictFlag">
              限制正价产品款数
            </a-checkbox>
            <a-input-number v-model="form.restrictCategory" :min="0" :step="1" :precision="0" size="small"/>
          </div>
        </a-form-model-item>
        <a-form-model-item label="数量叠加" prop="accrualFlag">
          <a-radio-group v-model="form.accrualFlag" button-style="solid" size="small">
            <a-radio-button value="1">
              是
            </a-radio-button>
            <a-radio-button value="0">
              否
            </a-radio-button>
          </a-radio-group>
          <span style="color:gray;">（如：满10送2，数量叠加则：满20送4，满30送6）</span>
        </a-form-model-item>
        <a-form-model-item prop="minOrderFlag">
          <span slot="label">
            <a-tooltip title="What do you want others to call you?">
              <a-icon type="info-circle" />
            </a-tooltip>
            订单起订金额
          </span>
          <a-select default-value="1" :value="form.minOrderFlag" style="width: 160px" size="small">
            <a-select-option value="0">
              不限
            </a-select-option>
            <a-select-option value="1">
              限制
            </a-select-option>
          </a-select>
          <a-input-number
            v-show="form.minOrderFlag && form.minOrderFlag=='1'"
            size="small"
            style="margin-left:10px;width:300px;"
            v-model="form.minOrderAmount"
            :min="0"
            :step="1"
            :precision="2"
            placeholder="请输入金额"/>
        </a-form-model-item>
        <a-form-model-item prop="upperLimitFlag">
          <span slot="label">
            <a-tooltip title="What do you want others to call you?">
              <a-icon type="info-circle" />
            </a-tooltip>
            活动经费上限
          </span>
          <a-select default-value="1" :value="form.upperLimitFlag" style="width: 160px" size="small">
            <a-select-option value="0">
              不限
            </a-select-option>
            <a-select-option value="1">
              限制
            </a-select-option>
          </a-select>
          <a-input-number
            v-show="form.upperLimitFlag&&form.upperLimitFlag=='1'"
            size="small"
            style="margin-left:10px;width:300px;"
            v-model="form.upperLimitAmount"
            :min="0"
            :step="1"
            :precision="2"
            placeholder="请输入金额"/>
        </a-form-model-item>
        <a-form-model-item label="促销品与正品一致" prop="regularPromotionSameFlag">
          <a-radio-group v-model="form.regularPromotionSameFlag" button-style="solid" size="small">
            <a-radio-button value="1">
              是
            </a-radio-button>
            <a-radio-button value="0">
              否
            </a-radio-button>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
      <a-card size="small" :bordered="false" class="pages-wrap">
        <div class="flex">
          <a-radio-group v-model="chooseVal" button-style="solid" size="small">
            <a-radio-button value="a" v-show="form.gateFlag==='1'">
              门槛产品
            </a-radio-button>
            <a-radio-button value="b">
              正价产品
            </a-radio-button>
            <a-radio-button value="c" v-show="form.regularPromotionSameFlag==='0'">
              促销产品
            </a-radio-button>
          </a-radio-group>
          <a-button
            size="small"
            type="link"
            class="button-info"
            id="promotionList-edit-btn">+导入产品</a-button>
        </div>
        <div v-show="chooseVal=='a'"><tableType1 ref="cillProduct" selectType="gateProduct"></tableType1></div>
        <div v-show="chooseVal=='b'"><tableType1 ref="normalPriceProduct" selectType="regularProduct"></tableType1></div>
        <div v-show="chooseVal=='c'"><tableType2 ref="offerProduct" selectType="specialProductList"></tableType2></div>
      </a-card>
      <div class="btn-cont">
        <a-button id="promotionList-basicInfo-modal-back" @click="isShow = false">取消</a-button>
        <a-button style="margin-left: 15px;" type="primary" id="promotionList-modal-save" @click="handleSave">确定</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { VSelect } from '@/components'
import tableType1 from './tableType1.vue'
import tableType2 from './tableType2.vue'
import { promotionSave, sparePartsReturnInfo } from '@/api/promotion'
export default {
  name: 'PromotionListBasicInfoModal',
  mixins: [commonMixin],
  components: { VSelect, tableType1, tableType2 },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    promotionSn: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 17 }
      },
      form: {
        promotionRuleType: 'BUY_PROD_GIVE_PROD', // 买产品送产品
        gateFlag: '0', // 门槛
        gateAmount: '',
        quotaAmount: '',
        regularSameFlag: '0', // 满赠规则  不同款商品
        regularQty: '',
        promotionQty: '',
        restrictFlag: undefined, // 限制正价产品款数
        restrictCategory: '',
        accrualFlag: '1', // 数量叠加
        minOrderFlag: '1', // 起订金额
        minOrderAmount: '',
        upperLimitFlag: '1', // 活动经费上限
        upperLimitAmount: '',
        regularPromotionSameFlag: undefined
      },
      rules: {
        gateFlag: [ { required: true, message: '请选择规则门槛', trigger: 'change' } ],
        regularSameFlag: [{ required: true, message: '请选择满赠规则', trigger: 'change' }],
        accrualFlag: [{ required: true, message: '请选择数量是否叠加', trigger: 'change' }],
        minOrderFlag: [{ required: true, message: '请选择订单起订金额', trigger: 'change' }],
        upperLimitFlag: [{ required: true, message: '请选择活动经费上限', trigger: 'change' }],
        regularPromotionSameFlag: [{ required: true, message: '请选择促销品与正品是否一致', trigger: 'change' }]
      },
      chooseVal: 'a'
    }
  },
  methods: {
    // 门槛切换  tab 按钮默认显示问题
    changeGateFlag (e) {
      if (this.chooseVal != 'c') {
        if (e.target.value == '0') {
          this.chooseVal = 'b'
        } else {
          this.chooseVal = 'a'
        }
      }
    },
    //  保存
    handleSave () {
      const _this = this
      const tableData = this.$refs.cillProduct.getResultVal()
      const newtableData = this.$refs.normalPriceProduct.getResultVal()
      const newtableData1 = this.$refs.offerProduct.getResultVal()
      debugger
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const form = JSON.parse(JSON.stringify(_this.form))
          form.attachmentList = _this.attachList
          _this.spinning = true
          promotionSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.isShow = false
                _this.$emit('ok', res.data)
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    // 获取编辑详情
    getDetail () {
      sparePartsReturnInfo({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          const resultObj = res.data
          this.attachList = resultObj.attachmentList
          const obj = {
            supplierSn: resultObj.supplierSn,
            returnReason: resultObj.returnReason, // 退货原因
            explainInfo: resultObj.explainInfo, // 补充说明
            warehouseSn: resultObj.warehouseSn,
            sparePartsReturnSn: this.itemSn
          }
          this.$nextTick(() => {
            this.$refs.attachList.setFileList(this.attachList)
          })
          this.form = { ...this.form, ...obj }
        } else {
          this.detailsData = null
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.attachList = []
        this.supplierName = null
        this.$nextTick(() => {
          this.$refs.attachList.setFileList('')
          this.$refs.ruleForm.resetFields()
        })
      } else {
        if (this.itemSn) {
          this.getDetail()
        } else {
          this.chooseVal = this.form.gateFlag == '0' ? 'b' : 'a'
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
  .promotionList-basicInfo-modal{
    /deep/.ant-modal-body {
      padding: 40px 40px 24px;
      max-height: 745px !important;
      overflow-y: scroll !important;
    }
    .ant-radio-button-wrapper{
      width:80px;
      text-align: center;
    }
    .ant-form-item{
      margin-bottom:5px;
    }
    .buyerBox{
      border:1px solid #d9d9d9;margin-top:10px;border-radius:4px;padding:4px 10px;background:#f2f2f2;max-height:130px;overflow-y:scroll;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
    .flex{
      display:flex;
      align-items:center;
      justify-content:space-between;
    }
  }

</style>
