<template>
  <el-dialog
    title="政策信息"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    @close="handleClose"
    width="1200px"
    height="800px"
  >
    <el-form
      ref="baseForm"
      class="base-form"
      label-position="right"
      label-width="130px"
      :model="baseForm"
      :rules="rules"
      size="small"
    >
      <el-row>
        <el-form-item label="标题：" prop="subsidypolicyTitle">
          <el-input
            placeholder="请输入"
            v-model="baseForm.subsidypolicyTitle">
          </el-input>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="图片：" prop="src">
           <el-upload
             class="avatar-uploader"
             :action="uploadUrl"
             :show-file-list="false"
             :on-success="handleAvatarSuccess"
             :on-error="handleUploadError"
             name="file"
             :headers="headers"
             :before-upload="beforeAvatarUpload">
             <img v-if="baseForm.src" :src="baseForm.src" class="avatar">
             <i v-else class="el-icon-plus avatar-uploader-icon"></i>
           </el-upload>
        </el-form-item>
      </el-row>
      <el-row style="height: 200px;">
        <el-form-item label="内容：" prop="subsidypolicyContent">
          <Editor
            style="height: 150px"
            v-model="baseForm.subsidypolicyContent"
            :mode="mode"
            @onCreated="onCreated"
          />
        </el-form-item>
      </el-row>
    </el-form>

    <div style="text-align: right;margin-top:20px;" class="button-container">
      <el-button type="info" plain @click="resetForm('baseForm')"
        >取消</el-button
      >
      <el-button
        type="primary"
        style="margin-left:16px;"
        @click="submitForm('baseForm')"
        :disabled="dialogSubmitLoading"
        :loading="dialogSubmitLoading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import { addSubsidyPolicy, editSubsidyPolicy } from '@/api/subsidypolicy/index.js'
import { getToken } from "@/utils/auth";
import { Message } from 'element-ui'
import { assign } from 'lodash-es';
export default {
  name:'newPolicy',
  props: {
    data: {
      default() {
        return {};
      }
    },
    visible: {
      type: Boolean,
      default: false
    },
    editRow: {
      type: Object,
      default: () => { }
    },
    areaId: {
      type: String
    },
    policyType: {
      type: String
    }
  },
  data() {
    return {
      uploadUrl: process.env.VUE_APP_BASE_API + "/wpr/subsidypolicy/preview/upload", // 上传的图片服务器地址
      headers: {
        Authorization: "Bearer " + getToken()
      },
      textarea: '',
      editor: null,
      decisionContent:"",
      mode: 'default',
      dialogSubmitLoading: false,
      dialogVisible: false,
      src: '',
      baseForm:  {
        subsidypolicyTitle: '',
        src: '',
        subsidypolicyContent: ''
      },
      resId: '',
      hasFetched: false,
      rules: {
        subsidypolicyTitle: [
          { required: true, message: '请输入您的标题', trigger: 'blur' }
        ],
        src: [
          { required: true, message: '请选择您的图片', trigger: 'blur' }
        ],
        subsidypolicyContent: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    };
  },
  watch: {
    visible(val) {
      if (val !== this.dialogVisible) {
        this.dialogVisible = val;
        if (val) {
          assign(this.baseForm, this.data);
        }
      }
    },
    data: {
      deep: true,
      handler(val) {
        assign(this.baseForm, val);
      }
    },
    editRow: {
      handler(val) {
        this.baseForm = { ...this.editRow }
      },
      deep: true
     }
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor)
    },
    handleAvatarSuccess(res, file) {
      this.resId = res?.data?.resId
      this.baseForm.src = URL.createObjectURL(file.raw);
    },
    handleUploadError() {
      Message.error("图片插入失败");
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        Message({
          type: 'error',
          message: '上传头像图片大小不能超过 2MB!'
        })
      }
      return isJPG && isLt2M;
    },
    resetForm() {
      this.dialogVisible = false;
      this.resetFormData()
    },
    
    resetFormData() {
      this.baseForm = {
        subsidypolicyTitle: '',
        src: '',
        subsidypolicyContent: ''
      };
    },
    async addSubsidyPolicyHandler() {
      if (this.policyType == 'add') {
        const params = {
          areaIds: this.areaId,
          resIds: this.resId,
          subsidypolicyContent: this.baseForm.subsidypolicyContent,
          subsidypolicyTitle: this.baseForm.subsidypolicyTitle
        }
        await addSubsidyPolicy(params)
      } else { 
        const params = {
          areaIds: this.areaId,
          resIds: this.resId,
          subsidypolicyContent: this.baseForm.subsidypolicyContent,
          subsidypolicyTitle: this.baseForm.subsidypolicyTitle,
          subsidypolicyId: this.baseForm.subsidypolicyId
        }
        await editSubsidyPolicy(params)
      }
      this.handleClose()
      this.$emit('addSuccess')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        valid && this.addSubsidyPolicyHandler()
      })
    },
    handleClose() {
      this.$emit('update:visible', false);
      this.resetForm('baseForm');
    },
  }
};
</script>

<style lang="scss" scoped>
::v-deep.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
::v-deep.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
::v-deep.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  line-height: 100px;
  text-align: center;
}
::v-deep.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.input-number {
  width: 100%;
}
.base-form {
  max-height: 70vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 20px;
}
</style>
<style lang="css" scoped>
::v-deep .el-dialog__header {
  border-bottom: 1px solid #ebeef5;
}
</style>
