<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-col :span="12">  <!-- 左侧列，宽度为12 -->
        <el-form-item class="input" v-if="type!='info'"  label="房主账号" prop="fangzhuzhanghao">  <!-- 房主账号输入框 -->
          <el-input v-model="ruleForm.fangzhuzhanghao"
                    placeholder="房主账号" clearable  :readonly="ro.fangzhuzhanghao"></el-input>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="房主账号" prop="fangzhuzhanghao">
            <el-input v-model="ruleForm.fangzhuzhanghao"
                      placeholder="房主账号" readonly></el-input>
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">  <!-- 右侧列，宽度为12 -->
        <el-form-item class="input" v-if="type!='info'"  label="密码" prop="mima">  <!-- 密码输入框 -->
          <el-input v-model="ruleForm.mima"
                    placeholder="密码" clearable  :readonly="ro.mima"></el-input>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="密码" prop="mima">
            <el-input v-model="ruleForm.mima"
                      placeholder="密码" readonly></el-input>
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">  <!-- 左侧列，宽度为12 -->
        <el-form-item class="input" v-if="type!='info'"  label="房主姓名" prop="fangzhuxingming">  <!-- 房主姓名输入框 -->
          <el-input v-model="ruleForm.fangzhuxingming"
                    placeholder="房主姓名" clearable  :readonly="ro.fangzhuxingming"></el-input>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="房主姓名" prop="fangzhuxingming">
            <el-input v-model="ruleForm.fangzhuxingming"
                      placeholder="房主姓名" readonly></el-input>
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">  <!-- 右侧列，宽度为12 -->
        <el-form-item class="select" v-if="type!='info'"  label="性别" prop="xingbie">  <!-- 性别选择框 -->
          <el-select v-model="ruleForm.xingbie" placeholder="请选择性别">
            <el-option
                v-for="(item,index) in xingbieOptions"
                v-bind:key="index"
                :label="item"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="性别" prop="xingbie">
            <el-input v-model="ruleForm.xingbie"
                      placeholder="性别" readonly></el-input>
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="24">  <!-- 占满整行 -->
        <el-form-item class="upload" v-if="type!='info' && !ro.touxiang" label="头像" prop="touxiang">  <!-- 头像上传组件 -->
          <file-upload
              tip="点击上传头像"
              action="file/upload"
              :limit="3"
              :multiple="true"
              :fileUrls="ruleForm.touxiang?ruleForm.touxiang:''"
              @change="touxiangUploadChange"
          ></file-upload>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式或只读，显示图片 -->
          <el-form-item v-if="ruleForm.touxiang" label="头像" prop="touxiang">
            <img style="margin-right:20px;" v-bind:key="index" v-for="(item,index) in ruleForm.touxiang.split(',')" :src="item" width="100" height="100">
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">  <!-- 左侧列，宽度为12 -->
        <el-form-item class="input" v-if="type!='info'"  label="手机" prop="shouji">  <!-- 手机输入框 -->
          <el-input v-model="ruleForm.shouji"
                    placeholder="手机" clearable  :readonly="ro.shouji"></el-input>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="手机" prop="shouji">
            <el-input v-model="ruleForm.shouji"
                      placeholder="手机" readonly></el-input>
          </el-form-item>
        </div>
      </el-col>
      <el-col :span="12">  <!-- 右侧列，宽度为12 -->
        <el-form-item class="input" v-if="type!='info'"  label="身份证" prop="shenfenzheng">  <!-- 身份证输入框 -->
          <el-input v-model="ruleForm.shenfenzheng"
                    placeholder="身份证" clearable  :readonly="ro.shenfenzheng"></el-input>
        </el-form-item>
        <div v-else>  <!-- 如果是查看模式，显示只读输入框 -->
          <el-form-item class="input" label="身份证" prop="shenfenzheng">
            <el-input v-model="ruleForm.shenfenzheng"
                      placeholder="身份证" readonly></el-input>
          </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: { /* 样式配置参数 */ },
      id: '',
      type: '',
      ro:{
        // 各字段只读状态配置
        fangzhuzhanghao : false,
        mima : false,
        fangzhuxingming : false,
        xingbie : false,
        touxiang : false,
        shouji : false,
        shenfenzheng : false,
      },
      // 表单数据模型
      ruleForm: {
        fangzhuzhanghao: '',
        mima: '',
        fangzhuxingming: '',
        xingbie: '',
        touxiang: '',
        shouji: '',
        shenfenzheng: '',
      },
      // 性别选项
      xingbieOptions: [],
      // 表单验证规则
      rules: {
        fangzhuzhanghao: [
          { required: true, message: '房主账号不能为空', trigger: 'blur' },  // 必填验证
        ],
        mima: [
          { required: true, message: '密码不能为空', trigger: 'blur' },  // 必填验证
        ],
        fangzhuxingming: [
          { required: true, message: '房主姓名不能为空', trigger: 'blur' },  // 必填验证
        ],
        xingbie: [
        ],
        touxiang: [
        ],
        shouji: [
          { validator: validateMobile, trigger: 'blur' },  // 手机号验证
        ],
        shenfenzheng: [
          { validator: validateIdCard, trigger: 'blur' },  // 身份证验证
        ],
      }
    };
  },
  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=='fangzhuzhanghao'){
            this.ruleForm.fangzhuzhanghao = obj[o];  // 设置房主账号
            this.ro.fangzhuzhanghao = true;  // 设置为只读
            continue;
          }
          if(o=='mima'){
            this.ruleForm.mima = obj[o];  // 设置密码
            this.ro.mima = true;  // 设置为只读
            continue;
          }
          if(o=='fangzhuxingming'){
            this.ruleForm.fangzhuxingming = obj[o];  // 设置房主姓名
            this.ro.fangzhuxingming = true;  // 设置为只读
            continue;
          }
          if(o=='xingbie'){
            this.ruleForm.xingbie = obj[o];  // 设置性别
            this.ro.xingbie = true;  // 设置为只读
            continue;
          }
          if(o=='touxiang'){
            this.ruleForm.touxiang = obj[o];  // 设置头像
            this.ro.touxiang = true;  // 设置为只读
            continue;
          }
          if(o=='shouji'){
            this.ruleForm.shouji = obj[o];  // 设置手机
            this.ro.shouji = true;  // 设置为只读
            continue;
          }
          if(o=='shenfenzheng'){
            this.ruleForm.shenfenzheng = obj[o];  // 设置身份证
            this.ro.shenfenzheng = true;  // 设置为只读
            continue;
          }
        }
      }
      // 获取当前用户信息
      this.$http({
        url: `${this.$storage.get('sessionTable')}/session`,
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          var json = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
      this.xingbieOptions = "男,女".split(',')  // 设置性别选项
    },
    // 获取详细信息方法
    info(id) {
      this.$http({
        url: `fangzhu/info/${id}`,
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.ruleForm = data.data;  // 更新表单数据
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 提交表单方法
    onSubmit() {
      // 表单验证并提交
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: `fangzhu/${!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.fangzhuCrossAddOrUpdateFlag = false;  // 隐藏关联表单页
                  this.parent.search();  // 刷新列表
                  this.parent.contentStyleChange();  // 更新列表样式
                }
              });
            } else {
              this.$message.error(data.msg);  // 显示错误消息
            }
          });
        }
      });
    },
    // 获取唯一标识符方法
    getUUID () {
      return new Date().getTime();
    },
    // 返回方法
    back() {
      this.parent.showFlag = true;  // 显示列表页
      this.parent.addOrUpdateFlag = false;  // 隐藏表单页
      this.parent.fangzhuCrossAddOrUpdateFlag = false;  // 隐藏关联表单页
      this.parent.contentStyleChange();  // 更新列表样式
    },
    // 头像上传变更方法
    touxiangUploadChange(fileUrls) {
      this.ruleForm.touxiang = fileUrls;  // 更新头像数据
      this.addEditUploadStyleChange();  // 更新上传组件样式
    },
    addEditStyleChange() {  // 更新表单样式方法
      this.$nextTick(()=>{  // 在DOM更新后执行
        // 更新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';
        });
        // 更新textarea样式
        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(()=>{  // 在DOM更新后执行
        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%;  <!-- 宽度为100% -->
  height: 500px;  <!-- 高度为500px -->
}
.search-box {  <!-- 搜索按钮 -->
  position: absolute;  <!-- 绝对定位 -->
}
.addEdit-block {  <!-- 表单容器 -->
  margin: -10px;  <!-- 外边距 -->
}
.detail-form-content {  <!-- 表单内容 -->
  padding: 12px;  <!-- 内边距 -->
}
.btn .el-button {  <!-- 按钮样式 -->
  padding: 0;  <!-- 无内边距 -->
}
</style>