<template>
  <!-- 主容器块 -->
  <div class="addEdit-block">
    <!-- 表单组件，用于添加或修改 -->
    <el-form
      class="detail-form-content"
      ref="ruleForm"
      :model="ruleForm" <!-- 绑定表单数据模型 -->
      :rules="rules" <!-- 表单验证规则 -->
      label-width="80px" <!-- 标签宽度 -->
	  :style="{backgroundColor:addEditForm.addEditBoxColor}" <!-- 动态背景颜色 -->
    >
      <el-row>
        <!-- 空行占位 -->
      </el-row>
      <!-- 评论内容区域 -->
      <el-row>
        <el-col :span="24">
          <!-- 评论内容输入框（编辑模式） -->
          <el-form-item class="textarea" v-if="type!='info'" label="评论内容" prop="content">
            <el-input
              style="min-width: 200px; max-width: 600px;" <!-- 输入框宽度限制 -->
              type="textarea" <!-- 文本域类型 -->
              :rows="8" <!-- 行数 -->
              placeholder="评论内容" <!-- 占位符 -->
              v-model="ruleForm.content" readonly> <!-- 只读绑定 -->
            </el-input>
          </el-form-item>
          <!-- 评论内容显示（查看模式） -->
          <div v-else>
            <el-form-item v-if="ruleForm.content" label="评论内容" prop="content">
                <span>{{ruleForm.content}}</span> <!-- 显示评论内容 -->
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- 回复内容区域 -->
      <el-row>
        <el-col :span="24">
          <!-- 回复内容输入框（编辑模式） -->
          <el-form-item class="textarea" v-if="type!='info'" label="回复内容" prop="reply">
            <el-input
              style="min-width: 200px; max-width: 600px;" <!-- 输入框宽度限制 -->
              type="textarea" <!-- 文本域类型 -->
              :rows="8" <!-- 行数 -->
              placeholder="回复内容" <!-- 占位符 -->
              v-model="ruleForm.reply"> <!-- 绑定回复内容 -->
            </el-input>
          </el-form-item>
          <!-- 回复内容显示（查看模式） -->
          <div v-else>
            <el-form-item v-if="ruleForm.reply" label="回复内容" prop="reply">
                <span>{{ruleForm.reply}}</span> <!-- 显示回复内容 -->
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- 操作按钮区域 -->
      <el-form-item class="btn">
        <!-- 提交按钮（编辑模式） -->
        <el-button v-if="type!='info'" type="primary" class="btn-success" @click="onSubmit">提交</el-button>
        <!-- 取消按钮（编辑模式） -->
        <el-button v-if="type!='info'" class="btn-close" @click="back()">取消</el-button>
        <!-- 返回按钮（查看模式） -->
        <el-button v-if="type=='info'" class="btn-close" @click="back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
// 导入校验工具函数
import { isNumber,isIntNumer,isEmail,isPhone, isMobile,isURL,checkIdCard } from "@/utils/validate";

