<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-rent__rentHouse}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()" label-width="100px">
        <el-form-item label="所属网格"
                      prop="gridId">
          <el-select
                  v-model.trim="dataForm.gridId"
                  placeholder="请选择"
                  clearable
                  @change="handleChangeGrid"
          >
            <el-option
                    v-for="item in optionsG"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属房屋" >
          <div class="resi-cell-value">
            <div class="resi-cell-col">
              <el-form-item prop="villageId">
                <el-select
                        v-model.trim="dataForm.villageId"
                        placeholder="请选择小区"
                        clearable
                        @clear="handleClearVillage"
                        @change="handleChangeV"
                >
                  <el-option
                          v-for="item in optionsV"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="buildId">
                <el-select
                        v-model.trim="dataForm.buildId"
                        placeholder="楼号"
                        clearable
                        style="width: 120px;"
                        :disabled="changeVDisabled"
                        @clear="handleClearBuild"
                        @change="handleChangeB"
                >
                  <el-option
                          v-for="item in optionsB"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="unitId">
                <el-select
                        v-model.trim="dataForm.unitId"
                        :disabled="changeBDisabled"
                        placeholder="单元"
                        clearable
                        style="width: 120px;"
                        @click="handleClearDan"
                        @change="handleChangeD"
                >
                  <el-option
                          v-for="item in optionsD"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item prop="homeId">
                <el-select
                        v-model.trim="dataForm.homeId"
                        :disabled="changeDDisabled"
                        placeholder="房号"
                        clearable
                        style="width: 120px;"
                >
                  <el-option
                          v-for="item in optionsH"
                          :key="item.value"
                          :label="item.label"
                          :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </div>
        </el-form-item>

        <br/>
        <el-form-item label="姓名"
                      prop="name">
          <el-input v-model="dataForm.name"
                    size="small"
                    class="list_item_width_1"
                    clearable
                    placeholder="请输入姓名">
          </el-input>
        </el-form-item>
        <el-form-item label="身份证"
                      prop="idCard">
          <el-input v-model="dataForm.idCard"
                    size="small"
                    class="list_item_width_1"
                    clearable
                    placeholder="请输入身份证">
          </el-input>
        </el-form-item>
        <el-form-item label="手机"
                      prop="mobile">
          <el-input v-model="dataForm.mobile"
                    size="small"
                    class="list_item_width_1"
                    clearable
                    placeholder="请输入手机">
          </el-input>
        </el-form-item>
        <br/>
        <el-form-item label="房屋状态" prop="rentFlag">
          <el-select v-model="dataForm.rentFlag" placeholder="房屋状态" clearable>
            <el-option v-for="item in rentFlagArr" :key="item.dictValue" :label="item.dictName" :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="购房时间"
                      prop="startTime">
          <el-date-picker v-model="timeRange"
                          type="daterange"
                          clearable
                          size="small"
                          range-separator="至"
                          start-placeholder="选择日期"
                          end-placeholder="选择日期"
                          value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button class="diy-button--reset" size="small" @click="resetForm">重置</el-button>
        </el-form-item>
        <br/>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleEdit()">{{ '购房登记' }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" size="small" @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item>
        <!--<el-form-item>-->
          <!--<el-button type="danger" @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>-->
        <!--</el-form-item>-->
      </el-form>
      <el-table v-loading="dataListLoading" :data="dataList" border @selection-change="dataListSelectionChangeHandle" style="width: 100%">
        <el-table-column label="序号"
                         type="index"
                         fixed="left"
                         align="center"
                         width="50">
        </el-table-column>
        <el-table-column prop="villageName" label="所属小区" header-align="center" align="center"></el-table-column>
        <el-table-column prop="buildName" label="所属楼栋" header-align="center" align="center"></el-table-column>
        <el-table-column prop="unitName" label="单元号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="homeName" label="门牌号" header-align="center" align="center"></el-table-column>
        <el-table-column prop="houseType" label="房屋类型" header-align="center" align="center">
          <template slot-scope="scope">
            {{
            scope.row.houseType == '1'
            ? "楼房"
            : scope.row.houseType == '2'
            ? "平房"
            : scope.row.houseType == '3'
            ? "别墅"
            : ""
            }}
          </template>
        </el-table-column>
        <el-table-column prop="purpose" label="用途" header-align="center" align="center">
          <template slot-scope="scope">
            {{
            scope.row.purpose == '1'
            ? "住宅"
            : scope.row.purpose == '2'
            ? "商业"
            : scope.row.purpose == '3'
            ? "办公"
            : scope.row.purpose == '4'
            ? "工业"
            : scope.row.purpose == '5'
            ? "仓储"
            : scope.row.purpose == '6'
            ? "商住混用"
            : scope.row.purpose == '7'
            ? "其他"
            : ""
            }}
          </template>
        </el-table-column>
        <el-table-column prop="rentFlag" label="房屋状态" header-align="center" align="center">
          <template slot-scope="scope">
            {{
            scope.row.rentFlag == 1
            ? "出租"
            : scope.row.rentFlag == 2
            ? "闲置"
            : scope.row.rentFlag == 0
            ? "自住"
            : ""
            }}
          </template>
        </el-table-column>
        <el-table-column prop="ownerName" label="房主姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="ownerPhone" label="房主电话" header-align="center" align="center"></el-table-column>
        <el-table-column prop="ownerIdCard" label="房主身份证号" header-align="center" align="center"></el-table-column>
        <!--<el-table-column prop="purchaseDate" label="购房日期" header-align="center" align="center"></el-table-column>-->
        <el-table-column :label="$t('handle')" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
      <!-- 购房登记弹出框 -->
      <el-dialog :visible.sync="formShow"
                 :close-on-click-modal="false"
                 :close-on-press-escape="false"
                 :title="formTitle"
                 width="900px"
                 top="5vh"
                 class="dialog-h"
                 @closed="diaClose">
        <room-form ref="ref_form"
                   @dialogCancle="addFormCancle"
                   @dialogOk="addFormOk"></room-form>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './renthouse-add-or-update'
