<template>
  <!-- 全景课堂详情 -->
  <div class="ssysDetail">
    <el-card>
      <div style="padding: 0 75px;">
        <span class="title">{{detailInfo.ks_name}}</span>
        <span class="subTitle">我的课程 - 我创建的 - <span class="pointer" @click="goBack">全景课堂</span> - <span style="color: #000;">{{detailInfo.ks_name}}</span></span>
      </div>
    </el-card>

    <el-card v-loading="loading">
      <div class="contentDiv">
        <div class="leftContent">
          <div class="titleDiv">
            <div class="contentTitle" style="margin: 0;">{{detailInfo.ks_name}}</div>
            <div class="listDiv">
              <div v-for="(item, index) in recordData" :key="index">
                <el-popover placement="bottom" :width="160" trigger="hover">
                  <div class="text-center">
                    <!-- <qrcode-vue :value="`https://txwxopenapi.txhlwxx.com/openapi/url2qr.php?url=${item.url}`" size="100" level="H" /> -->
                    <img style="width: 100px;height: 100px;margin: auto;" :src="`https://txwxopenapi.txhlwxx.com/openapi/url2qr.php?url=${item.url}`" alt="" srcset="" />
                  </div>
                  <template #reference>
                    <el-button type="primary" :plain="activeRecord==index?false:true" @click="handleRecord(index)">{{item.name}}</el-button>
                  </template>
                </el-popover>
              </div>
              <!-- <el-button v-for="(item, index) in recordData" :key="index" type="primary" :plain="activeRecord==index?false:true" @click="handleRecord(index)">{{item.name}}</el-button>
              <qrcode-vue :value="row.tt_tkjstldz" size="100" level="H" /> -->
            </div>
          </div>
          <div class="image">
            <!-- <img src="/zbktssys/1.png" alt="" /> -->
            <img v-show="!showVideo" :src="commonUrl" alt="">
            <video ref="videoPlay" v-show="showVideo" controls>
              <source :src="commonUrl" type="video/mp4" />
              <source :src="commonUrl" type="video/ogg" />
            </video>
          </div>
        </div>
        <div class="rightContent">
          <div class="contentTitle">互评</div>
          <!-- <el-rate size="large" v-model="comment" allow-half :disabled="disabled" disabled-void-color="#fff" /> -->
          <div>
            <div class="commentList" v-for="(item, index) in commentValueData" :key="index">
              <div>{{item.name}}</div>
              <el-rate size="large" v-model="item.value" allow-half :disabled="disabled" disabled-void-color="#fff" />
            </div>
          </div>
          <div v-show="!disabled" class="h-26px leading-26px px-4 bg-gradient-to-l from-green-400 to-blue-500 text-white rounded-14px cursor-pointer btn" @click="commitComment">评价</div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script>
