/* eslint-disable no-duplicate-case */
<template>
  <div class="school-container">
    <div class="topic">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="code">
          <el-input v-model="formInline.code" placeholder="code" clearable />
        </el-form-item>
        <el-form-item label="名字">
          <el-input v-model="formInline.title" placeholder="名字" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="请选择状态">
            <el-option label="未上架" value="ABNORMAL" />
            <el-option label="上架" value="NORMAL" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
      <el-button
        type="primary"
        style="position: absolute;right: 20px; top: 15px;"
        @click="addTopic"
      >添加</el-button>
      <el-table
        :data="getCourseData.list"
        style="width: 100%"
      >
        <el-table-column
          label="id"
          width="180"
          prop="id"
        />
        <el-table-column
          label="code"
          width="180"
          prop="code"
        />
        <el-table-column
          label="icon"
          width="140"
        >
          <template slot-scope="scope">
            <img :src="scope.row.iconImg" width="75">
          </template>
        </el-table-column>
        <el-table-column
          label="标题"
          width="200"
          prop="title"
        />
        <el-table-column
          label="类型"
          width="100"
        >
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.courseType === 'KNOWLEDGE' ? '知识付费' : '图书馆' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="状态"
          width="100"
        >
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.status === 'ABNORMAL' ? '未上架' : '已上架' }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.gmtCreated) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="修改时间"
          width="200"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ newDate(scope.row.gmtModified) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleDel(scope.$index, scope.row)"
            >{{ scope.row.status === 'ABNORMAL' ? '上架' : '下架' }}</el-button>
            <el-button
              size="mini"
              @click="handleSet(scope.$index, scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              @click="handleDetail(scope.$index, scope.row)"
            >课件</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="课程" :visible.sync="dialogFormVisible">
        <el-form ref="ruleForm" :model="form" :rules="rules">
          <el-form-item label="code" :label-width="formLabelWidth" prop="code">
            <el-input v-model="form.code" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="名字" :label-width="formLabelWidth" prop="title">
            <el-input v-model="form.title" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="短名称" :label-width="formLabelWidth">
            <el-input v-model="form.subTitle" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="类型" :label-width="formLabelWidth" prop="courseType">
            <el-select v-model="form.courseType" placeholder="请选择状态">
              <el-option label="知识付费" value="KNOWLEDGE" />
              <el-option label="图书馆" value="LIBRARY" />
            </el-select>
          </el-form-item>
          <el-form-item label="icon" :label-width="formLabelWidth">
            <el-input v-model="form.iconImg" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="封面图" :label-width="formLabelWidth">
            <el-input v-model="form.coverImg" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="课程简介" :label-width="formLabelWidth">
            <el-input v-model="form.summary" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="关键词" :label-width="formLabelWidth">
            <el-input v-model="form.keyWords" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="播放状态" :label-width="formLabelWidth">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="连续" value="CONTINUOUS_PLAY" />
              <el-option label="不连续" value="DISCONTINUOUS_PLAY" />
              <el-option label="循环" value="CIRCULATION_PLAY" />
            </el-select>
          </el-form-item>
          <el-form-item label="排序" :label-width="formLabelWidth">
            <el-input v-model="form.sort" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="价格" :label-width="formLabelWidth">
            <el-input v-model="form.price" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="吊牌价" :label-width="formLabelWidth">
            <el-input v-model="form.originPrice" autocomplete="off" clearable />
          </el-form-item>
          <el-form-item label="状态" :label-width="formLabelWidth">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="未上架" value="ABNORMAL" />
              <el-option label="上架" value="NORMAL" />
            </el-select>
          </el-form-item>
          <el-form-item label="优惠" :label-width="formLabelWidth">
            <el-select v-model="form.isDiscount" placeholder="请选择状态">
              <el-option label="不限时优惠" value="NO" />
              <el-option label="优惠" value="YES" />
            </el-select>
          </el-form-item>
          <el-form-item label="优惠券" :label-width="formLabelWidth">
            <el-input v-model="form.voucher" autocomplete="off" clearable />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="handOk('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
      <el-pagination
        background
        layout="prev, pager, next"
        :total="getCourseData.totalSize"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      dialogFormVisible: false,
      formInline: {
        code: '',
        titl: '',
        status: ''
      },
      form: {},
      formLabelWidth: '120px',
      rules: {
        code: [{ required: true, message: '请输入code', trigger: 'blur' }],
        title: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        courseType: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      page: 1,
      type: ''
    }
  },
  created() {
    this.$store.dispatch('courseList/getCourseLists', {
      page: this.page
    })
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters([
      'getCourseData'
    ])
  },
  methods: {
    newDate(date) {
      return new Date(date).toLocaleString()
    },
    onSubmit() {
      this.$store.dispatch('courseList/getCourseLists', this.formInline)
    },
    handleDel(index, row) {
      const id = row.id
      const status = row.status === 'ABNORMAL' ? 'NORMAL' : 'ABNORMAL'
      this.$store.dispatch('courseList/setCourseLists', {
        id,
        status
      }).then(() => {
        this.$store.dispatch('courseList/getCourseLists', {
          page: this.page
        })
      })
    },
    handleSet(index, row) {
      console.log(index, row)
      this.dialogFormVisible = true
      this.type = 'set'
      this.form = {
        id: row.id,
        code: row.code,
        title: row.title,
        subTitle: row.subTitle,
        courseType: row.courseType,
        iconImg: row.iconImg,
        coverImg: row.coverImg,
        summary: row.summary,
        keyWords: row.keyWords,
        continuity: row.continuity,
        sort: row.sort,
        price: row.price,
        originPrice: row.originPrice,
        status: row.status,
        isDiscount: row.isDiscount,
        voucher: row.voucher
      }
    },
    handOk(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            this.$store.dispatch('courseList/addCourseLists', this.form).then(() => {
              this.dialogFormVisible = false
              this.$store.dispatch('courseList/getCourseLists', {
                page: this.page
              })
            })
          } else {
            this.$store.dispatch('courseList/setCourseLists', this.form).then(() => {
              this.dialogFormVisible = false
              this.$store.dispatch('courseList/getCourseLists', {
                page: this.page
              })
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addTopic() {
      this.dialogFormVisible = true
      this.type = 'add'
      this.form = {
        code: '',
        title: '',
        subTitle: '',
        courseType: '',
        iconImg: '',
        coverImg: '',
        summary: '',
        keyWords: '',
        continuity: '',
        sort: '',
        price: '',
        originPrice: '',
        status: '',
        isDiscount: '',
        voucher: ''
      }
    },
    handleCurrentChange(val) {
      console.log(val)
      this.page = val
      this.$store.dispatch('courseList/getCourseLists', {
        page: this.page
      })
    },
    handleDetail(index, row) {
      console.log(index, row)
      this.$router.push({
        path: '/courseWareList/courseWareList',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.topic {
  margin-top: 20px;
}
.el-form {
  width: 80%;
}
.el-input {
  width: 300px;
}
.el-select>.el-input {
  width: 300px;
}
.el-pagination {
  text-align: center;
  margin-top: 20px;
}
</style>
