<template>
  <Dialog :is-dialog-exist="showDialog" :title="dialogTitle" :dialog-style="dialogStyle" @close-dialog="handleClose">
    <el-container>
      <el-main>
        <el-form ref="formData" :model="formData" :rules="rules" label-width="auto">
          <el-form-item label="等级名称" prop="name">
            <el-input v-model="formData.name" placeholder="等级名称"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="memo">
            <el-input v-model="formData.memo" placeholder="备注"></el-input>
          </el-form-item>
          <el-form-item label="等级权重" prop="weight">
            <el-input v-model="formData.weight" type="number"></el-input>
            <CustomToolTip content="数字越大，等级越高"></CustomToolTip>
          </el-form-item>
          <el-form-item label="自购折扣" prop="selfDiscountRate">
            <el-input v-model="formData.selfDiscountRate" type="number" placeholder="例如95">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="优惠码折扣" prop="discountCodeRate">
            <el-input v-model="formData.discountCodeRate" type="number" placeholder="例如95">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="单个订单最高优惠金额" prop="discountMaxPrice">
            <el-input v-model="formData.discountMaxPrice" type="number">
              <template slot="append">元</template>
            </el-input>
            <CustomToolTip content="最高优惠金额10000元，设置为0为不限制"></CustomToolTip>
          </el-form-item>
          <el-form-item label="推广佣金比例" prop="promoCommissionRate">
            <el-input v-model="formData.promoCommissionRate" type="number">
              <template slot="append">%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="成长值区间">
            <el-input v-model="formData.minIntegral" type="number" style="width: 41%; margin-right: 10px;"></el-input>
            <span>至</span>
            <el-input v-model="formData.maxIntegral" type="number" style="width: 41%; margin-left: 10px;"></el-input>
            <CustomToolTip content="填写该等级成长值区间，最小值与最大值"></CustomToolTip>
          </el-form-item>
          <el-form-item label="隐藏等级" prop="isHideLevel">
            <el-switch v-model="formData.isHideLevel" active-value="1" inactive-value="0"></el-switch>
          </el-form-item>
          <el-form-item label="能否享受优惠码折扣" prop="isDiscountCode">
            <el-switch v-model="formData.isDiscountCode" active-value="1" inactive-value="0"></el-switch>
          </el-form-item>
        </el-form>
      </el-main>
      <el-footer>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-footer>
    </el-container>
  </Dialog>
</template>

<script>
import Dialog from '@/components/Dialog/index'
import CustomToolTip from '@/components/Common/CustomToolTip'
import { saveData } from '@/api/master-info-manage/master-level'
/**
 * 站长等级的编辑dialog
 */
export default {
  components: { Dialog, CustomToolTip },
  props: {
    // 弹窗控制参数
    showDialog: {
      type: Boolean,
      default: false
    },
    // 弹窗标题
    dialogTitle: {
      type: String,
      default: ''
    },
    // 选中的数据
    selectItem: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      // dialog样式
      dialogStyle: {
        '--dialog-width': '850px',
        '--dialog-height': '600px',
        '--title-height': '51px'
      },
      localFormData: {
        id: '',
        // 等级名称
        name: '',
        // 备注
        memo: '',
        // 等级权重
        weight: '',
        // 自购折扣
        selfDiscountRate: '',
        // 优惠码折扣
        discountCodeRate: '',
        // 单个订单最高优惠金额
        discountMaxPrice: '0',
        // 推广佣金比例
        promoCommissionRate: '',
        // 成长值要求
        minIntegral: '0',
        maxIntegral: '9999',
        // 隐藏等级
        isHideLevel: '0',
        // 能否享受优惠码折扣
        isDiscountCode: '0'
      },
      // 提交参数
      formData: {},
      // 校验规则
      rules: {
        name: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        weight: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        selfDiscountRate: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        discountCodeRate: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        // discountMaxPrice: [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],
        promoCommissionRate: [
          { required: true, message: '请输入', trigger: 'blur' }
        ],
        isHideLevel: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        isDiscountCode: [
          { required: true, message: '请选择', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    showDialog(val) {
      if (val) {
        this.initDataView()
      }
    }
  },
  methods: {
    // 关闭弹窗
    handleClose() {
      this.$emit('handle-close')
    },
    // 提交弹窗
    handleSuccess() {
      this.$emit('handle-success')
    },
    // 初始化弹窗参数
    initDataView() {
      if (this.selectItem.id === undefined) {
        // 如果是新增 则设置默认值
        this.formData = this.$lodash.cloneDeep(this.localFormData)
      } else {
        // 如果是编辑，则赋值
        this.formData = {}
        this.formData = this.$lodash.cloneDeep(this.selectItem)
        this.formData.isHideLevel = this.selectItem.isAgent
      }
      console.log('初始化弹窗参数' + JSON.stringify(this.formData))
    },
    // 提交表单
    submitForm() {
      this.$refs.formData.validate(vaild => {
        if (vaild && this.customValidate()) {
          console.log('提交表单')
          this._saveData()
        }
      })
    },
    customValidate() {
      let rlt = true
      if (this.formData.minIntegral === undefined || Number(this.formData.minIntegral) < 0) {
        this.$message.warning('成长区间最小值不能小于0')
        rlt = false
      }
      if (this.formData.maxIntegral === undefined || Number(this.formData.maxIntegral) < 0) {
        this.$message.warning('成长区间最大值不能小于0')
        rlt = false
      }
      if (Number(this.formData.maxIntegral) < Number(this.formData.minIntegral)) {
        this.$message.warning('成长区间最大值不能小于最小值')
        rlt = false
      }
      return rlt
    },
    _saveData() {
      const params = {
        id: this.formData.id,
        name: this.formData.name,
        memo: this.formData.memo,
        weight: this.formData.weight,
        selfDiscountRate: this.formData.selfDiscountRate,
        discountCodeRate: this.formData.discountCodeRate,
        discountMaxPrice: this.formData.discountMaxPrice,
        promoCommissionRate: this.formData.promoCommissionRate,
        minIntegral: this.formData.minIntegral,
        maxIntegral: this.formData.maxIntegral,
        isHideLevel: this.formData.isHideLevel,
        isDiscountCode: this.formData.isDiscountCode
      }
      saveData(params).then(resp => {
        if (resp.code === 0) {
          this.$message.success('操作成功～')
          this.handleSuccess()
          this.handleClose()
        } else {
          this.$message.error(resp.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
  .el-container {
    width: 100%;
    height: 100%;
    .el-main {
      height: 90%;
      width: 100%;
      padding-left: 76px;
      padding-right: 100px;
      .el-input {
        width: 90%;
      }
    }
    .el-footer {
      height: 10%;
      width: 100%;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
</style>
