<template>
    <div class="propertyEditor">
      <ul class="detailUl">
        <li class="detailLi">
          <span class="liName">
            <span style="color: red;margin-right: 5rpx">*</span>
            父级
          </span>
          <div class="inputBox" @tap="showPop('父级')">
            <input placeholder="园区、楼盘、楼栋数" disabled class="myIpt" v-model="buildFa">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            <span style="color: red;margin-right: 5rpx">*</span>
            区域
          </span>
          <div class="inputBox" @tap="showArea('区域')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.region">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            <span style="color: red;margin-right: 5rpx">*</span>
            楼层
          </span>
          <div class="inputBox" @tap="showArea('楼层')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.floor">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            <span style="color: red;margin-right: 5rpx">*</span>
            出租单元名称
          </span>
           <input placeholder="请填写" class="myIpt" style="padding-right: 30rpx" v-model="postData.roomNo">
        </li>
        <li class="detailLi">
          <span class="liName">
            平面图号
          </span>
           <input placeholder="请填写" class="myIpt" style="padding-right: 30rpx" v-model="postData.imgNo">
        </li>
        <li class="detailLi">
          <span class="liName">
          <span style="color: red;margin-right: 5rpx">*</span>
            面积（㎡）
          </span>
           <input placeholder="请填写" class="myIpt" v-model="postData.size" style="padding-right: 30rpx">
        </li>
        <li class="detailLi">
          <span class="liName">
          <span style="color: red;margin-right: 5rpx">*</span>
            用途
          </span>
          <div class="inputBox" @tap="showArea('用途')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.roomUseLabel">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            物业性质
          </span>
          <div class="inputBox" @tap="showArea('物业性质')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.propertyNatureLabel">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
          <span style="color: red;margin-right: 5rpx">*</span>
            朝向
          </span>
          <div class="inputBox" @tap="showArea('朝向')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.direction">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
          <span style="color: red;margin-right: 5rpx">*</span>
            装修情况
          </span>
          <div class="inputBox" @tap="showArea('装修情况')">
            <input placeholder="请选择" disabled class="myIpt" v-model="postData.decorationSituation">
            <van-icon name="arrow-down" />
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            层高
          </span>
           <input placeholder="请填写" class="myIpt" style="padding-right: 30rpx" v-model="postData.floorHeight">
        </li>
        <li class="detailLi2" >
          <div class="titleBox" >
            <span class="liName">承重情况</span>
          </div>
          <van-field
              autosize
              :value="postData.bearing"
              type="textarea"
              placeholder="请输入"
              class="myField"
              maxlength="1000"
              show-word-limit
              @change="changeBearing"
          />
        </li>
        <li class="detailLi2" >
          <div class="titleBox" >
            <span class="liName">用电说明</span>
          </div>
          <van-field
              :value="postData.electricity"
              autosize
              type="textarea"
              placeholder="请输入"
              class="myField"
              maxlength="1000"
              show-word-limit
              @change="changeElectricity"
          />
        </li>
        <li class="detailLi2" >
          <div class="titleBox" >
            <span style="color: red;margin-right: 5rpx">*</span>
            <span class="liName">出租指导价（元/㎡/月）</span>
          </div>
          <van-field
              :value="postData.guidePrice"
              autosize
              type="textarea"
              placeholder="请输入"
              class="myField"
              @change="changeGuidePrice"
          />
        </li>
        <li class="detailLi2" >
          <div class="titleBox" >
            <span class="liName">备注</span>
          </div>
          <van-field
              :value="postData.remark"
              @change="changeRemark"
              autosize
              type="textarea"
              placeholder="请输入"
              class="myField"
              maxlength="1000"
              show-word-limit
          />
        </li>
        <li class="detailLi2" >
          <div class="titleBox" style="margin-bottom: 24rpx">
            <span class="liName">
              房型图
               <span style="color: #B3B3B3;font-size: 28rpx">（只能上传jpg/png格式，限制上传9张）</span>
            </span>
          </div>
          <van-uploader
              :max-count="9"
              @delete="deleteRYXXZP"
              :file-list="fileList"
              @after-read="uploadRYXXZP"
              :show-upload="true"
          />
        </li>
        <li class="detailLi2" >
          <div class="titleBox" style="margin-bottom: 24rpx">
            <span class="liName">
              室内外环境
            <span style="color: #B3B3B3;font-size: 28rpx">（只能上传jpg/png格式，限制上传6张）</span>
            </span>
          </div>
          <van-uploader
              :max-count="6"
              @delete="deleteRYXXZP2"
              :file-list="fileList2"
              @after-read="uploadRYXXZP2"
              :show-upload="true"
          />
        </li>
        <li class="detailLi2" style="position:relative;">
          <div class="titleBox" >
            <span class="liName">出租位置</span>
          </div>
          <van-field
              :value="postData.address"
              @change="changeAddress"
              autosize
              type="textarea"
              placeholder="请选择地址"
              class="myField"
              style="padding-right: 120rpx;box-sizing: border-box"
          />
          <div class="locationBox" @tap="showMap">
            <van-icon name="location" />
            地图
          </div>
        </li>
        <li class="detailLi">
          <span class="liName">
            当前可用状态
          </span>
        <div class="inputBox" @tap="showArea('可用状态')">
          <input placeholder="请选择" disabled class="myIpt" v-model="postData.enableLabel">
          <van-icon name="arrow-down" />
        </div>
        </li>
        <li class="detailLi" style="border-bottom: none">
          <span class="liName">
            租赁状态
          </span>
        <div class="inputBox" @tap="showArea('租赁状态')">
          <input placeholder="请选择" disabled class="myIpt" v-model="postData.stateLabel">
          <van-icon name="arrow-down" />
        </div>
        </li>
      </ul>
      <button class="commitBtn" @tap="commit">保存</button>
      <van-popup
        :show="showTree"
         round position="bottom"
        custom-style="height:50%;"
      >
        <van-cascader
            :value="cascaderValue"
            title="请选择所在地区"
            :options="options"
            @close="showTree = false"
            @finish="onFinish"
            :field-names="fieldNames"
        />
      </van-popup>
      <van-popup
          :show="show"
          position="bottom"
          custom-style="height: 50%;"
      >
        <van-picker v-show="popType==='区域'" :columns="areaList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='楼层'" :columns="floorList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='用途'" :columns="usegeList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='物业性质'" :columns="natureList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='朝向'" :columns="cxList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='装修情况'" :columns="situationList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='可用状态'" :columns="enableList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
        <van-picker v-show="popType==='租赁状态'" :columns="statusList" show-toolbar @cancel="show=false" @confirm="onConfirm" value-key="label"/>
      </van-popup>
    </div>