import QrcodeVue from "qrcode.vue";
import { ssyskc_detail, user_info } from "./api";
import { getFullUrl } from '@/utils/helper';
import empty from '/images/personal/empty.png';
import user from '~/store/user';
export default {
  data() {
    return {
      loading: false,
      detailId: "",
      detailInfo: {},
      showVideo: true,
      commonUrl: "",
      comment: "",
      disabled: false,
      isFamous: false,
      commentData: [],
      commentValueData: [],
      commentData1: [
        // "学具准备",
        // "提前调试",
        // "设备使用",
        // "课堂纪律",
        // "积极应答",
        // "作业上传",
      ],
      commentData2: [
        // "准备充分",
        // "设计科学",
        // "过程互动",
        // "方法多样",
        // "评价多元",
        // "仪态大方",
        // "精神饱满",
        // "学生喜欢",
        // "效果良好",
        // "作业批改"
      ],
      recordData: [],
      activeRecord: "",
    }
  },
  components: { QrcodeVue },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    initDetailData() {
      this.loading = true;
      ssyskc_detail({ ks_id: this.detailId }).then((res) => {
        if (res.code == 1) {
          this.detailInfo = res.data.one_info;

          let arr = this.detailInfo.ks_kzms_json ? JSON.parse(this.detailInfo.ks_kzms_json) : [];
          this.isFamous = arr.some(({ user_id }) => user_id === user.user_id)

          this.recordData = this.detailInfo.ks_lzsp_array || [];
          if (this.recordData.length == 0) {
            this.showVideo = false;
            this.commonUrl = empty;
          } else {
            this.showVideo = true;
            this.handleRecord(0);
          }
          // let suffix = this.detailInfo.ks_kj.substr(this.detailInfo.ks_kj.lastIndexOf(".") + 1);
          // if (suffix == "mp4" || suffix == "MP4") {
          //   this.showVideo = true;
          //   this.commonUrl = getFullUrl(this.detailInfo.ks_kj);
          //   this.$refs.videoPlay.load()
          // } else {
          //   this.showVideo = false;
          //   this.commonUrl = empty;
          // }
          this.disabled = this.detailInfo.hp_status == 1 ? false : true;

          if (this.detailInfo.ksh_star_string != "") {
            this.disabled = true;
            try {
              this.commentValueData = JSON.parse(this.detailInfo.ksh_star_string);
            } catch (error) {
              let arr = this.detailInfo.ksh_star_string.split(",");
              this.commentData = arr.length == this.commentData1.length ? this.commentData1 : this.commentData2;
              this.commentValueData = [];
              this.commentData.map((item, index) => {
                this.commentValueData.push({
                  name: item, value: Number(arr[index])
                })
              })
            }
            this.$forceUpdate();
          } else {
            this.commentData = this.isFamous ? this.commentData1 : this.commentData2;
            this.commentValueData = [];
            this.commentData.map((item) => {
              this.commentValueData.push({
                name: item, value: 0
              })
            })
          }

          this.loading = false;
        }
      });
    },
    commitComment() {
      let data = [];
      this.commentValueData.map(item => {
        data.push(item.value)
      })
      request({
        url: '/kzkt/ssyskc_hpjl/add',
        data: {
          kzkt_ssyskc_hpjl: {
            ks_id: this.detailId,
            // ksh_star: this.comment
            // ksh_star_string: data.join(",")
            ksh_star_string: JSON.stringify(this.commentValueData)
          }
        }
      }).then(res => {
        if (res.code == 1) {
          this.$message.success("互评成功！");
          this.initDetailData();
        }
      })
    },
    handleRecord(index) {
      this.activeRecord = index;
      this.commonUrl = getFullUrl(this.recordData[index].url);
      this.$refs.videoPlay.load()
    }
  },
  async created() {
    // 全科老师
    let res1 = await request({url:"/common/sjzd/index", data:{cs_level:"3", cs_pid:"3002", limit:"100"}})
    if(res1.code == 1) {
      this.commentData1 = res1.data.page_data.map(item => {return item.cs_name})
    }

    // 客座名师
    let res2 = await request({url:"/common/sjzd/index", data:{cs_level:"3", cs_pid:"3001", limit:"100"}})
    if(res2.code == 1) {
      this.commentData2 = res2.data.page_data.map(item => {return item.cs_name})
    }

    // user_info().then((res) => {
    //   if (res.code == 1) {
    //     let userInfo = res.data;
    //     // this.isFamous = userInfo.ext.ue_is_ms == 1 ? true : false;

        this.detailId = this.$route.params.id;
        this.initDetailData();
    //   }
    // });
  }
}
</script>

<style lang="scss" scoped>
.ssysDetail::v-deep {
  .el-card {
    margin-bottom: 15px;
    background: #fff;
  }
  .pointer {
    cursor: pointer;
  }
  .title {
    font-size: 18px;
    color: #050026;
  }
  .subTitle {
    font-size: 14px;
    color: #949494;
    margin-left: 45px;
  }
  .contentDiv {
    display: flex;
    .titleDiv {
      margin-bottom: 20px;
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    .listDiv {
      flex: 1;
      margin-left: 20px;
      display: flex;
      justify-content: right;
      .el-button {
        margin: 5px 10px;
      }
    }
    .contentTitle {
      margin-bottom: 20px;
      font-size: 16px;
      color: #050026;
      white-space: nowrap;
    }
    .leftContent {
      flex: 1;
      padding-right: 20px;
      .image {
        height: 600px;
        // background: #dff3ff;
        // border-radius: 10px;
        img,
        video {
          width: 100%;
          height: 100%;
        }
        video {
          background: #000;
        }
      }
    }
    .rightContent {
      width: 280px;
      padding-left: 20px;
      border-left: 1px dashed #dbdbdb;
      .el-rate__decimal {
        color: #f7ba2a !important;
      }
      .commentList {
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      .btn {
        width: 110px;
        height: 32px;
        line-height: 32px;
        text-align: center;
        margin-top: 15px;
        border-radius: 38px;
        font-size: 14px;
      }
    }
  }
}
</style>