<template>
  <ai-list class="Family">
    <template slot="title">
      <ai-title title="责任家庭" isShowBack isShowBottomBorder @onBackClick="onBack"></ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="dialog=true">添加</el-button>
          <el-button size="small" :disabled="!ids.length" icon="iconfont iconDelete" @click="removeAll">批量删除</el-button>
          <el-select size="small" style="width: 200px;" v-model="search.girdId" placeholder="所属网格" clearable @change="getListInit()">
            <el-option
                v-for="(item,i) in girdList"
                :key="i"
                :label="item.girdName"
                :value="item.id"
            >
            </el-option>
          </el-select>
        </template>
        <template #right>
          <el-input
              v-model="search.name"
              class="search-input"
              size="small"
              v-throttle="() => {search.current = 1, getList()}"
              placeholder="姓名/身份证/联系方式"
              clearable
              @clear="search.current = 1, search.name = '', getList()"
              suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
          :tableData="tableData"
          :col-configs="colConfigs"
          :total="total"
          style="margin-top: 6px;"
          :current.sync="search.current"
          :size.sync="search.size"
          @handleSelectionChange="handleSelectionChange"
          @getList="getList">
        <el-table-column slot="options" width="100px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="remove(row.gmrId)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
          :visible.sync="dialog"
          width="1100px"
          @close="closeDialog"
          title="添加户主"
          @onConfirm="onConfirm">
        <el-form ref="DialogForm" size="small" label-width="0">
          <el-form-item>
            <ai-area-select clearable always-show :instance="instance" v-model="areaId" :disabled-level="disabledLevel"/>
          </el-form-item>
          <el-form-item label="网格：" required label-width="80px">
            <el-select size="small" v-model="girdId" placeholder="请选择网格" clearable>
              <el-option v-for="(item,i) in girdList" :key="i" :label="item.girdName" :value="item.id"/>
            </el-select>
          </el-form-item>
          <el-form-item>
            <ai-table-select :instance="instance" :action="`/app/appresident/list?householdName=1&areaId=${areaId}&auditStatus=1`"
                             @select="v=>chooseUser=v" multiple/>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
import {mapState} from 'vuex'

export default {
  name: 'Family',
  props: {
    instance: Function,
    dict: Object,
  },

  data() {
    return {
      search: {
        current: 1,
        size: 10,
        name: '',
        girdId: ''
      },
      isLoading: false,
      form: {},
      chooseUser: [],
      dialog: false,
      total: 10,
      colConfigs: [
        {type: 'selection', label: ''},
        {prop: 'name', label: '户主姓名', align: 'left', width: '200px'},
        {prop: 'idNumber', label: '身份证号', align: 'center'},
        {prop: 'phone', label: '联系方式', align: 'center'},
        {prop: 'girdName', label: '所属网格', align: 'center'},
        {prop: 'createTime', label: '添加时间', align: 'center'},
        {slot: 'options', label: '操作', align: 'center'}
      ],
      tableData: [],
      areaId: '',
      ids: [],
      disabledLevel: 0,
      girdList: [],
      girdId: '',
    }
  },

  computed: {
    ...mapState(['user'])
  },
  created() {
    this.areaId = this.user.info.areaId || ""
    this.disabledLevel = this.user.info.areaList?.length || 0
    this.getGirdList()
    this.getList()
    this.dict.load('epidemicDangerousAreaLevel')
  },
  methods: {
    getGirdList() {
      this.instance.post(`/app/appgirdmemberinfo/queryMyGirdListByLevel2?girdMemberId=${this.$route.query.id}`).then(res => {
        if (res.code == 0) {
          this.girdList = res.data
        }
      })
    },
    getListInit() {
      this.search.current = 1
      this.getList()
    },
    getList() {
      this.instance.post(`/app/appgirdmemberresident/listByGirdMember`, null, {
        params: {
          ...this.search,
          girdMemberId: this.$route.query.id,
          areaId: this.areaId
        }
      }).then(res => {
        if (res.code == 0) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },

    removeAll() {
      if (!this.ids) {
        return this.$message.error('请选择户主')
      }
      this.remove(this.ids.join(','))
    },
    handleSelectionChange(e) {
      this.ids = e.map(v => v.gmrId)
    },
    onConfirm() {
      if (!this.girdId) {
        return this.$message.error('请选择网格')
      }

      if (!this.chooseUser.length) {
        return this.$message.error('请选择户主')
      }

      const residentList = this.chooseUser.map(v => {
        return {
          girdMemberId: this.$route.query.id,
          name: v.name,
          residentId: v.id,
          girdId: this.girdId
        }
      })
      this.instance.post(`/app/appgirdmemberresident/add`, {
        residentList
      }).then(res => {
        if (res.code == 0) {
          this.current = 1
          this.getList()
          this.$message.success('添加成功')
          this.dialog = false
        }
      })
    },
    closeDialog() {
      this.chooseUser = []
      this.girdId = ''
      this.areaId = this.user.info.areaId
    },
    del(e) {
      this.chooseUser.splice(this.chooseUser.indexOf(e), 1)
    },
    onBack() {
      this.$emit('change', {
        type: 'list'
      })
    },

    remove(id) {
      this.$confirm('确定删除该数据？').then(() => {
        this.instance.post(`/app/appgirdmemberresident/delete?ids=${id}`).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.Family {
}
</style>