</template>

<script>
import {
  getTreeData2,
  parkRoomGetById,
  getByCodes,
  parkRoomEdit
} from "@/js_sdk/http";
export default {
  name: "propertyEditor",
  data(){
    return{
      dic_key: [
        "MNP_BUILDING_LCQY",
        "MNP_BUILDING_LC",
        "HOUSE_USAGE",
        "PROPERTY_NATURE",
        "MNP_BUILDING_CX",
        "DECORATION_SITUATION",
        "HOUSE_ENABLE",
        "HOUSE_STATUS"
      ],
      fileList: [],
      fileList2: [],
      buildFa:'',
      show:false,
      showTree:false,
      cascaderValue: null,
      fieldNames: {
        text: 'label',
        value: 'value',
        children: 'children',
      },
      options:[

      ],
      areaList:[],
      floorList:[],
      usegeList:[],
      natureList:[],
      enableList:[],
      cxList:[],
      situationList:[],
      statusList:[],
      popType:'',
      postData:{
        groupName:'',
        floorDiscName: '',
        buildName: '',
        roomNo: '',
        region: '',
        size: '',
        imgNo: '',
        floor:'',
        roomUse:'',
        roomUseLabel:'',
        propertyNature:'',
        direction:'',
        decorationSituation:'',
        floorHeight:'',
        bearing:'',
        electricity:'',
        guidePrice:'',
        remark:'',
        environmentPicture:'',
        planPicture:'',
        address:'',
      }
    }
  },
  onLoad(options){
    console.log('id',options.id)
    this.getTree()
    if (options.id){
      this.getByCodes(options.id)
    }
  },
  methods:{
    changeBearing(e){
      this.postData.bearing = e.detail
    },
    changeElectricity(e){
      this.postData.electricity = e.detail
    },
    changeGuidePrice(e){
      this.postData.guidePrice = e.detail
    },
    changeRemark(e){
      this.postData.remark = e.detail
    },
    changeAddress(e){
      this.postData.address = e.detail
    },
    showMap(){
      const _this = this
      uni.chooseLocation({
        success: function (res) {
          console.log('选择的位置：', res.name);
          _this.postData.address = res.name
          console.log('纬度：' + res.latitude + '，经度：' + res.longitude);
          _this.postData.latitude = res.latitude
          _this.postData.longitude = res.longitude
          _this.$forceUpdate();
          // 其他业务逻辑
        },
        fail: function (error) {
          console.error('Choose location failed: ' + JSON.stringify(error));
        },
        complete: function () {
          console.log('chooseLocation operation is complete');
        }
      });
    },
    getTree(){
      getTreeData2().then(res=>{
        console.log(res.data)
        this.options = res.data
      })
    },
    commit(){
      if (this.fileList.length > 0){
        let pictureArr = []
        this.fileList.forEach(e=>{
          pictureArr.push(e.id)
        })
        this.postData.planPicture = pictureArr.toString()
      }

      if (this.fileList2.length > 0){
        let pictureArr2 = []
        this.fileList2.forEach(e=>{
          pictureArr2.push(e.id)
        })
        this.postData.environmentPicture = pictureArr2.toString()
      }
      if (!this.buildFa){
        this.$showToast('请选择父级')
        return
      }
      if (!this.postData.region){
        this.$showToast('请选择区域')
        return
      }
      if (!this.postData.floor){
        this.$showToast('请选择楼层')
        return
      }
      if (!this.postData.roomNo){
        this.$showToast('请输入出租单元名称')
        return
      }
      if (!this.postData.size){
        this.$showToast('请输入面积')
        return
      }
      if (!this.postData.roomUseLabel){
        this.$showToast('请选择用途')
        return
      }
      if (!this.postData.direction){
        this.$showToast('请选择朝向')
        return
      }
      if (!this.postData.decorationSituation){
        this.$showToast('请选择装修情况')
        return
      }
      if (!this.postData.guidePrice){
        this.$showToast('请填写出租指导价')
        return
      }
      parkRoomEdit(this.postData).then(res=>{
        if (res.code == 200){
          this.$showToast('保存成功')
        }
      })
    },
    async getByCodes(id) {
      let data = await getByCodes(JSON.stringify(this.dic_key));
      this.dic_SelectList = this.$common.handleDicList(data);
      this.areaList = this.dic_SelectList.MNP_BUILDING_LCQY
      this.floorList = this.dic_SelectList.MNP_BUILDING_LC
      this.usegeList = this.dic_SelectList.HOUSE_USAGE
      this.natureList = this.dic_SelectList.PROPERTY_NATURE
      this.cxList = this.dic_SelectList.MNP_BUILDING_CX
      this.situationList = this.dic_SelectList.DECORATION_SITUATION
      this.enableList = this.dic_SelectList.HOUSE_ENABLE
      this.statusList = this.dic_SelectList.HOUSE_STATUS
      parkRoomGetById(id).then(res=>{
        this.postData = res
        this.buildFa = res.groupName + '/' + res.floorDiscName+'/' + res.buildName
        this.dataCheck(res)
      })
    },
    showPop(e){
      this.showTree = true
    },
    showArea(e){
      this.popType = e
      this.show = true
    },
    onFinish(e){
      this.postData.groupName = e.detail.selectedOptions[0].label
      this.postData.floorDiscName = e.detail.selectedOptions[1].label
      this.postData.buildName = e.detail.selectedOptions[2].label
      console.log(e)
      const fieldValue = e.detail.selectedOptions
          .map((option) => option.label)
          .join('/');
      this.buildFa = fieldValue
      this.showTree = false
    },
    onConfirm(e){
      console.log(e)
      switch (this.popType) {
        case "区域":
          this.postData.region = e.detail.value.label
          break;
        case "楼层":
          this.postData.floor = e.detail.value.label
          break;
        case "用途":
          this.postData.roomUse = e.detail.value.value
          this.postData.roomUseLabel = e.detail.value.label
          break;
        case "物业性质":
          this.postData.propertyNature = e.detail.value.value
          this.postData.propertyNatureLabel = e.detail.value.label
          break;
        case "朝向":
          this.postData.direction = e.detail.value.label
          break;
        case "装修情况":
          this.postData.decorationSituation = e.detail.value.label
          break;
        case "可用状态":
          this.postData.isEnable = e.detail.value.value
          this.postData.enableLabel = e.detail.value.label
          break;
        case "租赁状态":
          this.postData.state = e.detail.value.value
          this.postData.stateLabel = e.detail.value.label
          break;
      }
      this.show = false
    },
    deleteRYXXZP(event) {
      this.fileList.splice(event.detail.index, 1);
      this.$forceUpdate();
    },
    deleteRYXXZP2(event) {
      this.fileList2.splice(event.detail.index, 1);
      this.$forceUpdate();
    },
    uploadRYXXZP(event) {
      console.log(event)
      let that = this;
      const { file } = event.detail;
      uni.uploadFile({
        url: that.$constant.BASE_URI + "/wx/fileController/upload",
        filePath: file.url,
        name: "file",
        formData: { user: "test" },
        success(res) {
          // 上传完成需要更新 fileList
          let data = JSON.parse(res.data);
          that.fileList.push({
            imgUrl: "/FileController/download/" + data.data[0],
            id: data.data[0],
            url:
                that.$constant.BASE_URI +
                "/FileController/download/" +
                data.data[0],
            isImage: true,
          });
        },
        fail(res) {},
      });
    },
    uploadRYXXZP2(event) {
      console.log(event)
      let that = this;
      const { file } = event.detail;
      uni.uploadFile({
        url: that.$constant.BASE_URI + "/wx/fileController/upload",
        filePath: file.url,
        name: "file",
        formData: { user: "test" },
        success(res) {
          // 上传完成需要更新 fileList
          let data = JSON.parse(res.data);
          that.fileList2.push({
            imgUrl: "/FileController/download/" + data.data[0],
            id: data.data[0],
            url:
                that.$constant.BASE_URI +
                "/FileController/download/" +
                data.data[0],
            isImage: true,
          });
        },
        fail(res) {},
      });
    },
    dataCheck(res){
      this.postData.roomUseLabel = this.usegeList.find(e=>{
        return e.value === res.roomUse

      }).label
      this.postData.propertyNatureLabel = this.natureList.find(e=>{
        return e.value === res.propertyNature

      }).label
      this.postData.enableLabel = this.enableList.find(e=>{
        return e.value === res.isEnable

      }).label
      this.postData.stateLabel = this.statusList.find(e=>{
        return e.value === res.state

      }).label
      const trimmedString = res.environmentPicture.replace(/,$/, '');
      const trimmedString2 = res.planPicture.replace(/,$/, '');
      let environmentList = trimmedString.split(',')
      let planPictureList = trimmedString2.split(',')
      environmentList.forEach(id=>{
        this.fileList2.push({
          imgUrl: "/FileController/download/" + id,
          id: id,
          url:
              this.$constant.BASE_URI +
              "/FileController/download/" +
              id,
          isImage: true,
        });
      })
      planPictureList.forEach(id=>{
        this.fileList.push({
          imgUrl: "/FileController/download/" + id,
          id: id,
          url:
              this.$constant.BASE_URI +
              "/FileController/download/" +
              id,
          isImage: true,
        });
      })
    },
  }
}
</script>

