<template>
  <ai-list class="list">
    <ai-title slot="title" title="吸毒人员" isShowBottomBorder></ai-title>
    <template slot="content">
      <div class="content">
        <ai-search-bar bottomBorder>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" size="small" @click="toAdd('', 'Add')">添加
            </el-button>
          </template>
          <template #right>
            <el-input
              v-model="search.name"
              size="small"
              placeholder="请输入姓名"
              clearable
              v-throttle="() => {search.current = 1, getList()}"
              @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"
          v-loading="loading"
          style="margin-top: 16px;"
          :current.sync="search.current"
          :size.sync="search.size"
          @getList="getList">
          <el-table-column slot="options" width="140px" fixed="right" label="操作" align="center">
            <template slot-scope="{ row }">
              <div class="table-options">
                <el-button type="text" @click="toDetail(row.id)">详情</el-button>
                <el-button type="text" @click="toEdit(row.id)">编辑</el-button>
                <el-button type="text" @click="remove(row.id)">删除</el-button>
              </div>
            </template>
          </el-table-column>
        </ai-table>
      </div>
    </template>
  </ai-list>
</template>

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

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

    data () {
      return {
        search: {
          current: 1,
          size: 10,
          name: ''
        },
        colConfigs: [
          { prop: 'name', label: '姓名' },
          { prop: 'idNumber', align: 'center', label: '身份证号' },
          { prop: 'gender', align: 'center', label: '性别' },
          { prop: 'birthday', align: 'center', label: '出生日期' }, 
          { prop: 'age', align: 'center', label: '年龄', }, 
          { prop: 'phone', align: 'center', label: '联系方式' }
        ],
        tableData: [],
        total: 0,
        loading: false
      }
    },

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

      param () {
        return {

        }
      }
    },

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

    methods: {
      getList () {
        this.instance.post(`/app/appspecialdrug/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records.map(v => {
              let info = this.idCardNoUtil.getIdCardInfo(v.idNumber)
              v.birthday = info.birthday
              v.age = this.$calcAge(v.idNumber)
              v.gender = info.gender

              return v
            })
            this.total = res.data.total
          }
        })
      },

      toEdit (e) {
        this.$emit('change', {
          type: 'Add',
          params: {
            type: 'Add',
            id: e
          }
        })
      },

      toAdd() {
        this.$emit('change', {
          type: 'Add',
          params: {
            type: 'Add',
          }
        })
      },

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

      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      }
    }
  }
</script>

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