import roomForm from './renthouseForm'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/pli/power/rentHouse/page',
        getDataListIsPage: true,
        deleteURL: '/pli/power/rentHouse',
        deleteIsBatch: true,
        exportURL: '/pli/power/rentHouse/export'
      },
      dataForm: {
        id: '',
          name: '',
          mobile: '',
          idCard: '',
          gridId: '',
          villageId: '',
          buildId: '',
          unitId: '',
          homeId: '',
          rentFlag: ''
      },
        timeRange: '',
        optionsV: [],
        optionsB: [],
        optionsH: [],
        optionsD: [],
        optionsG: [],
        rentFlagArr: [
            { dictValue: 0, dictName: '自住' },
            { dictValue: 1, dictName: '出租' },
            { dictValue: 2, dictName: '闲置' }
        ],
        //form相关
        formShow: false,
        formTitle: '购房登记',
    }
  },
  components: {
    AddOrUpdate,
      roomForm
  },
    computed: {
        changeVDisabled() {
            return !this.dataForm.villageId
        },
        changeBDisabled() {
            return !this.dataForm.buildId
        },
        changeDDisabled() {
            return !this.dataForm.unitId
        }
    },
    created () {
        this.getGridList()
        this.getValiheList()
    },
    watch: {
        timeRange (val) {
            if (Array.isArray(val) && val.length == 2) {
                this.dataForm.startTime = val[0];
                this.dataForm.endTime = val[1];
            } else {
                this.dataForm.startTime = '';
                this.dataForm.endTime = '';
            }
        }
    },
    methods: {
        addFormCancle () {
            this.formShow = false
        },
        addFormOk () {
            this.formShow = false
            this.getDataList()
        },
        handleEdit (row) {
            this.formTitle = '购房登记'
            this.formShow = true
            this.$nextTick(() => {
                this.$refs.ref_form.initForm('add', row, '')
            })
        },
        resetForm(formName) {
            for(const n in this.dataForm) {
                this.dataForm[n] = ''
            }
            this.timeRange = ''
            this.getDataList()
        },
        handleClearVillage() {
            this.dataForm.buildId = ''
            this.dataForm.homeId = ''
        },
        handleClearBuild() {
            this.dataForm.buildId = ''
            this.dataForm.unitId = ''
            this.dataForm.homeId = ''
        },
        handleClearDan() {
            this.dataForm.unitId = ''
            this.dataForm.homeId = ''
        },
        handleChangeGrid(val) {
            console.log('val', val)
            this.dataForm.villageId = ''
            this.dataForm.buildId = ''
            this.dataForm.unitId = ''
            this.dataForm.homeId = ''
            this.getValiheList()
        },
        getGridList() {
            const { user } = this.$store.state
            this.$http
                .post('/gov/org/customergrid/gridoption', { agencyId: user.agencyId, purpose: 'query' })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    } else {
                        console.log('获取查询详情成功', res.data)
                        this.optionsG = res.data
                    }
                })
                .catch(() => {
                    return this.$message.error('网络错误')
                })
        },
        getValiheList() {
            const { user } = this.$store.state
            this.$http
                .post('/gov/org/icneighborhood/neighborhoodoption', {
                    gridId: this.dataForm.gridId,
                    agencyId: ''
                    // agencyId: user.agencyId
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    } else {
                        console.log('获取查询详情成功', res.data)
                        this.optionsV = res.data
                    }
                })
                .catch(() => {
                    return this.$message.error('网络错误')
                })
        },
        getBuildList() {
            this.$http
                .post('/gov/org/icbuilding/buildingoption', {
                    neighborHoodId: this.dataForm.villageId
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    } else {
                        console.log('获取查询详情成功', res.data)
                        this.optionsB = res.data
                    }
                })
                .catch(() => {
                    return this.$message.error('网络错误')
                })
        },
        getUniList() {
            this.$http
                .post('/gov/org/icbuildingunit/unitoption', {
                    buildingId: this.dataForm.buildId
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    } else {
                        console.log('获取查询详情成功', res.data)
                        this.optionsD = res.data
                    }
                })
                .catch(() => {
                    return this.$message.error('网络错误')
                })
        },
        getHouseList() {
            this.$http
                .post('/gov/org/ichouse/houseoption', { unitId: this.dataForm.unitId })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg)
                    } else {
                        console.log('获取查询详情成功', res.data)
                        this.optionsH = res.data
                    }
                })
                .catch(() => {
                    return this.$message.error('网络错误')
                })
        },
        handleChangeV(val) {
            console.log('val', val)
            this.dataForm.buildId = ''
            this.dataForm.unitId = ''
            this.dataForm.homeId = ''
            this.getBuildList()
        },
        handleChangeB(val) {
            console.log('val', val)
            this.dataForm.unitId = ''
            this.dataForm.homeId = ''
            this.getUniList()
        },
        handleChangeD(val) {
            console.log('val', val)
            this.dataForm.homeId = ''
            this.getHouseList()
        },
    }
}
</script>
