<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="tagObj.id ? `编辑（${curObj.estate_name}）团购助力规则` : `新增（${curObj.estate_name}）团购助力规则`"
      :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" style="padding-top: 20px;">
          <el-button class="xl-form-btn t2" @click="close">关 闭</el-button>
          <el-button class="xl-form-btn t1" @click="close('confirm')">保存</el-button>
          <el-button class="xl-form-btn t4" @click="delHandle">清空规则</el-button>
        </div>
      </base-form>
    </el-dialog>
  </div>
</template>
<script>
import { arrToObj } from '@/utils'
import baseTable from '_m/baseTable.js'
export default {
  components: { },
  props: {
    isShow: Boolean,
    curObj: Object
  },
  mixins: [...mixins, baseTable],
  inject: ['parentData'],
  data() {
    return {
      formData: [],
      loading: true,
      tagObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        this.getDtl()
      }
    },
  },
  mounted() {},
  methods: {
    getDtl () {
      this.listLoading = true
      this.$api.other.admactivityruledetail({estate_id: this.curObj.id}).then(res => {
        this.tagObj = res.rule || {}
        this.getDef()
        this.listLoading = false
      })
    },
    delHandle() {
      this.$msg(`您确定要清空改规则吗？`, 'confirm', () => {
        this.$api.other.admactivityruledel({
          estate_id: this.curObj.id,
        }).then(data => {
          this.tagObj = {}
          this.$msgs(`已清空！`)
          this.close()
        })
      }, null, true)
    },
    getDef() {
      const params = {...this.tagObj}
      this.formData = [
        { label: '是否开启', key: 'is_open', class: 'c-2', rules: 1, type: 'select', options: this.$dictData.sys_yesno },
        { label: '截止时间', key: 'end_at', class: 'c-2', rules: 1, type: 'datePicker', type2: 'date' },
        { label: '规则简述', key: 'rule', type: 'textarea', rules: 1 },
        { label: '助力人数', key: 'option_req', type: 'inputFont', appendFont: '人', rules: 
          [
            {
              validator: (rule, value, callback) => {
                if (Number(value) < 0 || isNaN(Number(value))) {
                  callback(new Error('请输入数字'))
                } else {
                  callback()
                }
              }, trigger: 'blur'
            }
          ]
        },
        { label: '规则700*1248', key: 'rule_img', class: 'c-2', type: 'uploads', rules: 1 },
        { label: '活动660*880', key: 'poster_img', class: 'c-2', type: 'uploads', rules: 1 },
      ]
      this.setDefaultValue(params)
    },
    close(str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            const newForm = { ...oldform }
            newForm.estate_id = this.curObj.id
            let apiStr = 'admactivityruleadd'
            if (this.tagObj.id) apiStr = 'admactivityruleedit'
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(this.tagObj.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;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
.scoped-ed {
  .se-op {
    display: inline-block;
    vertical-align: middle;
    border: 1px solid #dcdcdc;
    margin-bottom: 10px;
    margin-right: 10px;
    .t {
      display: inline-block;
      vertical-align: middle;
      height: 30px;
      line-height: 30px;
      width: 150px;
      padding: 0 20px;
      font-weight: bold;
    }
    .s {
      display: inline-block;
      vertical-align: middle;
      color: #fff;
      background: #ff875c;
      height: 30px;
      line-height: 30px;
      padding: 0 10px;
      cursor: pointer;
    }
  }
}
</style>
