<template>
  <div class="add">
    <div class="title">
      <span class="tips"/>房屋信息
    </div>
    <div class="pad-l32">
      <div class="item">
        <span class="label">房屋面积</span>
        <div class="value">
          <u-input type="digit" placeholder="请输入" v-model="houseInfo.houseArea" input-align="right"
                   placeholder-style="color:#999;font-size:16px;" height="48" :maxlength="7"/>
        </div>
      </div>
      <div class="item">
        <span class="label">居住现状</span>
        <div class="value" @click="selectClick('houselivingStatus', 'livingStatus')">
          <span
              :class="houseInfo.livingStatus === '' ? 'color-999' : ''">{{
              $dict.getLabel('houselivingStatus', houseInfo.livingStatus) || '请选择'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
      <div class="item">
        <span class="label">房屋用途</span>
        <div class="value" @click="selectClick('houseUseStatus', 'houseUse')">
          <span
              :class="houseInfo.houseUse === '' ? 'color-999' : ''">{{
              $dict.getLabel('houseUseStatus', houseInfo.houseUse) || '请选择'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
      <div class="item" v-if="houseInfo.livingStatus == 1">
        <span class="label">承租情况</span>
        <div class="value" @click="selectClick('houseLeaseSituation', 'leaseSituation')">
          <span
              :class="houseInfo.leaseSituation === '' ? 'color-999' : ''">{{
              $dict.getLabel('houseLeaseSituation', houseInfo.leaseSituation) || '请选择'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
      <div class="item" v-if="houseInfo.livingStatus == 1">
        <span class="label">起租日期</span>
        <div class="value" @click="dateShow=true">
          <span :class="houseInfo.startDate === '' ? 'color-999' : ''">{{ houseInfo.startDate || '请选择' }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
      <div class="item" v-if="houseInfo.livingStatus == 1">
        <span class="label">租房备案证明</span>
        <div class="value" @click="selectClick('isFilingCertificateStatus', 'isFilingCertificate')">
          <span
              :class="houseInfo.isFilingCertificate === '' ? 'color-999' : ''">{{
              $dict.getLabel('isFilingCertificateStatus', houseInfo.isFilingCertificate) || '请选择'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
    </div>
    <div class="line-bg"/>
    <div class="title">
      <span class="tips"/>房主信息
      <AiPagePicker class="select" :selected.sync="houseInfo.owner">选择人员</AiPagePicker>
    </div>
    <div class="pad-l32">
      <div class="item" v-for="(item, index) in houseInfo.owner" :key="index">
        <span class="label">{{ item.name }}</span>
        <div class="value" @click="tableRelationSelect('owner', index)">
          <span
              :class=" item.relation === '' ? 'color-999' : ''">{{
              $dict.getLabel('householdRelation', item.relation) || '与户主关系'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
    </div>
    <div class="line-bg"/>
    <div class="title">
      <span class="tips"/>承租人信息
      <AiPagePicker class="select" :selected.sync="houseInfo.renter">选择人员</AiPagePicker>
    </div>
    <div class="pad-l32">
      <div class="item" v-for="(item, index) in houseInfo.renter" :key="index">
        <span class="label">{{ item.name }}</span>
        <div class="value" @click="tableRelationSelect('renter', index)">
          <span
              :class=" item.relation === '' ? 'color-999' : ''">{{
              $dict.getLabel('householdRelation', item.relation) || '与户主关系'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
    </div>
    <div class="line-bg"/>
    <div class="title">
      <span class="tips"/>实际居住人员
      <AiPagePicker class="select" :selected.sync="houseInfo.live">选择人员</AiPagePicker>
    </div>
    <div class="pad-l32">
      <div class="item" v-for="(item, index) in houseInfo.live" :key="index">
        <span class="label">{{ item.name }}</span>
        <div class="value" @click="tableRelationSelect('live', index)">
          <span
              :class=" item.relation === '' ? 'color-999' : ''">{{
              $dict.getLabel('householdRelation', item.relation) || '与户主关系'
            }}</span>
          <u-icon name="arrow-right" color="#cccccc" size="14"/>
        </div>
      </div>
    </div>
    <div class="footer" @click="submit">
      <div class="btn">保存</div>
    </div>

    <u-picker mode="time" v-model="dateShow" :params="deteParams" start-year="2010" @confirm="dateConfirm">请选择
    </u-picker>
    <u-select v-model="showSelect" :list="selectList" label-name="dictName" value-name="dictValue"
              @confirm="confirmSelect"/>
    <u-select v-model="showRelation" :list="$dict.getDict('householdRelation')" label-name="dictName"
              value-name="dictValue" @confirm="confirmRelation"/>
  </div>
</template>

<script>
import {mapActions, mapState} from 'vuex'

export default {

  data() {
    return {
      houseInfo: {},
      showSelect: false,
      selectList: [],
      formName: '',
      dateShow: false,
      deteParams: {year: true, month: true, day: true, hour: false, minute: false, second: false},
      showRelation: false,
      tableName: '',
      tableIndex: '',
      lockSelector: false
    }
  },
  computed: {...mapState(['user'])},
  created() {
    this.$dict.load('householdRelation', 'houselivingStatus', 'houseLeaseSituation', 'houseUseStatus',
        'isFilingCertificateStatus', 'communityBuildingType', 'yesOrNo', 'BulidResidentType').then(() => {
      this.getDetail()
    })
  },
  onShow() {
    document.title = '房屋信息'
  },

  methods: {
    ...mapActions(['selectEnterpriseContact']),
    tableRelationSelect(name, index) {
      this.tableName = name
      this.tableIndex = index
      this.showRelation = true
    },
    confirmRelation(e) {
      this.houseInfo[this.tableName][this.tableIndex].relation = e[0].value
    },
    getDetail() {
      let {id} = this.$route.query
      id && this.$http.post(`app/appcommunityhouseinfo/queryDetailById?id=${id}`).then(res => {
        if (res?.data) {
          if (res.data.startDate) {
            res.data.startDate = res.data.startDate.substring(0, 10)
          }
          this.houseInfo = res.data
        }
      })
    },
    submit() {
      this.$http.post(`app/appcommunityhouseinfo/update`, this.houseInfo).then(res => {
        if (res?.code == 0) {
          this.$u.toast('提交成功')
          setTimeout(() => {
            uni.navigateBack({})
          }, 600)
        }
      })
    },
    selectClick(dictName, formName) {
      this.selectList = this.$dict.getDict(dictName)
      this.formName = formName
      this.showSelect = true
    },
    confirmSelect(e) {
      this.houseInfo[this.formName] = e[0].value
    },
    dateConfirm(e) {
      this.houseInfo.startDate = `${e.year}-${e.month}-${e.day}`
    }
  }
}
</script>

<style lang="scss" scoped>
.add {
  padding-bottom: 112px;

  .title {
    width: 100%;
    height: 96px;
    line-height: 96px;
    background: #FFF;
    font-size: 32px;
    font-weight: 500;
    color: #262B36;
    padding-right: 32px;
    box-sizing: border-box;
    overflow: hidden;

    .tips {
      display: inline-block;
      width: 8px;
      height: 32px;
      background: #4E75FE;
      box-shadow: 0 8px 24px 2px #C9D8FA;
      border-radius: 4px;
      vertical-align: middle;
      margin-right: 22px;
    }

    .select {
      float: right;
      font-size: 30px;
      color: #1365DD;
    }
  }

  .item {
    width: 100%;
    padding: 34px 32px 34px 0;
    background: #FFFFFF;
    border-bottom: 1px solid #D8DDE6;
    display: flex;
    justify-content: space-between;
    font-size: 32px;
    font-family: PingFangSC-Regular, PingFang SC;
    color: #333;
    line-height: 44px;
    box-sizing: border-box;

    .color-999 {
      color: #999;
    }

    .value {
      .u-icon {
        margin-left: 16px;
      }
    }
  }

  .footer {
    width: 100%;
    position: fixed;
    bottom: 0;
    left: 0;
  }

  .btn {
    width: 100%;
    height: 112px;
    line-height: 112px;
    text-align: center;
    background: #1365DD;
    font-size: 32px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: 500;
    color: #FFF;
  }

  .line-bg {
    width: 100%;
    height: 16px;
    background: #F5F5F5;
  }

  .pad-l32 {
    padding-left: 32px;
    background-color: #fff;
  }
}
</style>
