<template>
  <app-card class="register-box">
    <app-list v-bind="tableOptions" ref="list">
      <div class="line"></div>
      <div class="btn-box">
        <el-button type="primary" @click="$router.push({ path: '/student/update' })">+ 添加学员</el-button>
        <el-button type="primary" @click="importDialogVisible = true">批量导入</el-button>
        <el-button type="primary" @click="downloadRecords">导出学员</el-button>
      </div>
      <template v-slot:filter-name>
        <el-input v-model="form.name" placeholder="请输入内容"></el-input>
      </template>
      <template v-slot:filter-mobile>
        <el-input v-model="form.mobile" placeholder="请输入内容"></el-input>
      </template>
      <template v-slot:filter-project>
        <el-input v-model="form.project" placeholder="请输入内容"></el-input>
      </template>
      <template v-slot:filter-template>
        <el-input v-model="form.template" placeholder="请输入内容"></el-input>
      </template>
      <template v-slot:table-x="{ row }">
        <el-button type="text" @click="handleEdit(row)">编辑</el-button>
        <el-button type="text" @click="handleDelete(row)">删除</el-button>
      </template>
    </app-list>
    <import-people
      @cancel="importDialogVisible = false"
      width="400px"
      :visible.sync="importDialogVisible"
    ></import-people>
  </app-card>
</template>

<script>
import importPeople from '../components/importPeople.vue'
// 接口
import { getAppList, deleteStudent, getTemplateList, getProjectList } from '../api'

export default {
  components: {
    importPeople
  },
  data() {
    return {
      importDialogVisible: false,
      form: {
        name: '',
        mobile: '',
        project_id: '',
        template_id: ''
      },
      templateList: [],
      projectList: []
    }
  },
  computed: {
    // 列表配置
    tableOptions() {
      return {
        limit: 10,
        remote: {
          httpRequest: getAppList,
          params: this.form
        },
        filters: [
          {
            type: 'input',
            slots: 'filter-name',
            label: '姓名:'
          },
          {
            type: 'input',
            slots: 'filter-mobile',
            label: '手机号:'
          },
          {
            type: 'select',
            prop: 'project_id',
            label: '所属项目:',
            labelKey: 'title',
            valueKey: 'id',
            options: this.projectList
          },
          {
            type: 'select',
            prop: 'template_id',
            label: '证书模板:',
            labelKey: 'title',
            valueKey: 'id',
            options: this.templateList
          }
        ],
        columns: [
          { label: 'id', prop: 'id', align: 'center' },
          { label: '姓名', prop: 'name', align: 'center' },
          { label: '手机号', prop: 'mobile', align: 'center' },
          { label: '邮箱', prop: 'email', align: 'center' },
          { label: '编号', prop: 'number', align: 'center' },
          { label: '创建时间', prop: 'created_time', align: 'center' },
          { label: '操作', slots: 'table-x', align: 'center' }
        ]
      }
    }
  },
  methods: {
    // 项目列表
    getProjectList() {
      getProjectList({ 'per-page': '1000' }).then(res => {
        this.projectList = res.data.list
      })
    },
    // 证书模板
    getTemplateList() {
      getTemplateList({ 'per-page': 1000 }).then(res => {
        this.templateList = res.data.list
      })
    },
    // 导出
    downloadRecords() {
      const formParams = []
      Object.keys(this.form).forEach(item => {
        if (this.form[item]) {
          formParams.push(`${item}=${this.form[item]}`)
        }
      })
      window.open(
        `${window.location.origin}/api/certificate/v1/backend/student/download?${formParams.join().replace(/,/g, '&')}`
      )
    },
    // 编辑
    handleEdit(row) {
      const find = row.projects.find(item => item.status === '3' || item.status === '1')
      if (find) {
        this.$message(find.status === '1' ? '学员已有生成中证书禁止编辑' : '学员已有证书已生成禁止编辑')
        return false
      }
      this.$router.push({ path: '/student/update', query: { id: row.id } })
    },
    // 删除
    handleDelete(row) {
      this.$confirm('此操作将永久删除该学员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteStudent({ id: row.id }).then(res => {
            if (res.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success'
              })
              this.$refs.list.refetch(true)
            }
          })
        })
        .catch(() => {})
    }
  },
  mounted() {
    this.getTemplateList()
    this.getProjectList()
  }
}
</script>

<style lang="scss">
.register-box {
  .el-form-item {
    margin-right: 20px !important;
  }
  .line {
    // border-top: 1px solid #D6D6D6;
    height: 1px;
    background: #d6d6d6;
    margin: 8px 0 23px;
    box-sizing: border-box;
  }
  .btn-box {
    margin-bottom: 16px;
  }
}
</style>
