<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">
            <div class="searchwrap" style="display: flex;align-items: center;">
              <div>
                <el-input v-model="select.name" placeholder="请输入姓名"></el-input>
              </div>
              <div>
                <el-date-picker v-model="select.date" type="date" placeholder="考勤日期" value-format="yyyy-MM-dd"
                  format="yyyy-MM-dd">
                </el-date-picker>
              </div>
              <div style="margin-right: 10px;">
                <el-select v-model="select.status" placeholder="请选择状态" clearable>
                  <el-option v-for="item in keep_options" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-button type="primary" size="small" @click='select.page=1,getList()'>查询</el-button>
                <el-button type="primary" size="small" @click="importTable">导入</el-button>
                <el-button type="primary" size="small" @click="exportExcel">导出</el-button>
              </div>
            </div>
          </div>

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

    <div>
      <div class="txl">
        <div>课程名称：{{subjectObj.title}}</div>
        <div>开课日期：{{subjectObj.date}}</div>
        <div>课程体系：{{subjectObj.leibie}}</div>
        <div>班主任：{{subjectObj.teacher}}</div>
      </div>
      <xy-table :list="list" :total="total" :table-item="table_item" @pageIndexChange="pageIndexChange"
        @pageSizeChange="pageSizeChange">
        <template v-slot:status>
          <el-table-column align='center' label="状态" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in keep_options">
                <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:course_keeps_count>

          <el-table-column align='center' label="应到人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div @click="showAttendance(scope.row,'')"
                v-if="scope.row.course_keeps_count && scope.row.course_keeps_count>0"
                style="cursor: pointer;color:blue;text-decoration:underline">
                {{scope.row.course_keeps_count}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:user_sign_total>
          <el-table-column align='center' label="实到人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div @click="showAttendance(scope.row,1)" v-if="scope.row.user_sign_total && scope.row.user_sign_total>0"
                style="cursor: pointer;color:blue;text-decoration:underline">
                {{scope.row.user_sign_total}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:user_lack_sign_total>
          <el-table-column align='center' label="缺勤人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div @click="showAttendance(scope.row,0)">
                {{scope.row.user_lack_sign_total}}
              </div>
            </template>
          </el-table-column>
        </template> -->
        <template v-slot:btns>
          <div></div>
          <!-- <el-table-column align='center' label="操作" width="220" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="importTable(scope.row)">导入</el-button>
              <el-button type="primary" size="small" @click="showAttendance(scope.row,'')">查看明细</el-button>
            </template>
          </el-table-column> -->
        </template>
      </xy-table>
    </div>
    <showAttendance ref="showAttendance"></showAttendance>
    <imports ref="imports" :table-name="'course_keeps'" @refresh="getList"></imports>

  </div>
</template>

<script>
  import showAttendance from './components/showAttendance.vue'
  import {
    index
  } from '@/api/course/courseKeeps.js'
  import imports from "@/views/component/imports.vue"
  import {
    download
  } from "@/utils/downloadRequest";
  import myMixins from "@/mixin/selectMixin.js";
  export default {
    mixins: [myMixins],
    components: {
      showAttendance,
      imports
    },
    data() {
      return {
        select: {
          name: '',
          date: '',
          status: '',
          page: 1,
          page_size: 10
        },
        subjectObj: {},
        importData: [],
        list: [],
        total: 0,
        table_item: [{
            prop: 'date',
            label: '考勤日期',
            align: 'center',
            width: 180,
          },
          {
            prop: 'user.name',
            label: '学员姓名',
            align: 'center'
          }, {
            prop: 'status',
            label: '状态',
            align: 'center',

          }

          // {
          //   prop: 'period',
          //   label: '考勤时间',
          //   align: 'center',
          //   width: 180,
          // }, {
          //   prop: 'theme',
          //   label: '课程主题',
          //   align: 'left'
          // }, {
          //   prop: 'teacher.name',
          //   label: '授课教师',
          //   align: 'center',
          //   width: 160,
          // }, {
          //   prop: 'address',
          //   label: '授课教室',
          //   align: 'center',
          //   width: 160,
          // }, {
          //   prop: 'course_keeps_count',
          //   label: '应到人数',
          //   align: 'center',
          //   width: 120,
          // },
          // {
          //   prop: 'user_sign_total',
          //   label: '实到人数',
          //   align: 'center',
          //   width: 120,
          // },
          // {
          //   prop: 'user_lack_sign_total',
          //   label: '缺勤人数',
          //   align: 'center',
          //   width: 120,
          // }
        ]

      }
    },
    created() {
      this.subjectObj = this.$route.query
      console.log(this.$route.query)
      this.select.course_id = this.$route.query.id
      this.getList()
    },
    methods: {
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      showAttendance(row, state) {
        this.$refs.showAttendance.setRow(row, state)
        this.$refs.showAttendance.isShow = true
      },
      // 导入
      importTable() {
        this.$refs.imports.tableData = {
          'data[course_id]': this.select.course_id
        }
        this.$refs.imports.show()

        // {"course_id":15,"course_content_id":188}
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          show_relation: ['user'],
          name: this.select.name,
          filter: [{
            key: 'status',
            op: 'eq',
            value: this.select.status
          }, {
            key: 'date',
            op: 'eq',
            value: this.select.date ? this.select.date : ''
          }],
        })
        this.list = res.data
        this.total = res.total
      },
      exportExcel() {
        let _export = {}
        this.table_item.map(item => {
          if(item.prop==="status"){
            _export['status_text'] = item.label
          }else{
            _export[item.prop] = item.label
          }
        })
        download(
          '/api/admin/course-keeps/index',
          'get', {
            export_fields: _export,
            show_relation: ['user'],
            name: this.select.name,
            filter: [{
              key: 'status',
              op: 'eq',
              value: this.select.status
            }, {
              key: 'date',
              op: 'eq',
              value: this.select.date ? this.select.date : ''
            }],
            is_export: 1,
            page: 1,
            page_size: 999
          },
          `${this.subjectObj.title}${this.select.date ? this.select.date : ''}考勤信息.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;

    &>div {
      display: flex;
      align-items: center;
      margin-right: 10px;

      span {
        min-width: 70px;
      }
    }
  }
</style>
