<template>
  <div>
    <xy-dialog ref="dialog" :width="70" :is-show.sync="isShow" :type="'form'"
      :title="type === 'add' ? '新增通知公告' : '编辑通知公告'" :form="form" :rules='rules' @submit="submit">
      <template v-slot:biaoti>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>标题：
          </div>
          <div class="xy-table-item-content">
            <el-input v-model="form.biaoti" placeholder="请输入标题" clearable style="width: 100%;"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:image_id>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold;">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>标题图片（370*460）：
          </div>
          <div class="xy-table-item-content">
            <el-upload :action="action" class='upload-demo' :limit="1" list-type="picture-card" :file-list="imgList"
              ref="pictureUpload" :before-upload="beforeUpload" :auto-upload="true" :on-success="uploadSuccess" :on-remove="uploadRemove">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </div>
      </template>
      <template v-slot:leibie>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;">*</span>类别：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.leibie" placeholder="请选择类别" clearable style="width: 100%;">
              <el-option v-for="item in leibie_options" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:neirong>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>内容：
          </div>
          <div class="xy-table-item-content" style="flex-grow: 1;">
            <my-tinymce v-if="showTinymce" @input="saveContent" :value="form.neirong"></my-tinymce>
          </div>
        </div>
      </template>
      <template v-slot:shifoutongbugongzhonghao>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>是否同步公众号：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.shifoutongbugongzhonghao" placeholder="请选择" clearable style="width: 100%;">
              <el-option v-for="item in gzh_options" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:zhuangtai>
        <div class="xy-table-item">
          <div class="xy-table-item-label" style="font-weight: bold">
            <span style="color: red;font-weight: bold;padding-right: 4px;"></span>状态：
          </div>
          <div class="xy-table-item-content">
            <el-select v-model="form.zhuangtai" placeholder="请选择" clearable style="width: 100%;">
              <el-option v-for="item in zt_options" :key="item.id" :label="item.value" :value="item.id">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
    </xy-dialog>

  </div>
</template>

<script>
  import {
    save,
    show
  } from "@/api/system/baseForm.js"
  import {
    deepCopy,
    isNull
  } from "@/utils";
  export default {
    components: {

    },
    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        action: `${process.env.VUE_APP_UPLOAD_API}`,
        showTinymce: false,
        table_name: 'notices',

        leibie_options: [{
          id: 1,
          value: '公告'
        }, {
          id: 2,
          value: '通知'
        }],
        gzh_options: [{
          id: 0,
          value: '否'
        }, {
          id: 1,
          value: '是'
        }],
        zt_options: [{
          id: 0,
          value: '待发布'
        }, {
          id: 1,
          value: '发布'
        }],
        imgList: [],
        form: {
          biaoti: '',
          leibie: '',
          image_id: '',
          neirong: '',
          shifoutongbugongzhonghao: 0,
          zhuangtai: 1
        },
        rules: {
          biaoti: [{
            required: true,
            message: '请输入标题'
          }],
          leibie: [{
            required: true,
            message: '请选择类别'
          }],
          image_id: [{
            required: true,
            message: '请上传标题图片'
          }],
        }
      }
    },
    created() {},
    methods: {
      saveContent(e) {
        this.form.neirong = e
      },
      beforeUpload(file) {
        console.log(file)
        const isImage = file.type.includes('image');
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isImage) {
          this.$message.error('请上传正确的图片格式文件');
        }
        if (!isLt2M) {
          this.$message.error('上传文件大小不能超过2MB');
        }
        return isImage && isLt2M;
      },
      uploadSuccess(response, file, fileList) {
        console.log(response, file, fileList)
        this.form.image_id = response.id
        this.imgList = fileList
      },
      uploadRemove(file, fileList){
        this.imgList = fileList
        this.form.image_id = ''
      },
      submit() {
        if (this.id) {
          this.form.id = this.id
        }
        if (this.type == 'add') {
          this.form.id = ''
        }
        console.log("this.form",this.form)
        save({
          table_name: this.table_name,
          ...this.form
        }).then(res => {
          this.$message({
            type: 'success',
            message: this.type === 'add' ? '新增成功' : '编辑成功'
          })
          this.isShow = false
          this.$emit('refresh')
        })
      },

      getDetail() {
        show({
          id: this.id,
          table_name: this.table_name,
        }).then(res => {
          this.form = this.base.requestToForm(res, this.form)
          console.log("this.form",this.form)
          if(res.image_id){
            this.imgList.push(res.image_id_uploads_id_relation)
          }
          this.showTinymce = true
        })
      }
    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          } else {
            this.showTinymce = true
          }
        } else {
          this.id = ''
          this.type = "add"
          this.imgList = []
          this.showTinymce = false
          this.$refs['dialog'].reset()
        }
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .neirong {
    flex-basis: 100%;
  }
</style>
