<template>
  <div>

    <div>
      <div ref="lxHeader">
        <lx-header icon="md-apps" :text="$route.meta.title" style="margin-bottom: 10px; border: 0px; margin-top: 15px">
          <div slot="content" style="padding-left:0">

            <div class="searchwrap" style="display: flex;align-items: center;">

              <div>
                <div>
                  <el-input v-model="select.name" placeholder="请输入姓名"></el-input>
                </div>

                <xy-selectors style="width:auto" @reset="resetSelect" @search="select.page=1,getList()">
                  <template v-slot:selected>
                    <div class="selected-wrap">
                      <div>
                        <el-input v-model="select.mobile" placeholder="请输入手机号"></el-input>
                      </div>
                      <div style="width:200px">
                        <el-select v-model="typeSelect" placeholder="请选择个人荣誉"
                          @change="(e)=>{changeSelectType(e,'type')}" multiple collapse-tags clearable>
                          <el-option v-for="item in formSelect.type" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option>
                        </el-select>
                      </div>
                      <div>
                        <el-select style="width:100%" v-model="educationType"
                          @change="(e)=>{changeSelectType(e,'education')}" multiple collapse-tags placeholder="请选择学历"
                          clearable>
                          <el-option v-for="item in formSelect.education" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option>
                        </el-select>
                      </div>
                      <div>
                        <el-input v-model="select.company_name" placeholder="请输入公司名称"></el-input>
                      </div>
                      <div>
                        <el-input v-model="select.company_position" placeholder="请输入职务"></el-input>
                      </div>
                      <div style="width:200px">
                        <el-select style="width:100%" v-model="companyArea" multiple collapse-tags
                          @change="(e)=>{changeSelectType(e,'company_area')}" placeholder="请选择所属区域" clearable>
                          <el-option v-for="item in formSelect.company_area" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option>
                        </el-select>
                      </div>
                      <div style="width:250px">
                        <el-select style="width:100%" v-model="companyType"
                          @change="(e)=>{changeSelectType(e,'company_type')}" multiple collapse-tags
                          placeholder="请选择企业资质" clearable>
                          <el-option v-for="item in formSelect.company_type" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option>
                        </el-select>
                      </div>

                      <div style="width:250px">
                        <el-select style="width:100%" v-model="companyIndustry" multiple collapse-tags
                          @change="(e)=>{changeSelectType(e,'company_industry')}" placeholder="请选择所属领域" clearable>
                          <el-option v-for="item in formSelect.company_industry" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option>
                          <!-- <el-option v-for="item in formSelect.company_industry_detail" :key="item.id" :label="item.value"
                            :value="item.value">
                          </el-option> -->
                        </el-select>
                      </div>
                      <div>
                        <el-select v-model="select.status" placeholder="请选择状态" clearable>
                          <el-option v-for="item in apply_status_list" :key="item.id" :label="item.value"
                            :value="item.id">
                          </el-option>
                        </el-select>
                      </div>
                      <div>
                        <el-select v-model="select.sort_type" placeholder="请选择评分排序" clearable>
                          <el-option v-for="item in sortList" :key="item.id" :label="item.value" :value="item.id">
                          </el-option>
                        </el-select>
                      </div>
                    </div>
                  </template>
                </xy-selectors>

                <div>
                  <el-button type="primary" size="small" @click="select.page=1,getList()">查询</el-button>
                  <el-button type="primary" size="small" @click="resetSelect">重置</el-button>

                  <el-button type="primary" size="small" @click="importTable">导入</el-button>
                  <el-button style="margin-right:10px" type="primary" size="small" @click="exportExcel">导出</el-button>
                  <el-popconfirm @confirm="updateAllStatus" title="确定要批量审核吗？">
                    <el-button type="primary" size="small" slot="reference">批量审核</el-button>
                  </el-popconfirm>
                </div>
              </div>

            </div>
          </div>
        </lx-header>
      </div>
    </div>

    <div>
      <div class="txl">
        <div>课程名称：{{subjectObj.title}}</div>
        <div>开课日期：{{subjectObj.date?subjectObj.date:'开课待定'}}</div>
        <div>课程体系：{{subjectObj.leibie}}</div>
      </div>
      <xy-table :list="list" :total="total" :showIndex="false" @pageIndexChange="pageIndexChange"
        @pageSizeChange="pageSizeChange" :table-item="table_item" @selection-change="selectionChange">
        <template v-slot:index>
          <el-table-column align='center' fixed="left" label="" width="50" header-align="center">
            <template slot-scope="scope">
              {{getIndex(scope.$index)}}
            </template>
          </el-table-column>
        </template>
        <template v-slot:status>
          <el-table-column align='center' label="状态" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in apply_status_list">
                <el-tag :type="item.type" v-if="scope.row.status===item.id">{{item.value}}</el-tag>
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <el-table-column align='left' fixed="right" label="操作" width="220" header-align="center">
            <template slot-scope="scope">
              <!-- v-if="scope.row.status===0" -->
              <el-button type="primary" size="small" @click="showDetail('check',scope.row)">审核</el-button>
              <el-button type="primary" size="small" @click="showDetail('checkshow',scope.row)">查看</el-button>
              <el-popconfirm style="margin:0 10px" v-if="scope.row.status===4" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <student-detail ref="studentDetail" @refresh="getList"></student-detail>
    <imports ref="imports" :course_id="subjectObj.id" :status="0" :table-name="'course_signs'" @refresh="getList">
    </imports>

  </div>
