<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="this.curObj && this.curObj.id ? '编辑规则' : '添加规则'"
      :fullscreen="false"
      width="700px"
      custom-class="xl-dialog"
      center
    >
      <base-form ref="ruleForm" class="lib-edit" :data="formData" :is-inline="false" label-width="110px">
        <div slot="footer">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">确定</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {},
  mixins: [...mixins],
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      cObj: {},
      isShowMap: false
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDef()
      }
    },
  },
  methods: {
    getDef() {
      const params = { ...this.curObj }
      this.formData = [
        { label: '类目', key: 'category', class: 'c-2', rules: 1 },
        { label: '奖励积分', key: 'reward_point', class: 'c-2', rules: 1 },
        { label: '奖励周期', key: 'cycle', class: 'c-2', type: 'inputFont', appendFont: '天', rules: 1 },
        { label: '奖励次数', key: 'reward_num', class: 'c-2', rules: 1 },
        { label: '类型', key: 'rule_type', class: 'c-2', rules: 1, type: 'select', options: this.$dictData.sale_rule_type },
        { label: '说明', key: 'remark' },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            let newForm = { ...oldform }
            let apiStr = 'admsaleruleadd'
            if (this.curObj.id) {
              apiStr = 'admsaleruleedit'
              newForm.id = this.curObj.id
            }
            this.$api.facility[apiStr](newForm).then(data => {
              this.$msgs(this.curObj.id ? '编辑成功' : '新增成功')
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import "../../../../styles/libEdit.scss";
.lib-edit {
  padding-top: 0;
  width:100%;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
</style>
