<!--课程讨论-->
<template>
  <div>
    <div class="publish" v-if="isPublicShow">
      <div class="publish-header">
        <el-button type="text" @click="gobackDiscuss">
          {{ $t('pages.learn.courseDetail.Returntoquestionlist') }}
        </el-button>
        <div class="publish-title">
          {{ $t('pages.learn.courseDetail.Releaseissues') }}
        </div>
      </div>
      <el-form ref="setPublishform" :model="publish" :rules="publishRules" label-position="top">
        <el-form-item :label="$t('pages.learn.courseDetail.title1')" prop="title">
          <el-input
            type="text"
            :placeholder="$t('pages.learn.courseDetail.inputtitle1')"
            v-model="publish.title"
          ></el-input>
        </el-form-item>
        <el-form-item :label="$t('pages.learn.courseDetail.Textcontent')" prop="contents">
          <v-editor v-model="publish.contents"></v-editor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmitPublish">{{
            $t('pages.learn.courseDetail.Releaseissues')
          }}</el-button>
        </el-form-item>
      </el-form>
    </div>
    <template v-else>
      <div class="discuss-header">
        <div class="discuss-btn" @click="isPublicShow = true">
          <i class="el-icon-edit"></i>
          {{ $t('pages.learn.courseDetail.Publishingissues') }}
        </div>
        <div class="aside">
          <div class="discuss-btn" :class="{ 'is-active': sort === 'time' }" @click="handleSort('time')">
            {{ $t('pages.learn.courseDetail.Sortbytime') }}
          </div>
          <div class="discuss-btn" :class="{ 'is-active': sort === 'tag' }" @click="handleSort('tag')">
            {{ $t('pages.learn.courseDetail.Sortbyvote') }}
          </div>
        </div>
      </div>
      <div class="discuss-scroll">
        <discuss :params="params"></discuss>
      </div>
    </template>
  </div>
</template>

<script>
import * as api from '@/api/discuss'
import VEditor from '@/components/tinymce'

export default {
  components: { VEditor },
  data() {
    const { sid, cid } = this.$route.params
    return {
      sid,
      cid,
      isPublicShow: false,
      publish: {
        title: '',
        contents: ''
      },
      publishRules: {
        title: [{ required: true, message: this.$t('pages.learn.courseDetail.inputtitle1'), trigger: 'blur' }],
        contents: [{ required: true, message: this.$t('pages.learn.courseDetail.inputTextcontent'), trigger: 'blur' }]
      },
      sort: '',
      params: {
        path: `/${sid}/${cid}`,
        request: 'getCourseDiscussList',
        dataJson: { limit: 10, offset: 0, sort: '' }
      }
    }
  },
  methods: {
    /**
     * 返回问题列表
     */
    gobackDiscuss() {
      this.isPublicShow = false
    },
    /**
     * 问题发布 - 接口提交
     */
    onSubmitPublish() {
      this.$refs.setPublishform.validate(valid => {
        if (valid) {
          const params = Object.assign({}, this.publish, { course_id: this.cid, semester_id: this.sid })
          api.publishQues(params).then(res => {
            this.$message({ type: 'success', message: this.$t('pages.learn.courseDetail.publishSuccessTips') })
            this.isPublicShow = false
            this.$refs.setPublishform.resetFields()
          })
        }
      })
    },
    /**
     * 跳转到对应 问题详情页
     */
    goDiscussDetail(e) {
      const qid = e.currentTarget.dataset.id
      this.$router.push({
        path: `/app/learn/discuss-detail/${this.sid}/${this.cid}/${qid}`
      })
    },
    /**
     * 排序方式
     */
    handleSort(sort) {
      this.sort = this.sort === sort ? '' : sort
      this.params = {
        path: `/${this.sid}/${this.cid}`,
        request: 'getCourseDiscussList',
        dataJson: { limit: 10, offset: 0, sort: this.sort }
      }
    }
  }
}
</script>

<style lang="scss">
/* 课程讨论 */
.discuss-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  .aside {
    display: flex;
  }
}
.discuss-btn {
  padding: 0 0.3rem;
  height: 0.42rem;
  font-size: 0.2rem;
  line-height: 0.42rem;
  background: #fff;
  border: 1rpx solid #dcdcdc;
  cursor: pointer;
  border-radius: 0.28rem;
  white-space: nowrap;
  &.is-active {
    color: #fff;
    background: #b49441;
  }
}
.discuss-btn + .discuss-btn {
  margin-left: 0.1rem;
}

// 发布
.publish {
  padding: 0.3rem;
  background-color: #f7f7f7;
}
.publish-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.publish-title {
  font-size: 0.2rem;
}
</style>
