<template>
  <div class="div_main">

    <div class="div_search">
      <el-form :inline="true"
               :model="formData"
               ref="ref_searchform"
               :label-width="'90px'">
        <div>
          <el-form-item label="所属组织"
                        prop="orgId">
            <el-cascader class="customer_cascader"
                         ref="myCascader"
                         v-model="agencyIdArray"
                         :options="orgOptions"
                         :props="orgOptionProps"
                         :show-all-levels="false"
                         @change="handleChangeAgency"></el-cascader>

          </el-form-item>

          <el-form-item label="所属房屋">

            <el-form-item prop="neighborHoodId">
              <el-select v-model.trim="formData.neighborHoodId"
                         placeholder="小区"
                         size="small"
                         clearable
                         class="item_width_2"
                         @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="buildingId">
              <el-select v-model.trim="formData.buildingId"
                         placeholder="楼号"
                         :disabled="!this.formData.neighborHoodId"
                         size="small"
                         clearable
                         class="item_width_2 item_width_2-middle item_width_2"
                         @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="formData.unitId"
                         placeholder="单元"
                         size="small"
                         :disabled="!this.formData.buildingId"
                         clearable
                         class="item_width_2 item_width_2-middle item_width_2"
                         @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="houseId">
              <el-select v-model.trim="formData.houseId"
                         placeholder="房号"
                         :disabled="!this.formData.unitId"
                         size="small"
                         clearable
                         class="item_width_2 item_width_2-middle item_width_2">
                <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>

          <el-form-item label="姓名"
                        prop="name">
            <el-input v-model="formData.name"
                      size="small"
                      class="item_width_2"
                      clearable
                      placeholder="请输入姓名">
            </el-input>

          </el-form-item>
          <el-form-item label="手机号"
                        prop="mobile">
            <el-input v-model="formData.mobile"
                      size="small"
                      class="item_width_2"
                      clearable
                      placeholder="请输入手机号">
            </el-input>

          </el-form-item>

          <el-form-item label="身份证"
                        prop="mobile">
            <el-input v-model="formData.idCard"
                      size="small"
                      class="item_width_2"
                      clearable
                      placeholder="请输入身份证">
            </el-input>

          </el-form-item>
          <el-button style="margin-left:30px"
                     size="small"
                     class="diy-button--search"
                     @click="handleSearch">查询</el-button>
          <el-button style="margin-left:10px"
                     size="small"
                     class="diy-button--reset"
                     @click="resetSearch">重置</el-button>
          <el-button style="margin-left:10px"
                     class="diy-button--reset"
                     size="small"
                     @click="handleExport">导出</el-button>
        </div>
      </el-form>
    </div>
    <div class="div_table">

      <el-table ref="ref_table"
                class="table"
                :data="tableData"
                border
                :height="tableHeight"
                v-loading="tableLoading"
                :header-cell-style="{background:'#2195FE',color:'#FFFFFF'}"
                style="width: 100%">
        <el-table-column type="selection"
                         width="55"></el-table-column>
        <el-table-column label="序号"
                         header-align="center"
                         align="center"
                         type="index"
                         width="50"></el-table-column>

        <el-table-column prop="name"
                         header-align="center"
                         align="center"
                         label="姓名"
                         width="90">
        </el-table-column>
        <el-table-column prop="mobile"
                         header-align="center"
                         align="center"
                         label="手机号"
                         width="110">
        </el-table-column>
        <el-table-column prop="idCard"
                         header-align="center"
                         align="center"
                         label="身份证"
                         width="170">
        </el-table-column>
        <el-table-column prop="neighborHoodName"
                         header-align="center"
                         align="center"
                         label="所属小区"
                         min-width="200">
        </el-table-column>
        <el-table-column prop="age"
                         header-align="center"
                         align="center"
                         label="年龄"
                         width="80">
        </el-table-column>

      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="pageNo"
                       :page-sizes="[20, 50, 100, 200]"
                       :page-size="pageSize"
                       layout="sizes, prev, pager, next, total"
                       :total="total">
        </el-pagination>
      </div>

    </div>

  </div>

</template>

<script>



