<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 @change="changeDateRange" v-model="select.dateRange" type="daterange" range-separator="至"
                  value-format="yyyy-MM-dd" format="yyyy-MM-dd" start-placeholder="开课日期" end-placeholder="结束日期">
                </el-date-picker>
              </div>
              <div>
                <el-select v-model="select.type" placeholder="请选择课程体系" clearable>
                  <el-option v-for="item in courseTypesList" :key="item.id" :label="item.name" :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="resetSelect">重置</el-button>
              </div>
            </div>
          </div>

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

    <div>
      <xy-table :list="list" :total="total" @pageIndexChange="pageIndexChange" @pageSizeChange="pageSizeChange" :table-item="table_item">
        <template v-slot:dateRange>
          <el-table-column align='center' label="开课日期" width="200" header-align="center">
            <template slot-scope="scope">
              <div>
                {{scope.row.start_date?scope.row.start_date+'至'+scope.row.end_date:''}}
              </div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:course_signs_count>
          <el-table-column align='center' label="目前报名人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.course_signs_count && scope.row.course_signs_count>0" style="cursor: pointer;color:blue;text-decoration:underline"
                @click="toApply(scope.row,'')">
                {{scope.row.course_signs_count}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:sign_pass_total>
          <el-table-column align='center' label="审核通过人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div  v-if="scope.row.sign_pass_total && scope.row.sign_pass_total>0" style="cursor: pointer;color:blue;text-decoration:underline"
                @click="toApply(scope.row,1)">{{scope.row.sign_pass_total}}</div>
                <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:sign_fault_total>
          <el-table-column align='center' label="审核不通过人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.sign_fault_total && scope.row.sign_fault_total>0" style="cursor: pointer;color:blue;text-decoration:underline"
                @click="toApply(scope.row,2)">
                {{scope.row.sign_fault_total}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:sign_wait_total>
          <el-table-column align='center' label="待审核人数" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.sign_wait_total && scope.row.sign_wait_total>0" style="cursor: pointer;color:blue;text-decoration:underline"
                @click="toApply(scope.row,0)">
                {{scope.row.sign_wait_total}}
              </div>
              <div v-else>0</div>
            </template>
          </el-table-column>
        </template>
        <template v-slot:btns>
          <div></div>
        </template>
      </xy-table>
    </div>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import {
    index
  } from "@/api/course/index.js"
  import {
    index as indexType
  } from "@/api/course/courseType.js"
  export default {
    mixins: [myMixins],
    components: {},
    data() {
      return {
        dateRange: [],
        courseTypesList:[],
        select: {
          name: '',
          dateRange: '',
          type: "",
          page: 1,
          page_size: 10,
        },
        list: [],
        total: 0,
        table_item: [{
            prop: 'name',
            label: '课程名称',
            align: 'left'
          }, {
            prop: 'dateRange',
            label: '开课日期',
            align: 'center',
            width: 180,
          }, {
            prop: 'type_detail.name',
            label: '课程体系',
            align: 'center',
            width: 180,
          }, {
            prop: 'total',
            label: '拟开课人数',
            align: 'center',
            width: 120,
          }, {
            prop: 'course_signs_count',
            label: '目前报名人数',
            align: 'center',
            width: 120
          },
          {
            prop: 'sign_pass_total',
            label: '审核通过人数',
            align: 'center',
            width: 120
          },
          {
            prop: 'sign_fault_total',
            label: '审核不通过人数',
            align: 'center',
            width: 120
          },
          {
            prop: 'sign_wait_total',
            label: '待审核人数',
            align: 'center',
            width: 120
          }]

      }
    },
    created() {
      this.getList()
      this.getTypes()
    },
    methods: {
      pageIndexChange(e){
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e){
        this.select.page_size = e
        this.select.page = 1
        this.getList()
      },
      resetSelect(){
        this.select.name = ''
        this.select.type = ''
        this.select.dateRange = ''
        this.select.page=1
        this.getList()
      },
      async getList() {
        const res = await index({
          page: this.select.page,
          page_size: this.select.page_size,
          show_relation: ['typeDetail'],
          sort_name:'start_date',
          sort_type:'DESC',
          filter: [{
            key: 'name',
            op: 'like',
            value: this.select.name
          }, {
            key: 'type',
            op: 'eq',
            value: this.select.type
          }, {
            key: 'start_date',
            op: 'range',
            value: this.select.dateRange
          }]
        })
        this.list = res.data
        this.total = res.total
      },
      getTypes() {
        indexType({
          page: 1,
          page_size: 999,
          sort_name:'id',
          sort_type:'ASC',
        }).then(res => {
          // this.courseTypesList = res.data.filter(item => item.status === 1)
          this.courseTypesList = res.data
        })
      },
      changeDateRange(e) {
        if (e) {
          this.select.dateRange = e.join(",")
        } else {
          this.select.dateRange = ''
        }
      },
      toApply(row,status) {
        this.$router.push({
          path: '/course/apply_list',
          query: {
            status:status,
            title: row.name,
            id: row.id,
            date: row.start_date?row.start_date + '至' + row.end_date:'',
            leibie: row.type_detail.name
          }
        })
      },
    }
  }
</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>
