<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-news__populationinformation}">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item label="姓名">
          <el-input v-model.trim="dataForm.residentsName"
                    placeholder="请输入姓名"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model.trim="dataForm.residentsPhone"
                    placeholder="请输入联系电话"
                    clearable></el-input>
        </el-form-item>
        <el-form-item label="身份证号">
          <el-input v-model.trim="dataForm.residentsIdentityNo"
                    placeholder="请输入身份证号"
                    clearable></el-input>
        </el-form-item>
        <br>
        <el-form-item label="辖区范围">
          <el-cascader v-model="deptIdList"
                       :options="options"
                       :props="{ checkStrictly: true }"
                       clearable>
          </el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="exportHandle()">{{ $t('export') }}</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="序号" width="70px" header-align="center" align="center"><template slot-scope="scope">{{scope.$index+1}}</template></el-table-column>
        <el-table-column prop="gridNames" label="辖区范围" header-align="center" align="center" width="200px"></el-table-column>
        <el-table-column prop="residentsName" label="姓名" header-align="center" align="center"></el-table-column>
        <el-table-column prop="residentsSex" label="性别" header-align="center" align="center" :formatter="sexFormat"></el-table-column>
        <el-table-column prop="residentsPhone" label="联系电话" header-align="center" align="center"></el-table-column>
        <el-table-column prop="residentsIdentityNo" label="身份证号码" header-align="center" align="center"></el-table-column>
        <el-table-column prop="residentsNation" label="民族" header-align="center" align="center"></el-table-column>
        <el-table-column prop="householdRegistrationPlace" label="户籍地" header-align="center" align="center"></el-table-column>
        <el-table-column label="更多信息" fixed="right" header-align="center" align="center" width="150">
          <template slot-scope="scope">
              <el-button  type="text" size="small" @click="lookResidentInfo(scope.row.id)">查看</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>
      <!-- 弹窗, 新增 / 修改 -->
      <populationinformation-detail v-if="detailVisible" ref="populationinformationDetail" @refreshDataList="getDataList"></populationinformation-detail>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import PopulationinformationDetail from './populationinformation-detail'

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/app-user/populationinformation/page',
        getDataListIsPage: true,
        deleteURL: '/app-user/populationinformation',
        deleteIsBatch: true,
        exportURL: '/app-user/populationinformation/export'
      },
      dataForm: {
        id: ''
      },
      deptIdList: [],
      options: [],
      detailVisible: false
    }
  },
  components: {
    PopulationinformationDetail
  },
  created: function () {
    this.$http
      .get(`/sys/user/deptOptions/getByLoginUser`)
      .then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.options = res.data.options
      })
      .catch(() => { })
  },
  methods: {
    sexFormat (row, column) {
      return row.residentsSex === '0' ? '女' : '男'
    },
    lookResidentInfo (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.populationinformationDetail.dataForm.id = id
        this.$refs.populationinformationDetail.init()
      })
    }
  },
  watch: {
    'deptIdList': function (val) {
      if (val.length === 0) {
        this.dataForm.streetId = ''
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 1) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = ''
        this.dataForm.gridId = ''
      }
      if (val.length === 2) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = ''
      }
      if (val.length === 3) {
        this.dataForm.streetId = this.deptIdList[0]
        this.dataForm.communityId = this.deptIdList[1]
        this.dataForm.gridId = this.deptIdList[2]
      }
    }
  }
}
</script>
