<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.keywords" size="small" placeholder="姓名/手机号" clearable/>
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/gc/member/add')" size="small" icon="el-icon-plus" class="edit_btn" @click.stop="add">新增</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="goDetail(scope.row)">详情</el-button>
            <el-button v-if="hasPerm('/gc/member/update')" type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/gc/member/delete')" type="text" size="small" @click.stop="del(scope.row)">删除</el-button>
            <el-button v-if="hasPerm('/gc/member/addScore')" type="text" size="small" @click.stop="addScore(scope.row)">积分登记</el-button>
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-member v-show="editShow" ref="editmember" @watchChild="fetchData"/>
    <add-score ref="addscore" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import EditMember from './editMember'
import { getMemberListPage, delMember } from '@/api/biz/member'
import AddScore from './addScore'
export default {
  name: 'MemberList',
  components: { AddScore, EditMember },
  data() {
    return {
      listQuery: {
        keywords: '', // 关键字
        type: '', // 人员类型
        post: '', // 岗位
        hireDate: '', // 入职年月时间
        jobCode: '', // 作业内容
        responseArea: '', // 负责片区
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '姓名',
          value: 'name',
          align: 'center'
        },
        {
          text: '性别',
          value: 'sexName',
          align: 'center',
          width: 60
        },
        {
          text: '手机号',
          value: 'tel',
          align: 'center'
        },
        {
          text: '生日',
          value: 'hireDate',
          align: 'center',
          width: 120
        },
        {
          text: '当前积分',
          value: 'score',
          align: 'center'
        },
        {
          text: '创建时间',
          value: 'hireDate',
          align: 'center'
        },
        {
          text: '备注',
          value: 'notes',
          align: 'center'
        }
      ], // 显示列
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      editShow: false, // 编辑页面是否显示
      fileList: [] // 文件列表,上传用
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const that = this
      getMemberListPage(this.listQuery).then(response => {
        that.list = response.data.rows
        that.total = response.data.total
        that.listLoading = false
      })
    },
    // 点击编辑
    edit(row) {
      this.editShow = true
      this.$refs.editmember.initDialog('update', true, row)
    },
    // 点击详情
    goDetail(row) {
      this.editShow = true
      this.$refs.editmember.initDialog('detail', true, row)
    },
    // 点击新增
    add() {
      this.editShow = true
      this.$refs.editmember.initDialog('create', true)
    },
    // 登记积分
    addScore(row) {
      this.$refs.addscore.initDialog(row)
    },
    // 删除单个人员
    del(row) {
      const staffIds = [row.id]
      this.$confirm(
        '确定要删除该人员吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).then(() => {
        delMember(staffIds).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 删除多个人员
    batchDel() {
      if (this.checkSelection()) {
        const staffIds = []
        this.multipleSelection.forEach(function(value, index) {
          staffIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选人员吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }
        ).then(() => {
          delMember(staffIds).then(response => {
            if (response.code === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery = {
        keywords: '', // 关键字
        type: '', // 人员类型
        post: '', // 岗位
        hiredate: '', // 入职年月时间
        jobCode: '', // 作业内容
        responseArea: '', // 负责片区
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
</style>
