<!-- eslint-disable vue/no-parsing-error -->
<template>
  <div class="interaction-setting" v-if="!isStudy">
    <div class="seting-item">
      <div class="seting-label" v-show="false">
        课程问答：<el-switch
          v-model="baseCourseForm.openQuiz"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </div>
    </div>
    <div class="seting-item">
      <div class="seting-label">
        课程讨论：<el-switch
          :active-value="1"
          :inactive-value="0"
          v-model="baseCourseForm.openDiscussion"
        >
        </el-switch>
      </div>
    </div>
    <div class="seting-item">
      <div class="seting-label seting-label-flex">
        课程评价：<el-switch
          v-model="baseCourseForm.openEval"
          :active-value="1"
          :inactive-value="0"
          @change="baseCourseFormchange"
        >
        </el-switch>
      </div>
      <div class="seting-content" v-if="baseCourseForm.openEval">
        <div class="seting-item-2">
          <div class="seting-label">
            <el-radio v-model="baseCourseForm.txtOrGradeEvealute" :label="0">
              文本评价 &nbsp;</el-radio
            >
          </div>
          <div class="seting-content">
            <!-- 因原型需要，故此项输入框需隐藏，其他逻辑无影响 -->
            <!-- <el-input
              :disabled="baseCourseForm.txtOrGradeEvealute"
              placeholder="请输入内容"
              size="mini"
              v-model="baseCourseForm.textEvaluateTitle"
              clearable
            >
            </el-input> -->
          </div>
        </div>
        <div class="seting-item-2">
          <div class="seting-label">
            <el-radio :label="1" v-model="baseCourseForm.txtOrGradeEvealute">
              维度评价:</el-radio
            >
          </div>
          <div class="seting-content">
            支持1-5个维度，每个维度可以选择1-5颗星！
          </div>
        </div>
        <div
          v-show="baseCourseForm.txtOrGradeEvealute"
          class="seting-item-2"
          v-for="(item, index) in courseDimensionality"
          :key="index"
        >
          <div class="seting-label seting-label-2">维度{{ index + 1 }}:</div>
          <div class="seting-content">
            <el-input
              :disabled="!baseCourseForm.txtOrGradeEvealute"
              v-model="item.dimensionality"
              placeholder="请输入内容"
              size="mini"
            >
              <i
                class="el-icon-delete-solid delete-input-button"
                v-if="index"
                slot="append"
                @click="$emit('dleteInput', courseDimensionality, index)"
              ></i
            ></el-input>
          </div>
        </div>
        <div class="seting-item-2"  v-show="baseCourseForm.txtOrGradeEvealute">
          <div class="seting-label seting-label-2"></div>
          <div class="seting-content">
            <el-button
              round
              type="primary"
              class="add-section-button"
              @click="$emit('addDimensionality', 0)"
              ><i class="el-icon-plus"></i>添加维度</el-button
            >
          </div>
        </div>
      </div>
    </div>
    <div class="seting-item" v-if="isShowSelf">
      <div class="seting-label seting-label-flex">
        课后自评：<el-switch
          v-model="baseCourseForm.openSelfEvaluation"
          :active-value="1"
          :inactive-value="0"
        >
        </el-switch>
      </div>
      <div class="seting-content" v-if="baseCourseForm.openSelfEvaluation">
        <div class="seting-item-2">
          <div class="seting-label">

            <el-radio v-model="baseCourseForm.textOrGradeSelfEval" :label="0">
              文本评价 &nbsp;</el-radio
            >
          </div>
          <div class="seting-content">
            <!-- 因原型需要，故此项输入框需隐藏，其他逻辑无影响 -->
            <!-- <el-input
              :disabled="baseCourseForm.textOrGradeSelfEval"
              placeholder="请输入内容"
              size="mini"
              v-model="baseCourseForm.selfTextEvaluateTitle"
              clearable
            >
            </el-input> -->
          </div>
        </div>
        <div class="seting-item-2">
          <div class="seting-label">
            <el-radio :label="1" v-model="baseCourseForm.textOrGradeSelfEval">
              维度评价:</el-radio
            >
          </div>
          <div class="seting-content">
            支持1-5个维度，每个维度可以选择1-5课星！
          </div>
        </div>
        <div
          v-show="baseCourseForm.textOrGradeSelfEval"
          class="seting-item-2"
          v-for="(item, index) in studentDimensionality"
          :key="index"
        >
          <div class="seting-label seting-label-2">维度{{ index + 1 }}:</div>
          <div class="seting-content">
            <el-input
              :disabled="!baseCourseForm.textOrGradeSelfEval"
              v-model="item.dimensionality"
              placeholder="请输入内容"
              size="mini"
            >
              <i
                class="el-icon-delete-solid delete-input-button"
                v-if="index"
                slot="append"
                @click="$emit('dleteInput', studentDimensionality, index)"
              ></i
            ></el-input>
          </div>
        </div>
        <div class="seting-item-2"   v-show="baseCourseForm.textOrGradeSelfEval">
          <div class="seting-label seting-label-2"></div>
          <div class="seting-content">
            <el-button
              round
              type="primary"
              class="add-section-button"
              @click="$emit('addDimensionality', 1)"
              ><i class="el-icon-plus"></i>添加维度</el-button
            >
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="evealuate-box" v-else>
    <div v-if="baseCourseForm.openEval">
      <h2 class="v-title">课程评价</h2>
      <div class="course-evealuate" v-if="baseCourseForm.txtOrGradeEvealute">
        <div
          class="form-item"
          v-for="eveal in courseDimensionality"
          :key="eveal.id"
        >
          <div class="form-item-label">{{ eveal.dimensionality }}：</div>
          <div class="form-item-content">
            <el-rate v-model="evealuateLsit[eveal.id].value"></el-rate>
          </div>
        </div>
      </div>
      <div class="text-evealuate" v-else>
        <!-- <div class="text-eveal-title">
          {{ baseCourseForm.textEvaluateTitle }}
        </div> -->
        <el-input
          placeholder="请输入评价内容"
          v-model="nowCheckCourse.courseEveal"
          clearable
          style="width: 300px"
          size="mini"
        >
        </el-input>
      </div>
    </div>
    <div v-if="baseCourseForm.openSelfEvaluation">
      <h2 class="v-title">自我评价</h2>

      <div class="self-evealuate" v-if="baseCourseForm.textOrGradeSelfEval">
        <div
          class="form-item"
          v-for="eveal in studentDimensionality"
          :key="eveal.id"
        >
          <div class="form-item-label">{{ eveal.dimensionality }}：</div>
          <div class="form-item-content">
            <el-rate v-model="evealuateLsit[eveal.id].value"></el-rate>
          </div>
        </div>
      </div>
      <div class="text-evealuate" v-else>
        <!-- <div class="text-eveal-title">
          {{ baseCourseForm.selfTextEvaluateTitle }}
        </div> -->
        <el-input
          style="width: 300px"
          placeholder="请输入评价内容"
          v-model="nowCheckCourse.selfEvael"
          clearable
          size="mini"
        >
        </el-input>
      </div>
    </div>
    <el-button
      v-if="baseCourseForm.openEval || baseCourseForm.openSelfEvaluation"
      type="primary"
      style="padding: 5px; margin-left: 80px"
      size="mini"
      @click="submitEvealute"
      >提交评价</el-button
    >
  </div>