import { requestPost } from "@/js/dai/request";
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      loading: false,
      total: 0,
      pageSize: 20,
      pageNo: 1,
      tableLoading: false,

      agencyId: '',
      initLoading: false,

      tableArray: [

      ],

      formData: {
        gridId: '',
        agencyId: '',
        orgId: '',
        orgType: '',
        ruleId: '',
        ruleList: {},
        name: '',
        mobile: '',
        idCard: '',

        neighborHoodId: '',
        buildingId: '',
        unitId: '',
        houseId: ''
      },
      optionsV: [],
      optionsB: [],
      optionsH: [],
      optionsD: [],
      optionsG: [],
      orgOptions: [],
      agencyIdArray: [],
      tableData: [],

      orgOptionProps: {
        multiple: false,
        value: 'agencyId',
        label: 'agencyName',
        children: 'subAgencyList',
        checkStrictly: true
      }

    }
  },
  components: {

  },
  async created () {

  },
  activated () {
    this.$refs['ref_table'].doLayout()
  },
  async mounted () {

    this.getOrgTreeList()

    await this.loadTable()

  },

  methods: {
    handleSearch () {
      this.loadTable()
    },


    async loadTable () {
      this.tableLoading = true

      const url = "/data/aggregator/icuser/listByPolicyRules"
      // const url = "http://yapi.elinkservice.cn/mock/245/epmetuser/icEpidemicSpecialAttention/list"
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ...this.formData,
        ruleId: this.ruleId,
        ruleList: this.ruleList
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.total = data.total
        this.tableData = data.list

      } else {
        this.$message.error(msg)
      }
      this.tableLoading = false
    },


    handleChangeAgency (val) {
      let obj = this.$refs["myCascader"].getCheckedNodes()[0].data
      if (obj) {
        if (obj.level === 'grid') {
          this.formData.orgId = this.agencyIdArray.length > 0 ? this.agencyIdArray[this.agencyIdArray.length - 1] : '';
          this.formData.orgType = 'grid'
          this.formData.gridId = this.agencyIdArray.length > 0 ? this.agencyIdArray[this.agencyIdArray.length - 1] : '';
          this.formData.agencyId = ''


        } else {
          this.formData.orgId = this.agencyIdArray.length > 0 ? this.agencyIdArray[this.agencyIdArray.length - 1] : '';
          this.formData.orgType = 'agency'
          this.formData.agencyId = this.agencyIdArray.length > 0 ? this.agencyIdArray[this.agencyIdArray.length - 1] : '';
          this.formData.gridId = ''
        }

      } else {
        this.formData.orgId = ''
        this.formData.orgType = ''
        this.formData.agencyId = ''
        this.formData.gridId = ''
      }

      this.formData.neighborHoodId = ''
      this.formData.buildingId = ''
      this.formData.unitId = ''
      this.formData.houseId = ''
      this.getValiheList()
    },

    handleChangeV (val) {
      console.log('val', val)
      this.formData.buildingId = ''
      this.formData.unitId = ''
      this.formData.houseId = ''
      this.getBuildList()
      this.getUniList()
      this.getHouseList()
    },
    handleChangeB (val) {
      console.log('val', val)
      this.formData.unitId = ''
      this.formData.houseId = ''
      this.getUniList()
      this.getHouseList()
    },
    handleChangeD () {
      this.formData.houseId = ''
      this.getHouseList()
    },
    getOrgTreeList () {
      const { user } = this.$store.state
      this.$http
        .post('/gov/org/customeragency/agencygridtree', {})
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          } else {
            console.log('获取组织树成功', res.data)
            this.orgOptions = []
            this.orgOptions.push(res.data)
          }
        })
        .catch(() => {
          return this.$message.error('网络错误')
        })
    },
    getValiheList () {
      const { user } = this.$store.state
      this.$http
        .post('/gov/org/icneighborhood/neighborhoodoption', { gridId: this.formData.gridId, agencyId: this.formData.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.formData.neighborHoodId })
        .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.formData.buildingId })
        .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.formData.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('网络错误')
        })
    },

    //重置搜索条件
    resetSearch () {
      this.agencyIdArray = []
      this.formData = {
        orgId: '',
        orgType: '',
        ruleId: '',
        ruleList: {},
        name: '',
        mobile: '',
        idCard: '',

        neighborHoodId: '',
        buildingId: '',
        unitId: '',
        houseId: ''
      }

      this.pageNo = 1
      // this.loadTable()
    },


    handleSizeChange (val) {
      this.pageSize = val
      this.pageNo = 1
      this.loadTable()
    },
    handleCurrentChange (val) {
      this.pageNo = val
      this.loadTable()
    },

    //导出表格
    async handleExport () {
      let title = '核酸检测关注名单'

      const url = "/epmetuser/icEpidemicSpecialAttention/vaccination-export"

      app.ajax.exportFilePost(
        url,
        this.formData,
        (data, rspMsg) => {

          this.download(data, title + '.xlsx')
        },
        (rspMsg, data) => {
          this.$message.error(rspMsg);
        }
      );

    },

    // 下载文件
    download (data, fileName) {
      if (!data) {
        return
      }

      var csvData = new Blob([data])

      if (window.navigator && window.navigator.msSaveOrOpenBlob) {
        window.navigator.msSaveOrOpenBlob(csvData, fileName);
      }
      // for Non-IE (chrome, firefox etc.)
      else {
        var a = document.createElement('a');
        document.body.appendChild(a);
        a.style = 'display: none';
        var url = window.URL.createObjectURL(csvData);
        a.href = url;
        a.download = fileName;
        a.click();
        a.remove();
        window.URL.revokeObjectURL(url);
      }

    },







    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    tableHeight () {
      return 400

    },

    ...mapGetters(['clientHeight', 'iframeHeight'])
  },
  watch: {

  },
  props: {
    ruleId: {
      type: String,
      required: ''
    },
    ruleList: {
      type: Object,
      default () {
        return {}
      }
    },

  }
}
</script>

<style lang="scss" scoped >
.div_main {
  width: 100%;

  .div_search {
    background: #ffffff;
    border-radius: 4px;
    padding: 20px 20px 5px 15px;
    .item_width_2 {
      width: 160px;
    }
  }
}

.div_table {
  background: #ffffff;

  border-radius: 4px;

  padding: 0 20px;
  position: relative;

  .table {
    .img-voice {
      width: 30px;
      height: 30px;
    }
  }

  .div_notice {
    position: absolute;
    left: 15px;
    bottom: 0;
  }
}
</style>



