<template>
  <div class="g-main">
    <div ref="searchForm" class="m-search">
      <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
            class="u-item-width-normal"
            @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="迁往何地">
          <el-select
            v-model.trim="dataForm.villageId"
            placeholder="请选择小区"
            clearable
            class="u-item-width-communitycascader"
            @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-select
            v-model.trim="dataForm.buildId"
            placeholder="楼号"
            clearable
            class="u-item-width-buildcascader"
            style="margin-left: 5px"
            :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-select
            v-model.trim="dataForm.unitId"
            :disabled="changeBDisabled"
            placeholder="单元"
            clearable
            class="u-item-width-buildcascader"
            style="margin-left: 5px"
            @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-select
            v-model.trim="dataForm.homeId"
            :disabled="changeDDisabled"
            placeholder="房号"
            clearable
            class="u-item-width-buildcascader"
            style="margin-left: 5px"
          >
            <el-option
              v-for="item in optionsH"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="dataForm.name"
            size="small"
            class="u-item-width-normal"
            clearable
            placeholder="请输入姓名"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="证件号" prop="idCard">
          <el-input
            v-model="dataForm.idCard"
            size="small"
            class="u-item-width-normal"
            clearable
            placeholder="请输入证件号"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input
            v-model="dataForm.mobile"
            size="small"
            class="u-item-width-normal"
            clearable
            placeholder="请输入手机"
          >
          </el-input>
        </el-form-item>

        <el-form-item label="迁出时间" prop="startTime">
          <el-date-picker
            v-model="timeRange"
            type="daterange"
            clearable
            size="small"
            class="u-item-width-daterange2"
            range-separator="至"
            start-placeholder="选择日期"
            end-placeholder="选择日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="享受福利"
                      prop="isWelfare">
          <el-select v-model="dataForm.isWelfare"
                     placeholder="请选择"
                     class="u-item-width-normal"
                     clearable>
            <el-option v-for="item in isWelfareArr"
                       :key="item.dictValue"
                       :label="item.dictName"
                       :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item> -->

        <div class="u-search-btn">
          <el-button class="diy-button--white" size="small" @click="resetForm"
            >重置</el-button
          >
          <el-button
            style="margin-left: 10px"
            size="small"
            class="diy-button--blue"
            @click="getDataList"
            >查询</el-button
          >
        </div>
      </el-form>
    </div>

    <div class="m-table">
      <div class="u-table-btn1">
        <el-button
          size="small"
          class="diy-button--white"
          @click="exportHandle()"
          >{{ $t("export") }}</el-button
        >
      </div>

      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        :height="tableHeight"
        @selection-change="dataListSelectionChangeHandle"
        class="m-table-item"
      >
        <!--<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->
        <el-table-column
          label="序号"
          type="index"
          fixed="left"
          align="center"
          width="50"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          prop="oleDeptName"
          label="所属网格"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="oldAddress"
          label="所属房屋"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="ownerName"
          label="户主姓名"
          header-align="center"
          align="center"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="手机号"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="idCard"
          label="证件号"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="gender"
          label="性别"
          header-align="center"
          align="center"
          min-width="70"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ scope.row.gender == "2" ? "女" : scope.row.gender }}
          </template>
        </el-table-column>
        <el-table-column
          prop="age"
          label="年龄"
          header-align="center"
          align="center"
          min-width="90"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="outOfTime"
          label="迁出时间"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="reason"
          label="迁出原因"
          header-align="center"
          align="center"
          min-width="100"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          prop="address"
          label="迁往何地"
          header-align="center"
          align="center"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="180">
          <template slot-scope="scope">
            <el-button
              @click="handleLook(scope.row)"
              type="text"
              size="small"
              class="btn-color-look"
              >查看</el-button
            >

            <el-button
              v-if="false"
              @click="handleChangeRecord(scope.row)"
              type="text"
              size="small"
              class="div-table-button--blue"
              >变更记录</el-button
            >
            <el-popconfirm
              style="margin-left: 10px"
              v-if="scope.row.status !== '0'"
              title="确认恢复？"
              @onConfirm="handleRecovery(scope.row)"
              @confirm="handleRecovery(scope.row)"
            >
              <el-button
                slot="reference"
                type="text"
                size="small"
                class="div-table-button--blue"
                >恢复</el-button
              >
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="pageNo"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
    </div>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>

    <!-- 修改弹出框 -->
    <el-dialog
      :visible.sync="showedResiInfo"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="居民详情"
      width="986px"
      top="5vh"
      class="m-dialog"
      @closed="showedResiInfo = false"
    >
      <div class="dialog-h-content scroll-h">
        <resi-info
          v-if="showedResiInfo && lookInfo.resiId"
          :resiId="lookInfo.resiId"
          @close="handleCancleLook"
        />
      </div>
      <div class="resi-btns">
        <el-button
          size="small"
          @click="showedResiInfo = false"
          class="diy-button--common"
          >关闭</el-button
        >
      </div>
    </el-dialog>

    <!-- 变更记录 -->
    <el-dialog
      :visible.sync="changeRecordShow"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="'变更记录'"
      width="1150px"
      top="5vh"
      @closed="diaClose"
    >
      <resi-change-record ref="ref_changerecord"></resi-change-record>
    </el-dialog>
  </div>
