/* 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-select v-model="formInline.status" placeholder="请选择状态">
            <el-option label="未上架" value="NOT_ON_STOCK" />
            <el-option label="上架" value="HAS_ON_STOCK" />
          </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="getTopicData.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="背景图"
          width="140"
        >
          <template slot-scope="scope">
            <img :src="scope.row.bgImg" width="128">
          </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.status === 'NOT_ON_STOCK' ? '未上架' : '已上架' }}</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">{{ 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">{{ 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 === 'NOT_ON_STOCK' ? '上架' : '下架' }}</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="bgImg">
            <el-input v-model="form.bgImg" 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" prop="status">
            <el-select v-model="form.status" placeholder="请选择状态">
              <el-option label="未上架" value="NOT_ON_STOCK" />
              <el-option label="上架" value="HAS_ON_STOCK" />
            </el-select>
          </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="getTopicData.totalSize"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      dialogFormVisible: false,
      formInline: {
        code: '',
        status: ''
      },
      form: {
        code: '',
        title: '',
        bgImg: '',
        status: ''
      },
      formLabelWidth: '120px',
      rules: {
        code: [{ required: true, message: '请输入code', trigger: 'blur' }],
        title: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        bgImg: [{ required: true, message: '请输入背景图', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }]
      },
      page: 1
    }
  },
  created() {
    this.$store.dispatch('topicList/getTopicLists', {
      page: this.page
    })
  },
  // eslint-disable-next-line vue/order-in-components
  computed: {
    ...mapGetters([
      'getTopicData'
    ])
  },
  methods: {
    onSubmit() {
      this.$store.dispatch('topicList/getTopicLists', this.formInline)
    },
    handleDel(index, row) {
      const id = row.id
      const status = row.status === 'NOT_ON_STOCK' ? 'HAS_ON_STOCK' : 'NOT_ON_STOCK'
      this.$store.dispatch('topicList/setTopicLists', {
        id,
        status
      }).then(() => {
        this.dialogFormVisible = false
        this.$store.dispatch('topicList/getTopicLists', {
          page: this.page
        })
      })
    },
    handleDetail(index, row) {
      console.log(index, row)
      this.$router.push({
        path: '/topicRelation/topicRelation',
        query: {
          topicId: row.id
        }
      })
    },
    handOk(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store.dispatch('topicList/addTopicLists', this.form).then(() => {
            this.dialogFormVisible = false
            this.$store.dispatch('topicList/getTopicLists', {
              page: this.page
            })
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addTopic() {
      this.dialogFormVisible = true
    },
    handleCurrentChange(val) {
      console.log(val)
      this.page = val
      this.$store.dispatch('topicList/getTopicLists', {
        page: this.page
      })
    }
  }
}
</script>

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