export default {
  data() {
    let self = this;
    // 身份证校验规则
    var validateIdCard = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!checkIdCard(value)) {
        callback(new Error("请输入正确的身份证号码"));
      } else {
        callback();
      }
    };
    // URL校验规则
    var validateUrl = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isURL(value)) {
        callback(new Error("请输入正确的URL地址"));
      } else {
        callback();
      }
    };
    // 手机号校验规则
    var validateMobile = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isMobile(value)) {
        callback(new Error("请输入正确的手机号码"));
      } else {
        callback();
      }
    };
    // 电话号码校验规则
    var validatePhone = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isPhone(value)) {
        callback(new Error("请输入正确的电话号码"));
      } else {
        callback();
      }
    };
    // 邮箱校验规则
    var validateEmail = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isEmail(value)) {
        callback(new Error("请输入正确的邮箱地址"));
      } else {
        callback();
      }
    };
    // 数字校验规则
    var validateNumber = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isNumber(value)) {
        callback(new Error("请输入数字"));
      } else {
        callback();
      }
    };
    // 整数校验规则
    var validateIntNumber = (rule, value, callback) => {
      if(!value){
        callback();
      } else if (!isIntNumer(value)) {
        callback(new Error("请输入整数"));
      } else {
        callback();
      }
    };
    return {
	  addEditForm: {"btnSaveFontColor":"#fff","selectFontSize":"14px","btnCancelBorderColor":"rgba(152, 129, 129, 1)","inputBorderRadius":"22px","inputFontSize":"14px","textareaBgColor":"#fff","btnSaveFontSize":"14px","textareaBorderRadius":"22px","uploadBgColor":"#fff","textareaBorderStyle":"solid","btnCancelWidth":"88px","textareaHeight":"120px","dateBgColor":"#fff","btnSaveBorderRadius":"22px","uploadLableFontSize":"14px","textareaBorderWidth":"1px","inputLableColor":"#606266","addEditBoxColor":"rgba(210, 194, 194, 0.29)","dateIconFontSize":"14px","btnSaveBgColor":"#409EFF","uploadIconFontColor":"#8c939d","textareaBorderColor":"rgba(152, 129, 129, 1)","btnCancelBgColor":"rgba(143, 222, 143, 1)","selectLableColor":"#606266","btnSaveBorderStyle":"solid","dateBorderWidth":"1px","dateLableFontSize":"14px","dateBorderRadius":"22px","btnCancelBorderStyle":"solid","selectLableFontSize":"14px","selectBorderStyle":"solid","selectIconFontColor":"#C0C4CC","btnCancelHeight":"44px","inputHeight":"40px","btnCancelFontColor":"#606266","dateBorderColor":"rgba(152, 129, 129, 1)","dateIconFontColor":"#C0C4CC","uploadBorderStyle":"solid","dateBorderStyle":"solid","dateLableColor":"#606266","dateFontSize":"14px","inputBorderWidth":"1px","uploadIconFontSize":"28px","selectHeight":"40px","inputFontColor":"#606266","uploadHeight":"148px","textareaLableColor":"#606266","textareaLableFontSize":"14px","btnCancelFontSize":"14px","inputBorderStyle":"solid","btnCancelBorderRadius":"22px","inputBgColor":"rgba(252, 250, 250, 1)","inputLableFontSize":"14px","uploadLableColor":"#606266","uploadBorderRadius":"22px","btnSaveHeight":"44px","selectBgColor":"#fff","btnSaveWidth":"88px","selectIconFontSize":"14px","dateHeight":"40px","selectBorderColor":"rgba(152, 129, 129, 1)","inputBorderColor":"rgba(152, 129, 129, 1)","uploadBorderColor":"rgba(152, 129, 129, 1)","textareaFontColor":"#606266","selectBorderWidth":"1px","dateFontColor":"#606266","btnCancelBorderWidth":"1px","uploadBorderWidth":"1px","textareaFontSize":"14px","selectBorderRadius":"22px","selectFontColor":"#606266","btnSaveBorderColor":"#409EFF","btnSaveBorderWidth":"1px"}, // 样式配置对象
      id: '', // 数据ID
      type: '', // 页面类型（新增、编辑、查看）
      ro:{
	refid : false, // 关联表ID只读状态
	userid : false, // 用户ID只读状态
	content : false, // 评论内容只读状态
	reply : false, // 回复内容只读状态
      },
      ruleForm: {
                	        refid: '', // 关联表ID
	                        	        userid: '', // 用户ID
	                        	        content: '', // 评论内容
	                        	        reply: '', // 回复内容
	                      },
      rules: {
                  refid: [
                            { required: true, message: '关联表id不能为空', trigger: 'blur' }, // 关联表ID必填校验
                                    	                                                              ],
                  userid: [
                            { required: true, message: '用户id不能为空', trigger: 'blur' }, // 用户ID必填校验
                                    	                                                              ],
                  content: [
                            { required: true, message: '评论内容不能为空', trigger: 'blur' }, // 评论内容必填校验
                                    	                                                              ],
                  reply: [
                                    	                                                              ], // 回复内容无校验
              }
    };
  },
  props: ["parent"], // 父组件传递的属性
  computed: {}, // 计算属性
  created() {
	this.addEditStyleChange(); // 初始化样式调整
	this.addEditUploadStyleChange(); // 初始化上传样式调整
  },
  methods: {
    // 下载文件
    download(file){
      window.open(`${file}`); // 新窗口打开文件链接
    },
    // 初始化方法
    init(id,type) {
      if (id) {
        this.id = id; // 设置ID
        this.type = type; // 设置类型
      }
      if(this.type=='info'||this.type=='else'){
        this.info(id); // 查看详情
      }else if(this.type=='cross'){
        var obj = this.$storage.getObj('crossObj'); // 获取跨页面传递的数据
        for (var o in obj){
          	            if(o=='refid'){
            this.ruleForm.refid = obj[o]; // 设置关联表ID
	    this.ro.refid = true; // 设置只读
            continue;
          }
	            	            if(o=='userid'){
            this.ruleForm.userid = obj[o]; // 设置用户ID
	    this.ro.userid = true; // 设置只读
            continue;
          }
	            	            if(o=='content'){
            this.ruleForm.content = obj[o]; // 设置评论内容
	    this.ro.content = true; // 设置只读
            continue;
          }
	            	            if(o=='reply'){
            this.ruleForm.reply = obj[o]; // 设置回复内容
	    this.ro.reply = true; // 设置只读
            continue;
          }
	                    }
                                                                              }
                                                                                                      },
    // 查看详情
    info(id) {
      this.$http({
        url: `discussfangwuxinxi/info/${id}`, // 请求详情接口
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.ruleForm = data.data; // 更新表单数据
        } else {
          this.$message.error(data.msg); // 错误提示
        }
      });
    },
    // 提交表单
    onSubmit() {
                  // ${column.compare}
                              // ${column.compare}
                              // ${column.compare}
                              // ${column.compare}
                                                                                                            this.$refs["ruleForm"].validate(valid => { // 表单验证
        if (valid) {
          this.$http({
            url: `discussfangwuxinxi/${!this.ruleForm.id ? "save" : "update"}`, // 动态请求保存或更新接口
            method: "post",
            data: this.ruleForm // 提交表单数据
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功", // 成功提示
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.parent.showFlag = true; // 显示父组件标志
                  this.parent.addOrUpdateFlag = false; // 关闭当前页面
                  this.parent.discussfangwuxinxiCrossAddOrUpdateFlag = false; // 关闭跨页面标志
                  this.parent.search(); // 刷新父组件数据
                  this.parent.contentStyleChange(); // 刷新父组件样式
                }
              });
            } else {
              this.$message.error(data.msg); // 错误提示
            }
          });
        }
      });
    },
    // 获取UUID
    getUUID () {
      return new Date().getTime(); // 返回当前时间戳
    },
    // 返回上一页
    back() {
      this.parent.showFlag = true; // 显示父组件标志
      this.parent.addOrUpdateFlag = false; // 关闭当前页面
      this.parent.discussfangwuxinxiCrossAddOrUpdateFlag = false; // 关闭跨页面标志
      this.parent.contentStyleChange(); // 刷新父组件样式
    },
    // 动态调整样式
	addEditStyleChange() {
	  this.$nextTick(()=>{
	    // input
	    document.querySelectorAll('.addEdit-block .input .el-input__inner').forEach(el=>{
	      el.style.height = this.addEditForm.inputHeight; // 高度
	      el.style.color = this.addEditForm.inputFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.inputFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.inputBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.inputBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.inputBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.inputBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.inputBgColor; // 背景颜色
	    });
	    document.querySelectorAll('.addEdit-block .input .el-form-item__label').forEach(el=>{
	      el.style.lineHeight = this.addEditForm.inputHeight; // 行高
	      el.style.color = this.addEditForm.inputLableColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.inputLableFontSize; // 字体大小
	    });
	    // select
	    document.querySelectorAll('.addEdit-block .select .el-input__inner').forEach(el=>{
	      el.style.height = this.addEditForm.selectHeight; // 高度
	      el.style.color = this.addEditForm.selectFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.selectFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.selectBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.selectBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.selectBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.selectBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.selectBgColor; // 背景颜色
	    });
	    document.querySelectorAll('.addEdit-block .select .el-form-item__label').forEach(el=>{
	      el.style.lineHeight = this.addEditForm.selectHeight; // 行高
	      el.style.color = this.addEditForm.selectLableColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.selectLableFontSize; // 字体大小
	    });
	    document.querySelectorAll('.addEdit-block .select .el-select__caret').forEach(el=>{
	      el.style.color = this.addEditForm.selectIconFontColor; // 图标颜色
	      el.style.fontSize = this.addEditForm.selectIconFontSize; // 图标大小
	    });
	    // date
	    document.querySelectorAll('.addEdit-block .date .el-input__inner').forEach(el=>{
	      el.style.height = this.addEditForm.dateHeight; // 高度
	      el.style.color = this.addEditForm.dateFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.dateFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.dateBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.dateBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.dateBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.dateBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.dateBgColor; // 背景颜色
	    });
	    document.querySelectorAll('.addEdit-block .date .el-form-item__label').forEach(el=>{
	      el.style.lineHeight = this.addEditForm.dateHeight; // 行高
	      el.style.color = this.addEditForm.dateLableColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.dateLableFontSize; // 字体大小
	    });
	    document.querySelectorAll('.addEdit-block .date .el-input__icon').forEach(el=>{
	      el.style.color = this.addEditForm.dateIconFontColor; // 图标颜色
	      el.style.fontSize = this.addEditForm.dateIconFontSize; // 图标大小
	      el.style.lineHeight = this.addEditForm.dateHeight; // 行高
	    });
	    // upload
	    let iconLineHeight = parseInt(this.addEditForm.uploadHeight) - parseInt(this.addEditForm.uploadBorderWidth) * 2 + 'px'; // 图标行高
	    document.querySelectorAll('.addEdit-block .upload .el-upload--picture-card').forEach(el=>{
	      el.style.width = this.addEditForm.uploadHeight; // 宽度
	      el.style.height = this.addEditForm.uploadHeight; // 高度
	      el.style.borderWidth = this.addEditForm.uploadBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.uploadBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.uploadBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.uploadBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.uploadBgColor; // 背景颜色
	    });
	    document.querySelectorAll('.addEdit-block .upload .el-form-item__label').forEach(el=>{
	      el.style.lineHeight = this.addEditForm.uploadHeight; // 行高
	      el.style.color = this.addEditForm.uploadLableColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.uploadLableFontSize; // 字体大小
	    });
	    document.querySelectorAll('.addEdit-block .upload .el-icon-plus').forEach(el=>{
	      el.style.color = this.addEditForm.uploadIconFontColor; // 图标颜色
	      el.style.fontSize = this.addEditForm.uploadIconFontSize; // 图标大小
	      el.style.lineHeight = iconLineHeight; // 行高
	      el.style.display = 'block'; // 显示方式
	    });
	    // 多文本输入框
	    document.querySelectorAll('.addEdit-block .textarea .el-textarea__inner').forEach(el=>{
	      el.style.height = this.addEditForm.textareaHeight; // 高度
	      el.style.color = this.addEditForm.textareaFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.textareaFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.textareaBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.textareaBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.textareaBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.textareaBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.textareaBgColor; // 背景颜色
	    });
	    document.querySelectorAll('.addEdit-block .textarea .el-form-item__label').forEach(el=>{
	      // el.style.lineHeight = this.addEditForm.textareaHeight; // 行高
	      el.style.color = this.addEditForm.textareaLableColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.textareaLableFontSize; // 字体大小
	    });
	    // 保存按钮
	    document.querySelectorAll('.addEdit-block .btn .btn-success').forEach(el=>{
	      el.style.width = this.addEditForm.btnSaveWidth; // 宽度
	      el.style.height = this.addEditForm.btnSaveHeight; // 高度
	      el.style.color = this.addEditForm.btnSaveFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.btnSaveFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.btnSaveBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.btnSaveBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.btnSaveBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.btnSaveBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.btnSaveBgColor; // 背景颜色
	    });
	    // 返回按钮
	    document.querySelectorAll('.addEdit-block .btn .btn-close').forEach(el=>{
	      el.style.width = this.addEditForm.btnCancelWidth; // 宽度
	      el.style.height = this.addEditForm.btnCancelHeight; // 高度
	      el.style.color = this.addEditForm.btnCancelFontColor; // 字体颜色
	      el.style.fontSize = this.addEditForm.btnCancelFontSize; // 字体大小
	      el.style.borderWidth = this.addEditForm.btnCancelBorderWidth; // 边框宽度
	      el.style.borderStyle = this.addEditForm.btnCancelBorderStyle; // 边框样式
	      el.style.borderColor = this.addEditForm.btnCancelBorderColor; // 边框颜色
	      el.style.borderRadius = this.addEditForm.btnCancelBorderRadius; // 圆角
	      el.style.backgroundColor = this.addEditForm.btnCancelBgColor; // 背景颜色
	    });
	  });
	},
	// 动态调整上传组件样式
	addEditUploadStyleChange() {
		this.$nextTick(()=>{
		  document.querySelectorAll('.addEdit-block .upload .el-upload-list--picture-card .el-upload-list__item').forEach(el=>{
			el.style.width = this.addEditForm.uploadHeight; // 宽度
			el.style.height = this.addEditForm.uploadHeight; // 高度
			el.style.borderWidth = this.addEditForm.uploadBorderWidth; // 边框宽度
			el.style.borderStyle = this.addEditForm.uploadBorderStyle; // 边框样式
			el.style.borderColor = this.addEditForm.uploadBorderColor; // 边框颜色
			el.style.borderRadius = this.addEditForm.uploadBorderRadius; // 圆角
			el.style.backgroundColor = this.addEditForm.uploadBgColor; // 背景颜色
		  });
	  });
	},
  }
};
</script>
<style lang="scss">
.editor{
  height: 500px; // 编辑器高度

  & /deep/ .ql-container {
	  height: 310px; // 编辑器内容区域高度
  }
}
.amap-wrapper {
  width: 100%; // 地图宽度
  height: 500px; // 地图高度
}
.search-box {
  position: absolute; // 搜索框定位
}
.addEdit-block {
	margin: -10px; // 外边距调整
}
.detail-form-content {
	padding: 12px; // 内边距调整
}
.btn .el-button {
  padding: 0; // 按钮内边距调整
}
</style>
