<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="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-select v-model="select.status" placeholder="请选择发布状态" clearable>
                  <el-option v-for="item in course_status" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
              </div>
              <div>
                <el-button type="primary" size="small" @click="getList">查询</el-button>
              </div>
              <div>
                <el-button type="primary" size="small" @click="editCourse('add')">新增课程</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">
              {{scope.row.start_date}}至{{scope.row.end_date}}
            </template>
          </el-table-column>
        </template>
        <template v-slot:date_status>
          <el-table-column align='center' label="课程状态" width="120" header-align="center">
            <template slot-scope="scope">
              <div v-for="item in course_date">
                <el-tag :type="item.type" v-if="scope.row.date_status===item.value">{{item.value}}</el-tag>
              </div>
            </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 course_status">
                <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' label="操作" width="380" header-align="center">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="editCourse('editor',scope.row.id)">编辑</el-button>
              <el-popconfirm style="margin:0 10px" @confirm="deleteList(scope.row.id)" title="确定删除吗？">
                <el-button type="danger" size="small" slot="reference">删除</el-button>
              </el-popconfirm>
              <el-button type="primary" size="small" @click="toTxl(scope.row)">通讯录</el-button>
              <el-button type="primary" size="small" v-if="scope.row.is_fee===1"
                @click="toPay(scope.row)">缴费</el-button>
              <el-button type="primary" size="small" @click="showQrCode(scope.row)">二维码</el-button>
            </template>
          </el-table-column>
        </template>
      </xy-table>
    </div>

    <add-course ref="addCourse" @refresh="getList"></add-course>
    <show-code ref="showCode"></show-code>
  </div>
</template>

<script>
  import myMixins from "@/mixin/selectMixin.js";
  import addCourse from './components/addCourse.vue';
  import showCode from './components/showCode.vue';
  import {
    index,
    destroy
  } from "@/api/course/index.js"
  import {
    index as indexType
  } from "@/api/course/courseType.js"
  export default {
    mixins: [myMixins],
    components: {
      addCourse,
      showCode
    },
    data() {
      return {
        dateRange: [],
        courseTypesList:[],
        select: {
          name: '',
          dateRange: '',
          type: "",
          status: '',
          page: 1,
          page_size: 10,
        },
        list: [],
        total: 0,
        table_item: [{
          prop: 'name',
          label: '课程名称',
          align: 'left'
        }, {
          prop: 'dateRange',
          label: '开课日期',
          align: 'center',
          width: 180,
        }, {
          prop: 'sign_end_date',
          label: '报名截止日期',
          align: 'center',
          width: 120,
        }, {
          prop: 'type_detail.name',
          label: '类别',
          align: 'center',
          width: 180,
        }, {
          prop: 'date_status',
          label: '课程状态',
          align: 'center',
          width: 180,
        }, {
          prop: 'status',
          label: '发布状态',
          align: 'center',
          width: 180,
        }]

      }
    },
    created() {
      this.getList()
      this.getTypes()
    },
    methods: {
      getTypes() {
        indexType({
          page: 1,
          page_size: 999
        }).then(res => {
          this.courseTypesList = res.data.filter(item => item.status === 1)
        })
      },
      pageIndexChange(e) {
        this.select.page = e
        this.getList()
      },
      pageSizeChange(e) {
        this.select.page_size = e
        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:'sign_status',
          sort_type:'DESC',
          filter: [{
            key: 'name',
            op: 'like',
            value: this.select.name
          }, {
            key: 'type',
            op: 'eq',
            value: this.select.type
          }, {
            key: 'status',
            op: 'eq',
            value: this.select.status
          }, {
            key: 'start_date',
            op: 'range',
            value: this.select.dateRange
          }]
        })
        this.list = res.data
        this.total = res.total
      },
      changeDateRange(e) {
        if (e) {
          this.select.dateRange = e.join(",")
        } else {
          this.select.dateRange = ''
        }
      },

      editCourse(type, id) {
        if (id) {
          this.$refs.addCourse.id = id
        }
        this.$refs.addCourse.type = type
        this.$refs.addCourse.courseTypesList = this.courseTypesList
        this.$refs.addCourse.isShow = true
      },
      showQrCode(row) {
        let value = ''
        this.course_type.map(item => {
          if (item.id == row.type) {
            value = item.value
          }
        })
        this.$refs.showCode.row = {
          type_value: value,
          ...row
        }
        this.$refs.showCode.isShow = true
      },
      toTxl(row) {
        let value = row.type_detail.name

        this.$router.push({
          path: '/course/txl',
          query: {
            title: row.name,
            id: row.id,
            date: row.start_date + '至' + row.end_date,
            leibie: value
          }
        })
      },
      toPay(row) {
        let value = row.type_detail.name
        this.$router.push({
          path: '/course/pay',
          query: {
            title: row.name,
            id: row.id,
            date: row.start_date + '至' + row.end_date,
            leibie: value
          }
        })
      },
      deleteList(id) {
        var that = this;
        destroy({
          id: id,
        }).then(response => {
          this.$Message.success('删除成功');
          this.getList()
        }).catch(error => {
          console.log(error)
          reject(error)
        })
      },
    }
  }
</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>