</template>

<script>
  import studentDetail from '@/views/student/components/detail.vue';
  import myMixins from "@/mixin/selectMixin.js";
  import formMixin from "@/mixin/formMixin.js";
  import imports from "@/views/component/imports.vue"
  import {
    index,
    updateStatus,
    destroy
  } from '@/api/apply/index.js'
  import {
    download
  } from "@/utils/downloadRequest";

  export default {
    mixins: [myMixins, formMixin],
    components: {
      studentDetail,
      imports
    },
    data() {
      return {
        subjectObj: {},
        companyType: [],
        typeSelect: [],
        educationType: [],
        companyIndustry: [],
        companyArea: [],
        select: {
          name: '',
          mobile: '',
          course_id: '',
          company_name: '',
          company_position: '',
          company_area: '',
          company_type: '',
          company_industry: '',
          education: '',
          type: '',
          status: '',
          page: 1,
          page_size: 10,
          sort_type: 'ASC'
        },
        selectids: [],
        list: [],
        total: 0,
        table_item: [{
            type: 'selection',
            width: 50,
            fixed: 'left',
            prop: 'selection',
            // selectable: (row) => {
            //   return row.status === 0
            // }
          },
          {
            prop: 'index',
            label: '',
            align: 'center',
            width: 50,
            fixed: 'left'
          },
          {
            prop: 'user.username',
            label: '姓名',
            align: 'center',
            width: 120,
            fixed: 'left'
          }, {
            prop: 'user.sex',
            label: '性别',
            align: 'center',
            width: 120
          }, {
            prop: 'user.idcard',
            label: '身份证号',
            align: 'center',
            width: 180
          }, {
            prop: 'user.mobile',
            label: '联系方式',
            align: 'center',
            width: 120,
          }, {
            prop: 'user.birthday',
            label: '出生年月',
            align: 'center',
            width: 120,
          }, {
            prop: 'user.email',
            label: '邮箱',
            align: 'center',
            width: 120,
          }, {
            prop: 'user.company_name',
            label: '公司名称',
            align: 'left',
            width: 240,
          }, {
            prop: 'user.company_position',
            label: '职务',
            align: 'center',
            width: 120,
          }, {
            prop: 'score',
            label: '评分',
            align: 'center',
            width: 120,
          },
          {
            prop: 'created_at',
            label: '报名时间',
            align: 'center',
            width: 180,
          }, {
            prop: 'status',
            label: '审核状态',
            align: 'center',
            width: 180,
          }
        ]
      }
    },
    created() {
      this.subjectObj = this.$route.query
      console.log("this.$route.query", this.$route.query)
      this.select.course_id = this.subjectObj.id
      if (this.subjectObj.status === 0) {
        this.select.status = 0
      } else {
        this.select.status = this.subjectObj.status ? parseInt(this.subjectObj.status) : ''
      }
      this.getList()
    },
    methods: {
      changeSelectType(e, selectName) {
        if (e) {
          this.select[selectName] = e.join(",")
        } else {
          this.select[selectName] = ''
        }
      },
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      getIndex(e) {
        let perSize = (this.select.page - 1) * this.select.page_size
        let index = perSize + e + 1
        return index
      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id,
        }).then(response => {
          this.$Message.success('删除成功');
          this.getList()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
      resetSelect() {
        this.companyType = []
        this.typeSelect = []
        this.educationType = []
        this.companyIndustry = []
        this.companyArea = []
        this.select.name = ''
        this.select.mobile = ''
        this.select.company_name = ''
        this.select.company_position = ''
        this.select.company_area = ''
        this.select.company_type = ''
        this.select.company_industry = ''
        this.select.education = ''
        this.select.type = ''
        this.select.status = ''
        this.select.page = 1
        this.getList()
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          name: this.select.name,
          mobile: this.select.mobile,
          company_name: this.select.company_name,
          company_position: this.select.company_position,
          company_area: this.select.company_area,
          company_type: this.select.company_type,
          company_industry: this.select.company_industry,
          education: this.select.education,
          type: this.select.type,
          sort_name: 'score',
          sort_type: this.select.sort_type,
          filter: [{
            key: 'course_id',
            op: 'eq',
            value: this.select.course_id
          }, {
            key: 'status',
            op: 'eq',
            value: this.select.status
          }]
        })
        this.list = res.data
        this.total = res.total
      },
      // 批量审核
      selectionChange(row) {
        if (row && row.length > 0) {
          let arr = []
          row.map(item => {
            arr.push(item.id)
          })
          this.selectids = arr
        } else {
          this.selectids = []
        }
      },
      updateAllStatus(status) {
        // if (this.base.isNull(this.subjectObj.date)) {
        //   this.$message.warning("请先设置该课程的开课日期后再进行审核")
        //   return
        // }
        if (this.selectids.length < 1) {
          this.$message.warning("请先选择要审核的学员")
          return
        }
        let ids = this.selectids.join(',')
        updateStatus({
          course_id: this.select.course_id,
          ids: ids,
          status: 1,
        }).then(res => {
          this.$Message.success('审核成功')
          this.getList()
        })
      },
      showDetail(type, row) {
        // if (this.base.isNull(this.subjectObj.date)) {
        //   this.$message.warning("请先设置该课程的开课日期后再进行审核")
        //   return
        // }
        this.$refs.studentDetail.subjectObj = this.subjectObj
        this.$refs.studentDetail.id = row.id
        this.$refs.studentDetail.row = row
        this.$refs.studentDetail.type = type
        this.$refs.studentDetail.isShow = true
      },
      importTable(row) {
        this.$refs.imports.show()
      },
      exportExcel() {
        let _export = {}
        this.table_item.map(item => {

          if (item.prop === 'status') {
            _export['status_text'] = item.label
          } else if (item.prop === 'selection') {

          } else {
            _export[item.prop] = item.label
          }
        })
        download(
          '/api/admin/course-signs/index',
          'get', {
            name: this.select.name,
            mobile: this.select.mobile,
            company_name: this.select.company_name,
            company_position: this.select.company_position,
            company_area: this.select.company_area,
            company_type: this.select.company_type,
            company_industry: this.select.company_industry,
            education: this.select.education,
            type: this.select.type,
            sort_name: 'score',
            sort_type: this.select.sort_type,
            export_fields: _export,
            filter: [{
              key: 'course_id',
              op: 'eq',
              value: this.select.course_id
            }, {
              key: 'status',
              op: 'eq',
              value: this.select.status
            }],
            is_export: 1,
            page: 1,
            page_size: 999
          },
          `${this.subjectObj.title}报名信息.xlsx`)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .txl {
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    font-size: 18px;

    &>div {
      margin-right: 15px;

    }
  }

  .searchwrap {
    display: flex;
    align-items: center;
    flex-wrap: wrap;

    &>div,
    .selected-wrap {
      display: flex;
      align-items: center;
      // margin-bottom: 10px;
      flex-wrap: wrap;

      &>div {
        margin-right: 10px;
        margin-bottom: 10px;
        width: 150px;

        &:last-child {
          width: auto
        }
      }

    }
  }
</style>
