<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="isShow = 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.gmpId)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
          :visible.sync="isShow"
          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/apppreventionreturntopoverty/list?isHousehold=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: ''
      },
      form: {},
      name: '',
      chooseUser: [],
      isShow: 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
    this.dict.load('epidemicDangerousAreaLevel').then(() => {
      this.getGirdList()
      this.getList()
    })
  },

  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/appgirdmemberpoverty/listByGirdMemberByWeb`, null, {
        params: {
          ...this.search,
          girdMemberId: this.$route.query.id,
        }
      }).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.gmpId)
    },
    onConfirm() {
      if (!this.girdId) {
        return this.$message.error('请选择网格')
      }

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

      const povertyList = this.chooseUser.map(v => {
        return {
          girdMemberId: this.$route.query.id,
          name: v.name,
          girdId: this.girdId,
          povertyId: v.id
        }
      })
      this.instance.post(`/app/appgirdmemberpoverty/add`, {
        povertyList
      }).then(res => {
        if (res.code == 0) {
          this.current = 1
          this.getList()
          this.$message.success('添加成功')
          this.closeDialog()
        }
      })
    },
    closeDialog() {
      this.isShow = false
      this.chooseUser = []
      this.girdId = ''
      this.name = ''
      this.areaId = this.user.info.areaId
    },
    onBack() {
      this.$emit('change', {
        type: 'list'
      })
    },
    remove(ids) {
      this.$confirm('确定删除该数据？').then(() => {
        this.instance.post(`/app/appgirdmemberpoverty/delete`, null, {
          params: {ids}
        }).then(res => {
          if (res.code == 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.Family {
  .AiWechatSelecter-container {
    display: flex;
    height: 380px;
    margin-top: 20px;

    ::v-deep {
      .el-icon-circle-close {
        display: inline-block !important;
      }
    }

    .tree-container {
      & > span {
        display: block;
        margin-bottom: 4px;
      }

      .tree-user__item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        &:last-child {
          margin-bottom: 0;
        }
      }

      img {
        width: 27px;
        height: 27px;
        margin-right: 10px;
      }
    }

    ::v-deep .el-tree {
      background: transparent;

      .el-tree-node {
        margin-bottom: 8px;

        &:last-child {
          margin-bottom: 0;
        }
      }

      .el-tree-node__content {
        height: auto;
        margin-top: 2px;
        // align-items: inherit;
      }

      .el-tree-node__expand-icon {
        height: 24px;
      }
    }

    .mask-btn__wrapper {
      position: relative;
    }

    .mask-btn {
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      height: 100%;
      z-index: 1;
    }

    .userlist-item {
      display: flex;
      align-items: center;
      justify-content: space-between;
      margin-bottom: 8px;
      padding: 0 17px;
      cursor: pointer;
      user-select: none;

      ::v-deep .el-checkbox__label {
        display: none;
      }

      .userlist-item__left {
        display: flex;
        align-items: center;

        span {
          color: #222222;
          font-size: 14px;
        }

        img {
          width: 40px;
          height: 40px;
          margin-right: 8px;
        }
      }
    }

    & > div {
      width: 280px;
      background: #FCFCFC;
      border: 1px solid #D0D4DC;
    }

    .AiWechatSelecter-list {
      height: calc(100% - 40px);
      padding: 8px 0;

      ::v-deep .el-scrollbar__wrap {
        margin-bottom: 0 !important;
        overflow-x: hidden;
      }

      ::v-deep .el-checkbox-group {
        padding: 0 8px;

        .el-checkbox {
          display: block;
          margin-right: 0;
        }
      }
    }

    .AiWechatSelecter-container__left {
      flex: 1;
    }

    .AiWechatSelecter-container__right {
      flex: 1;
      margin-left: 20px;

      .AiWechatSelecter-list {
        .tags-wrapper {
          padding: 0 8px;
        }

        .el-tag {
          margin: 0 8px 8px 0px;
          color: #222222;
          font-size: 14px;
        }
      }
    }

    .AiWechatSelecter-header {
      display: flex;
      align-items: center;
      justify-content: space-between;
      height: 40px;
      padding: 0 8px 0 0;
      border-bottom: 1px solid #D0D4DC;
      background: #F5F7FA;

      .AiWechatSelecter-header__left {
        display: flex;
        align-items: center;
        padding: 0 8px;

        h2 {
          height: 100%;
          line-height: 40px;
          color: #222222;
          font-size: 14px;
          text-align: center;
          cursor: pointer;
          border-bottom: 2px solid transparent;

          &.active {
            color: #2266FF;
            border-color: #2266FF;
          }
        }
      }

      .el-button {
        height: 28px;
        padding: 7px 5px;
      }

      .el-input {
        width: 160px;
      }
    }

    .AiWechatSelecter-header__right {
      padding: 0 8px;

      h2 {
        color: #222222;
        font-size: 14px;
        text-align: center;
      }
    }
  }
}
</style>
