<template>
  <div>
    <xy-dialog ref="dialog" :is-show.sync="isShow" type="form" :title="type === 'add' ? '新增详情' : '编辑详情'" :form="form"
      :rules="rules" @submit="submit">
      <template v-slot:name>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>详情名称：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.name" placeholder="请输入详情名称" clearable style="width: 300px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:key>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>详情编码：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.key" placeholder="请输入详情编码" clearable style="width: 300px;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:value>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>详情内容：
          </div>
          <div class="xy-table-item-content" style="width: 680px;">
            <xyTinymce v-if="isShow" :height="200" v-model="form.value"></xyTinymce>
            <!-- <el-input :autosize="{minRows:2}" type="textarea" v-model="form.value" placeholder="请输入详情内容" clearable
              style="width: 300px;"></el-input> -->
          </div>
        </div>
      </template>
    </xy-dialog>


  </div>
</template>

<script>
  import {
    show,
    save
  } from "@/api/resource/bd.js"
  import xyTinymce from "@/components/XyTinymce/index.vue";
  export default {
    components: {
      xyTinymce
    },
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',

        form: {
          name:"",
          key:"",
          value:''
        },
        rules:{
          name:[{
            required: true,
            message: '请输入详情名称'
          }],
          key:[{
            required: true,
            message: '请输入详情编码'
          }],
          value:[{
            required: true,
            message: '请输入详情内容'
          }]
        }
      }
    },
    created() {

    },
    methods: {

      async getDetail() {
        const res = await show({
          id: this.id
        })
        this.form = {
            name:res?.name,
            key:res?.key,
            value:res?.value
        }
      },
      submit() {

        let that = this
        if (this.type === 'editor') {
          this.form.id = this.id
        }else{
          this.form.id = ""
        }
        save({
          ...that.form
        }).then(res => {
          this.$successMessage(this.type, '详情')
          this.isShow = false
          this.$emit('refresh')
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.$refs['dialog'].reset()
        }
      }
    }

  }
</script>

<style scoped lang="scss">
  .xy-table-item-label {
    width: 160px;
  }

  .img__delete {
    transform: scale(0.8, 0.8);

    position: absolute;
    top: 4px;
    right: 4px;
  }
</style>
