<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.theme" 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>
                <el-button type="primary" size="small" @click='getList'>查询</el-button>
                <el-button type="primary" size="small">导出</el-button>
              </div>
            </div>
          </div>

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

    <div>
      <xy-table :list="list" :total="total" :table-item="table_item">
        <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_nosign_total>
          <el-table-column align='center' label="缺勤人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div @click="showAttendance(scope.row,0)" v-if="scope.row.course_keeps_count>0 && scope.row.user_sign_total>0" style="cursor: pointer;color:blue;text-decoration:underline">
                {{scope.row.course_keeps_count - scope.row.user_sign_total}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <el-table-column align='center' label="操作" width="280" 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/courseContent.js'
    import imports from "@/views/component/imports.vue"
  export default {
    components: {
      showAttendance,
      imports
    },
    data() {
      return {
        select: {
          theme: '',
          date: '',
          page: 1,
          page_size: 10
        },
        importData:[],
        list: [],
        total: 0,
        table_item: [{
            prop: 'date',
            label: '考勤日期',
            align: 'center',
            width: 180,
          }, {
            prop:'period',
            label: '考勤时间',
            align: 'center',
            width: 180,
          },{
            prop: 'theme',
            label: '课程主题',
            align: 'left'
          }, {
            prop: 'teacher.name',
            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_nosign_total',
            label: '缺勤人数',
            align: 'center',
            width: 120,
          }
        ]

      }
    },
    created() {
      this.getList()
    },
    methods: {
      showAttendance(row,state) {
        this.$refs.showAttendance.setRow(row,state)
        this.$refs.showAttendance.isShow = true
      },
      // 导入
      importTable(row) {
        this.$refs.imports.tableData = {
          'data[course_id]':row.course_id,
          'data[course_content_id]':row.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: ['teacher'],
          filter: [{
            key: 'theme',
            op: 'like',
            value: this.select.theme
          }, {
            key: 'date',
            op: 'eq',
            value: this.select.date?this.select.date:''
          }]
        })
        this.list = res.data
        this.total = res.total
      },
    }
  }
</script>

<style lang="scss" scoped>
  .searchwrap {
    display: flex;
    align-items: center;

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

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