<template>
<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="470px"
  custom-class="xl-dialog"
  center>
  <base-form style="width:400px" :data="formData" ref="ruleForm" :isInline="false" labelWidth="120px">
    <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>
</template>
<script>
export default {
  props: {
    isShow: Boolean,
    curObj: Object
  },
  mixins,
  data() {
    return {
      formData: [],
      loading: true
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        let params = {...this.curObj}
        params.startEndTime = [params.start_at, params.end_at]
        this.formData = [
          {label: '主题名称', key: 'theme_name', rules: 1},
          {label: '执行日期', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker', rules: 1},
          {label: '类型说明', key: 'remark'},
        ]
        this.setDefaultValue(params)
      }
    }
  },
  methods: {
    systemChange (val) {
      // console.log(val)
    },
    close (str) {
      if (str === 'confirm') {
        this.$refs['ruleForm'].$refs['baseForm'].validate((valid) => {
          if (valid) {
            const oldform = this.$refs.ruleForm.baseForm
            let params = {...oldform}
            let str = 'admthemeadd'
            if(this.curObj && this.curObj.id) {
              params.id = this.curObj.id
              str = 'admthemeedit'
            }
            if (params.startEndTime) {
              params.start_at = params.startEndTime[0]
              params.end_at = params.startEndTime[1]
              delete params.startEndTime
            }
            this.$api.other[str](params).then(data => {
              this.$msgs('保存成功！')
              this.$emit('close', params)
              // this.setDefaultValue()
            })
          }
        })
      } else {
        this.$emit('close')
        this.setDefaultValue()
      }
    }
  }
}
</script>
