<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">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="房屋名称" prop="fangwumingcheng">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.fangwumingcheng"
                      placeholder="房屋名称" clearable  :readonly="ro.fangwumingcheng"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="房屋名称" prop="fangwumingcheng">
              <el-input v-model="ruleForm.fangwumingcheng"
                        placeholder="房屋名称" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="select" v-if="type!='info'"  label="房屋类型" prop="fangwuleixing">
            <!-- 定义下拉选择框 -->
            <el-select v-model="ruleForm.fangwuleixing" placeholder="请选择房屋类型">
              <el-option
                  v-for="(item,index) in fangwuleixingOptions"
                  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="fangwuleixing">
              <el-input v-model="ruleForm.fangwuleixing"
                        placeholder="房屋类型" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="24">
          <!-- 定义表单项 -->
          <el-form-item class="upload" v-if="type!='info' && !ro.tupian" label="图片" prop="tupian">
            <!-- 定义文件上传组件 -->
            <file-upload
                tip="点击上传图片"
                action="file/upload"
                :limit="3"
                :multiple="true"
                :fileUrls="ruleForm.tupian?ruleForm.tupian:''"
                @change="tupianUploadChange"
            ></file-upload>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item v-if="ruleForm.tupian" label="图片" prop="tupian">
              <!-- 定义图片展示 -->
              <img style="margin-right:20px;" v-bind:key="index" v-for="(item,index) in ruleForm.tupian.split(',')" :src="item" width="100" height="100">
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="select" v-if="type!='info'"  label="租赁方式" prop="zulinfangshi">
            <!-- 定义下拉选择框 -->
            <el-select v-model="ruleForm.zulinfangshi" placeholder="请选择租赁方式">
              <el-option
                  v-for="(item,index) in zulinfangshiOptions"
                  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="zulinfangshi">
              <el-input v-model="ruleForm.zulinfangshi"
                        placeholder="租赁方式" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="朝向楼层" prop="chaoxianglouceng">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.chaoxianglouceng"
                      placeholder="朝向楼层" clearable  :readonly="ro.chaoxianglouceng"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="朝向楼层" prop="chaoxianglouceng">
              <el-input v-model="ruleForm.chaoxianglouceng"
                        placeholder="朝向楼层" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="小区" prop="xiaoqu">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.xiaoqu"
                      placeholder="小区" clearable  :readonly="ro.xiaoqu"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="小区" prop="xiaoqu">
              <el-input v-model="ruleForm.xiaoqu"
                        placeholder="小区" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="面积" prop="mianji">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.mianji"
                      placeholder="面积" clearable  :readonly="ro.mianji"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="面积" prop="mianji">
              <el-input v-model="ruleForm.mianji"
                        placeholder="面积" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="月租价格" prop="yuezujiage">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.yuezujiage"
                      placeholder="月租价格" clearable  :readonly="ro.yuezujiage"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="月租价格" prop="yuezujiage">
              <el-input v-model="ruleForm.yuezujiage"
                        placeholder="月租价格" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="date" v-if="type!='info'" label="发布日期" prop="faburiqi">
            <!-- 定义日期选择器 -->
            <el-date-picker
                format="yyyy 年 MM 月 dd 日"
                value-format="yyyy-MM-dd"
                v-model="ruleForm.faburiqi"
                type="date"
                placeholder="发布日期">
            </el-date-picker>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" v-if="ruleForm.faburiqi" label="发布日期" prop="faburiqi">
              <el-input v-model="ruleForm.faburiqi"
                        placeholder="发布日期" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="用户名" prop="yonghuming">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.yonghuming"
                      placeholder="用户名" clearable  :readonly="ro.yonghuming"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="用户名" prop="yonghuming">
              <el-input v-model="ruleForm.yonghuming"
                        placeholder="用户名" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
        <!-- 定义表单的列布局 -->
        <el-col :span="12">
          <!-- 定义表单项 -->
          <el-form-item class="input" v-if="type!='info'"  label="联系电话" prop="lianxidianhua">
            <!-- 定义输入框 -->
            <el-input v-model="ruleForm.lianxidianhua"
                      placeholder="联系电话" clearable  :readonly="ro.lianxidianhua"></el-input>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item class="input" label="联系电话" prop="lianxidianhua">
              <el-input v-model="ruleForm.lianxidianhua"
                        placeholder="联系电话" readonly></el-input>
            </el-form-item>
          </div>
        </el-col>
      </el-row>
      <!-- 定义表单的行布局 -->
      <el-row>
        <!-- 定义表单的列布局 -->
        <el-col :span="24">
          <!-- 定义表单项 -->
          <el-form-item v-if="type!='info'"  label="房屋详情" prop="fangwuxiangqing">
            <!-- 定义富文本编辑器 -->
            <editor
                style="min-width: 200px; max-width: 600px;"
                v-model="ruleForm.fangwuxiangqing"
                class="editor"
                action="file/upload">
            </editor>
          </el-form-item>
          <!-- 定义只读显示 -->
          <div v-else>
            <el-form-item v-if="ruleForm.fangwuxiangqing" label="房屋详情" prop="fangwuxiangqing">
              <!-- 显示内容 -->
              <span v-html="ruleForm.fangwuxiangqing"></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:{
        fangwumingcheng : false,
        fangwuleixing : false,
        tupian : false,
        zulinfangshi : false,
        chaoxianglouceng : false,
        xiaoqu : false,
        mianji : false,
        yuezujiage : false,
        fangwuxiangqing : false,
        faburiqi : false,
        yonghuming : false,
        lianxidianhua : false,
        sfsh : false,
        shhf : false,
      },
      // 定义表单数据
      ruleForm: {
        fangwumingcheng: '',
        fangwuleixing: '',
        tupian: '',
        zulinfangshi: '',
        chaoxianglouceng: '',
        xiaoqu: '',
        mianji: '',
        yuezujiage: '',
        fangwuxiangqing: '',
        faburiqi: '',
        yonghuming: '',
        lianxidianhua: '',
        shhf: '',
      },
      // 定义下拉选项
      fangwuleixingOptions: [],
      zulinfangshiOptions: [],
      // 定义表单验证规则
      rules: {
        fangwumingcheng: [
          { required: true, message: '房屋名称不能为空', trigger: 'blur' },
        ],
        fangwuleixing: [
          { required: true, message: '房屋类型不能为空', trigger: 'blur' },
        ],
        tupian: [
          { required: true, message: '图片不能为空', trigger: 'blur' },
        ],
        zulinfangshi: [
          { required: true, message: '租赁方式不能为空', trigger: 'blur' },
        ],
        chaoxianglouceng: [
        ],
        xiaoqu: [
        ],
        mianji: [
        ],
        yuezujiage: [
          { validator: validateIntNumber, trigger: 'blur' },
        ],
        fangwuxiangqing: [
        ],
        faburiqi: [
        ],
        yonghuming: [
        ],
        lianxidianhua: [
        ],
        sfsh: [
        ],
        shhf: [
        ],
      }
    };
  },
  // 定义父组件传值
  props: ["parent"],
  // 定义生命周期钩子
  created() {
    // 调用样式初始化方法
    this.addEditStyleChange()
    // 调用上传组件样式初始化方法
    this.addEditUploadStyleChange()
  },
  // 定义方法
  methods: {
    // 定义下载方法
    download(file){
      window.open(`${file}`)
    },
    // 定义初始化方法
    init(id,type) {
      if (id) {
        this.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=='fangwumingcheng'){
            this.ruleForm.fangwumingcheng = obj[o];
            this.ro.fangwumingcheng = true;
            continue;
          }
          if(o=='fangwuleixing'){
            this.ruleForm.fangwuleixing = obj[o];
            this.ro.fangwuleixing = true;
            continue;
          }
          if(o=='tupian'){
            this.ruleForm.tupian = obj[o];
            this.ro.tupian = true;
            continue;
          }
          if(o=='zulinfangshi'){
            this.ruleForm.zulinfangshi = obj[o];
            this.ro.zulinfangshi = true;
            continue;
          }
          if(o=='chaoxianglouceng'){
            this.ruleForm.chaoxianglouceng = obj[o];
            this.ro.chaoxianglouceng = true;
            continue;
          }
          if(o=='xiaoqu'){
            this.ruleForm.xiaoqu = obj[o];
            this.ro.xiaoqu = true;
            continue;
          }
          if(o=='mianji'){
            this.ruleForm.mianji = obj[o];
            this.ro.mianji = true;
            continue;
          }
          if(o=='yuezujiage'){
            this.ruleForm.yuezujiage = obj[o];
            this.ro.yuezujiage = true;
            continue;
          }
          if(o=='fangwuxiangqing'){
            this.ruleForm.fangwuxiangqing = obj[o];
            this.ro.fangwuxiangqing = true;
            continue;
          }
          if(o=='faburiqi'){
            this.ruleForm.faburiqi = obj[o];
            this.ro.faburiqi = true;
            continue;
          }
          if(o=='yonghuming'){
            this.ruleForm.yonghuming = obj[o];
            this.ro.yonghuming = true;
            continue;
          }
          if(o=='lianxidianhua'){
            this.ruleForm.lianxidianhua = obj[o];
            this.ro.lianxidianhua = true;
            continue;
          }
        }
      }
      // 获取用户信息
      this.$http({
        url: `${this.$storage.get('sessionTable')}/session`,
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          var json = data.data;
          if(json.yonghuming!=''&&json.yonghuming){
            this.ruleForm.yonghuming = json.yonghuming
          }
          if(json.lianxidianhua!=''&&json.lianxidianhua){
            this.ruleForm.lianxidianhua = json.lianxidianhua
          }
        } else {
          this.$message.error(data.msg);
        }
      });
      // 初始化下拉选项
      this.$http({
        url: `option/fangwuleixing/fangwuleixing`,
        method: "get"
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.fangwuleixingOptions = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
      // 初始化下拉选项
      this.zulinfangshiOptions = "整租,合租".split(',')
    },
    // 定义获取详情方法
    info(id) {
      this.$http({
        url: `woyaodangfangzhu/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: `woyaodangfangzhu/${!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.woyaodangfangzhuCrossAddOrUpdateFlag = 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.woyaodangfangzhuCrossAddOrUpdateFlag = false;
      this.parent.contentStyleChange();
    },
    // 定义文件上传变化方法
    tupianUploadChange(fileUrls) {
      this.ruleForm.tupian = fileUrls;
      this.addEditUploadStyleChange()
    },
    // 定义样式初始化方法
    addEditStyleChange() {
      this.$nextTick(()=>{})
    },
    // 定义上传组件样式初始化方法
    addEditUploadStyleChange() {
      this.$nextTick(()=>{})
    },
  }
};
</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>