</template>

<script>
import mixinViewModule from "@/mixins/view-module2";
import AddOrUpdate from "./changerelocation-add-or-update";
import nextTick from "dai-js/tools/nextTick";
import resiChangeRecord from "../../../components/resiChangeRecord.vue";
import { requestPost } from "@/js/dai/request";
import { mapGetters } from "vuex";
import resiInfo from "@/views/components/resiInfo.vue";

export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/actual/base/residentMoveOutRecord/page",
        getDataListIsPage: true,
        deleteURL: "/epmetuser/changeRelocation",
        deleteIsBatch: true,
        exportURL: "/actual/base/residentMoveOutRecord/recovery",
      },
      timeRange: "",
      optionsV: [],
      optionsB: [],
      optionsH: [],
      optionsD: [],
      optionsG: [],
      dataForm: {
        name: "",
        mobile: "",
        idCard: "",
        gridId: "",
        deptId: "",
        villageId: "",
        buildId: "",
        unitId: "",
        homeId: "",
        startTime: "",
        endTime: "",
        // isWelfare: "",
      },
      // 显示人员详情
      showedResiInfo: false,
      lookInfo: {
        resiId: "",
        gridName: "",
      },
      isWelfareArr: [
        { dictValue: "0", dictName: "否" },
        { dictValue: "1", dictName: "是" },
      ],
      changeRecordShow: false,

      searchH: 0,
      tableHeight: 0,
    };
  },
  components: {
    AddOrUpdate,
    resiInfo,
    resiChangeRecord,
  },
  computed: {
    ...mapGetters(["clientHeight", "iframeHeight"]),
    changeVDisabled() {
      return !this.dataForm.villageId;
    },
    changeBDisabled() {
      return !this.dataForm.buildId;
    },
    changeDDisabled() {
      return !this.dataForm.unitId;
    },
  },
  created() {
    this.getGridList();
    this.getValiheList();
  },
  mounted() {
    this.searchH = this.$refs.searchForm.offsetHeight + 290;
    console.log("searchH----", this.$refs.searchForm.offsetHeight);
  },
  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 = "";
      }
    },
    searchH() {
      const h = this.clientHeight - this.searchH + this.iframeHeight;
      const _h = this.clientHeight - this.searchH;
      console.log("computed-searchH---_h", _h);

      this.$nextTick(() => {
        this.tableHeight = this.$store.state.inIframe ? h : _h;
      });
    },
  },
  methods: {
    async handleRecovery(rowData) {
      console.log("11111");
      const url = "/actual/base/residentMoveOutRecord/recovery";

      let params = {
        id: rowData.id,
      };
      const { data, code, msg } = await requestPost(url, params);
      if (code === 0) {
        this.$message.success("恢复成功！");
        this.getDataList();
      } else {
        this.$message.success("操作失败！");
      }
    },
    diaClose() {
      this.changeRecordShow = false;
    },
    //变更记录
    async handleChangeRecord(row) {
      this.changeRecordShow = true;

      await nextTick(200);

      this.$refs.ref_changerecord.initForm(row);
    },

    exportHandle() {
      const url = this.mixinViewModuleOptions.exportURL;
      this.$http({
        method: "POST",
        url,
        responseType: "blob",
        params: this.dataForm,
      })
        .then((res) => {
          // this.download(res.data, title + '.xls')
          if (res.headers["content-disposition"]) {
            let fileName = window.decodeURI(
              res.headers["content-disposition"].split(";")[1].split("=")[1]
            );
            console.log("filename", fileName);
            let blob = new Blob([res.data], {
              type: "application/vnd.ms-excel",
            });
            var url = window.URL.createObjectURL(blob);
            var aLink = document.createElement("a");
            aLink.style.display = "none";
            aLink.href = url;
            aLink.setAttribute("download", fileName);
            document.body.appendChild(aLink);
            aLink.click();
            document.body.removeChild(aLink); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          } else this.$message.error("下载失败");
        })
        .catch((err) => {
          console.log("err", err);
          return this.$message.error("网络错误");
        });
    },
    async handleLook(row) {
      if (row.agencyId !== "") {
        const url = "/gov/org/grid/griddetail";
        let params = {
          gridId: row.agencyId,
        };
        const { data, code, msg } = await requestPost(url, params);
        if (code === 0) {
          this.lookInfo.gridName = data.gridName;
        } else {
          this.$message.error(msg);
        }
      } else {
        this.lookInfo.gridName = "";
      }
      this.lookInfo.resiId = row.icUserId;
      this.showedResiInfo = true;
    },
    handleCancleLook() {
      this.lookInfo.resiId = "";
      this.lookInfo.gridName = "";
      this.showedResiInfo = false;
    },
    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.deptId = 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>
<style lang="scss" scoped>
@import "@/assets/scss/modules/management/list-main.scss";
</style>