<style lang="scss">
.propertyEditor{
  padding-bottom: 150rpx;
  .detailUl{
    margin-top: 20rpx;
    background: white;
    padding: 24rpx 32rpx;
    .liName{
      font-size: 32rpx;
      color: rgba(51, 51, 51, 1);
      white-space: nowrap;
      .callNumber{
        color: rgba(24, 23, 42, 1);
        font-size: 36rpx;
        font-weight: 600;
      }
      .callName{
        color: rgba(102, 102, 102, 1);
        font-size: 28rpx;
      }
    }
    .phoneIcon{
      width: 64rpx;
      height: 64rpx;
    }
    .detailLi{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 32rpx 0;
      border-bottom: 2rpx solid #E6E6E6;
      .liInfo{
        display: inline-block;
        text-align: right;
        max-width: 428rpx;
        color: rgba(102, 102, 102, 1);
        font-size: 32rpx;
        line-height: 38rpx;
        overflow: hidden;
        word-wrap: break-word;
      }
      .inputBox{
        display: flex;
        align-items: center;
      }
      .myIpt{
        text-align: right;
        margin-right: 20rpx;
        color: rgba(102, 102, 102, 1);
      }
    }
    .detailLi2{
      display: flex;
      flex-direction: column;
      padding: 32rpx 0;
      border-bottom: 2rpx solid #E6E6E6;
      .detailLi2BoxTop{
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        margin: 12rpx 0;
        .checkItem{
          width: 128rpx;
          height: 64rpx;
          font-size: 28rpx;
          color: #666666;
          display: flex;
          align-items: center;
          justify-content: center;
          background: #F5F7FA;
          border-radius: 8rpx;
          margin: 12rpx 0;
        }
        .checked{
          background: #0365F9;
          color: white;
        }
      }
      .locationBox{
        position: absolute;
        right: 24rpx;
        top: 100rpx;
      }
      .van-cell {
        background: #F5F7FA !important;
        border-radius: 8rpx;
        font-size: 28rpx;
        margin-top: 16rpx;
      }
    }
  }
  .commitBtn{
    width: 654rpx;
    height: 96rpx;
    background: rgba(3, 101, 249, 1);
    margin: 64rpx 48rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 36rpx;
    color: white;
  }
}
</style>
