<template>
  <ai-list isTabs>
    <template slot="content">
      <ai-search-bar>
        <template #left>
          <ai-select
            v-model="search.auditStatus"
            clearable
            placeholder="请选择审核状态"
            :selectList="dict.getDict('auditStatus')"
            @change="search.current = 1, getList()">
          </ai-select>
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="search.createTimeStart"
            type="date"
            size="small"
            unlink-panels
            placeholder="选择开始日期"
            @change="search.current = 1, getList()"
          />
          <el-date-picker
            value-format="yyyy-MM-dd"
            v-model="search.createTimeEnd"
            type="date"
            size="small"
            unlink-panels
            placeholder="选择结束日期"
            @change="search.current = 1, getList()"
          />
        </template>
        <template #right>
          <el-input
            v-model="search.con"
            size="small"
            placeholder="姓名/身份证/联系方式"
            clearable
            v-throttle="() => {search.current = 1, getList()}"
            @clear="search.current = 1, search.con = '', 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"
        @getList="getList">
        <el-table-column slot="options" width="120px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="toAudit(row.id)" :disabled="row.auditStatus !== '0'">审核</el-button>
              <el-button type="text" @click="toDetail(row.id)">详情</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
        :visible.sync="isShow"
        width="800px"
        @close="onClose"
        title="居民信息审核"
        @onConfirm="onConfirm">
        <el-form class="ai-form" label-width="120px" :model="form" ref="form">
          <el-form-item label="是否通过审核" prop="pass" style="width: 100%;" :rules="[{ required: true, message: '请选择是否通过审核' }]">
            <el-radio-group v-model="form.pass" @change="onStatusChange">
              <el-radio label="0">否</el-radio>
              <el-radio label="1">是</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核意见" prop="opinion" style="width: 100%;" :rules="[{ required: form.pass === '0', message: '请输入审核意见' }]">
            <el-input type="textarea" :rows="5" :maxlength="200" v-model="form.opinion" clearable placeholder="请输入审核意见" show-word-limit></el-input>
          </el-form-item>
        </el-form>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'auditList',

    props: {
      instance: Function,
      dict: Object,
      areaId: String
    },

    data() {
      return {
        search: {
          current: 1,
          size: 10,
          auditStatus: '',
          con: '',
          createTimeEnd: '',
          createTimeStart: ''
        },
        isShow: false,
        currIndex: -1,
        areaList: [],
        total: 10,
        form: {
          opinion: '',
          pass: ''
        },
        id: '',
        colConfigs: [
          { prop: 'name', label: '申请人', align: 'left' },
          { prop: 'createTime', label: '申请时间', align: 'center' },
          { prop: 'sex', label: '性别', align: 'center', formart: v => this.dict.getLabel('sex', v) },
          { prop: 'idNumber', label: '身份证号', align: 'center', formart: v => v.substring(0, 10) + '****' + v.substring(14, 18) },
          { prop: 'age', label: '年龄', align: 'center' },
          { prop: 'auditStatus', label: '审批结果', align: 'center', formart: v => this.dict.getLabel('auditStatus', v) },
          { prop: 'auditUserName', label: '审批人', align: 'center' },
          { prop: 'auditTime', label: '审批时间', align: 'center' }
        ],
        tableData: []
      }
    },

    computed: {
      ...mapState(['user'])
    },

    watch: {
      areaId () {
        this.search.current = 1
        this.getList()
      }
    },

    created() {
      this.search.areaId = this.user.info.areaId
      this.areaName = this.user.info.areaName

      this.dict.load('auditStatus').then(() => {
        this.getList()
      })
    },

    methods: {
      getList() {
        this.instance.post(`/app/appresident/list`, null, {
          params: {
            ...this.search,
            areaId: this.areaId,
            source: 1
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      onStatusChange () {
        this.$refs.form.clearValidate()
      },

      toAudit (id) {
        this.id = id
        this.isShow = true
      },

      onClose () {
        this.form.pass = ''
        this.form.opinion = ''
        this.id = ''
      },

      onConfirm () {
        this.$refs.form.validate(v => {
          if (v) {
            this.instance.post('/app/appresident/examine', null, {
              params: {
                ...this.form,
                id: this.id
              }
            }).then(res => {
              if (res?.code == 0) {
                this.isShow = false
                this.getList()
                this.$message.success('审核成功!')
              }
            })
          }
        })
      },

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

      toDetail (id) {
        this.$router.push({query: {type: '4', id: id}})
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
