<template>
  <div>
    <el-dialog
      v-loading="loading"
      :show-close="false"
      :close-on-click-modal="false"
      :visible.sync="isShow"
      :title="curObj.id ? '编辑小程序主题数据' : '新增小程序主题数据'"
      :fullscreen="false"
      width="760px"
      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>
import { arrToObj } from '@/utils'
export default {
  components: { },
  mixins,
  props: {
    isShow: Boolean,
    curObj: Object
  },
  inject: ['parentData'],
  data() {
    return {
      loading: false,
      formData: [],
      cObj: {},
    }
  },
  watch: {
    isShow: function(val) {
      if (val) {
        if (this.curObj.id) {
          this.$api.other.admthemedatadetail({id: this.curObj.id}).then(res => {
            this.cObj = res || {}
            this.getDef()
          })
        } else {
          this.getDef()
        }
      }
    },
  },
  methods: {
    getDef (str) {
      let params = {}
      params = { ...this.cObj }
      if (str === 'edit') {
        params = {...this.$refs.ruleForm.baseForm, ...params}
      }
      if (!params.hide_status) params.hide_status = '1'
      if (!params.type) params.type = 'top_entry_2'
      this.formData = [
        { label: '分类', key: 'type', class: 'c-2', type: 'select', options: this.$dictData.app_theme_type, rules: 1},
        { label: '排序', key: 'sort', class: 'c-2', rules: 1},
        { label: '链接', key: 'link', class: 'c-2', type: 'textarea', rules: 1},
        { label: '图片', rules: 1, key: 'icon', class: 'c-2', type: 'uploadIcon'}, // , type: 'uploadIcon'
        { label: '标题', key: 'title', class: 'c-2', rules: 1},
        { label: '状态', rules: 1, key: 'hide_status', class: 'c-2', type: 'select', options: this.$dictData.hide_status},
        { label: '自定义值1', class: 'c-2', key: 'option1'},
        { label: '自定义值2', class: 'c-2', key: 'option2'},
        { 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
            const newForm = { ...oldform }
            if (this.curObj.id) newForm.id = this.curObj.id
            newForm.theme_id = this.curObj.theme_id
            let apiStr = 'admthemedataadd'
            if (this.curObj.id) apiStr = 'admthemedataedit'
            this.$api.other[apiStr](newForm).then(data => {
              this.$msgs(newForm.id ? '编辑成功' : '新增成功')
              this.cObj = {}
              this.$emit('close', newForm)
            })
          }
        })
      } else {
        this.$emit('close')
        this.cObj = {}
        this.setDefaultValue()
      }
    },
  }
}
</script>
<style lang="scss" scoped>
@import '../../../../styles/libEdit.scss';
.lib-edit {
  padding-top: 0;
  padding-left: 0;
  padding-bottom: 40px;
  ::v-deep .el-form-item {
    margin-bottom: 10px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
</style>