</template>

<script>
import { updateEvealuateAPI } from '@/api/online-course/my-course'
export default {
  props: {
    nowCheckCourse: {
      type: Object,
      required: true
    },
    isStudy: {
      type: Boolean,
      default: false
    },
    isShowSelf: {
      default: true
    },
    studentDimensionality: {
      type: Array,
      default: () => []
    },
    courseDimensionality: {
      type: Array,
      default: () => []
    }
  },
  components: {},
  data () {
    // 这里存放数据
    return {
      baseCourseForm: {},
      evealuateLsit: {}
    }
  },
  // 监听属性 类似于data概念
  computed: {},
  // 监控data中的数据变化
  watch: {},
  // 方法集合
  methods: {
    async submitEvealute () {
      console.log(this.evealuateLsit)
      const toArray = []
      for (const k in this.evealuateLsit) {
        toArray.push(this.evealuateLsit[k])
        if (!this.evealuateLsit[k].id) {
          this.evealuateLsit[k].creator = this.$store.user.id
        }
        this.evealuateLsit[k].updater = this.$store.user.id
      }
      let sendData
      if (toArray.length > 0) {
        sendData = { evealInfo: toArray }
      }
      if (
        !this.baseCourseForm.txtOrGradeEvealute ||
        !this.baseCourseForm.textOrGradeSelfEval
      ) {
        sendData.textEvealInfo = {
          id: this.nowCheckCourse.id,
          selfEvael: this.nowCheckCourse.selfEvael,
          courseEveal: this.nowCheckCourse.courseEveal
        }
      }
      const res = await updateEvealuateAPI(sendData)
      console.log(res)
      this.$message.success('评价提交成功')
    },
    baseCourseFormchange () {
      console.log(this.baseCourseForm)
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created () {
    if (!this.isStudy) {
      this.baseCourseForm = this.nowCheckCourse
    } else {
      this.baseCourseForm = this.nowCheckCourse.courseInfo
      this.baseCourseForm.evalDimension.forEach((dimension) => {
        if (dimension.type === 0 && !this.baseCourseForm.openEval) {
          return
        }
        if (dimension.type === 1 && !this.baseCourseForm.openSelfEvaluation) {
          return
        }
        this.$set(this.evealuateLsit, dimension.id, {
          dimensionId: dimension.id,
          value: 0,
          recordId: this.nowCheckCourse.id,
          type: dimension.type
        })
      })
      this.nowCheckCourse.eveal.forEach((item) => {
        this.evealuateLsit[item.dimensionId] = item
      })
    }
  },
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted () {},
  beforeCreate () {}, // 生命周期 - 创建之前
  beforeMount () {}, // 生命周期 - 挂载之前
  beforeUpdate () {}, // 生命周期 - 更新之前
  updated () {}, // 生命周期 - 更新之后
  beforeUnmount () {}, // 生命周期 - 销毁之前
  unmounted () {}, // 生命周期 - 销毁完成
  activated () {} // 如果页面有keep-alive缓存功能，这个函数会触发
}
</script>
<style lang='scss' scoped>
.interaction-setting {
  .seting-item {
    width: 100%;
    display: flex;
    justify-content: flex-start;
    padding: 5px 0;
    font-size: 12px;
    // align-items: center;
  }

  .seting-label {
    font-weight: 800;
    display: inline-block;
  }
  .seting-label-flex {
    display: flex;
    height: 30px;
    align-items: center;
  }
  .seting-item-2 {
    margin: 5px;
    display: flex;
    align-items: center;
    .seting-label {
      width: 80px;
      text-align: right;
    }
    .seting-label-2 {
      font-weight: 400;
    }
  }
  .seting-content {
    margin-left: 10px;
  }
  ::v-deep .el-radio__label {
    font-size: 12px;
    font-weight: 700;
  }
}
.evealuate-box {
  .form-item {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
  }
  .form-item-label {
    min-width: 80px;
    display: flex;
    justify-content: flex-end;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .course-evealuate,
  .self-evealuate {
    margin: 10px 0;
  }
  .text-evealuate {
    padding: 10px 0 10px 10px;
  }
  .text-eveal-title {
    margin-bottom: 10px;
  }
}
</style>
