<template>
  <div>
    <!-- <Remark>
      1.教师，我开发的课程<br/>
      2.如果是自己创建的，那么邀请人就是空，显示“自己创建”<br/>
      3.建设课程邀请人流程，提交审核流程都需要讨论！！！！！ <br/>
      4.有哪种几状态和哪几种操作，需要讨论定一下 。在哪种状态下可以有哪些操作<br/>
    </Remark> -->
    <div style="display: flex;justify-content:space-between;position: relative;">
      <div style="display: flex;justify-content: flex-start;  padding: 12px 32px 10px 12px;">
        <!-- <div style="padding-left: 10px;">
          <el-select style="width: 120px;" v-model="params.type" clearable placeholder="课程类型">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="微课" :value="10"></el-option>
            <el-option label="在线课" :value="20"></el-option>
          </el-select>
        </div> -->
        <div style="padding-left: 10px;">
          <el-select style="width: 120px;" v-model="params.status" clearable placeholder="状态">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="草稿" :value="1"></el-option>
            <el-option label="待审核" :value="2"></el-option>
            <el-option label="已审核" :value="5"></el-option>
          </el-select>
        </div>
        <div style="margin-left:10px"><el-input v-model="params.keyword" placeholder="名称关键字" clearable></el-input></div>
        <div style="padding-left: 10px;"><el-button type="primary" icon="el-icon-search" @click="findList()">搜索</el-button></div>
        <div style="padding: 0px 5px;"><el-button icon="el-icon-refresh-right" type="primary" @click="reset">重置</el-button></div>

        <div class="Create-coures"><el-button type="primary" @click="addNewCourse()" icon="el-icon-plus">新建课程</el-button></div>
      </div>
    </div>
    <!--课程列表内容-->
    <div style="">
      <div class="uc-course" v-for="(item, idx) in couresList" :key="idx" @click="jumpRouter(item)">
        <div class="uc-course-img" style="width: 212px;height:119px">
          <course-image :course="item"></course-image>
          <!-- <img :src="fileBaseUrl + item.coverImg"> -->
        </div>
        <div class="uc-course-info">
          <div class="uc-course-name">
            <!-- <span class="uc-course-type1">{{courseType(item.type)}}</span> -->
            <span v-if="item.type == 10" class="uc-course-type1">录播</span>
            <span v-if="item.type == 20" class="uc-course-type1">录播</span>
            <span class="caogao" v-if="item.status == 1">【草稿】</span>
            <span class="daishenhe" v-if="item.status == 2">【待审核】</span>
            <span class="weitongguo" v-if="item.status == 3">【审核未通过】</span>
            <span class="yishenhe" v-if="item.status == 5">【已审核】</span>
            <!-- <a :href="`${webBaseUrl}/course/detail?id=${item.id}`" target="_blank"> {{item.name}}</a> -->
            <span style="font-size:18px;color:#333">{{ item.name }}</span>
          </div>
          <div class="summary-item">
            <div>{{ item.summary }}</div>
          </div>
          <div class="uc-course-text">
            上次修改时间：{{ item.sysUpdateTime }}
            <span type="text" style="margin-left:10px;font-size:14px;cursor: pointer;color:#666" @click.stop="toExamine(item)">查看审核记录</span>
          </div>
        </div>
        <div class="uc-course-btns" style="line-height: 30px;">
          <el-link style="display:block" :underline="false" @click.stop="examine(item)" type="primary" v-if="item.status == 1"  icon="el-icon-document-checked">提交审核</el-link>
          <el-link style="display:block" :underline="false" @click.stop="editCourse(item)" v-if="item.status == 3 || item.status == 4" type="primary" icon="el-icon-edit">编辑</el-link>
          <el-link style="display:block" :underline="false" @click.stop="editCourse(item)" v-if="item.status == 1" type="primary" icon="el-icon-edit">去开发</el-link>
          <el-link style="display:block" :underline="false" @click.stop="delItem(item)" v-if="item.status == 1" type="danger"  icon="el-icon-delete">删除</el-link>
          <el-link style="display:block" :underline="false" @click.stop="withdraw(item)" v-if="item.status == 2"  type="info"  icon="el-icon-delete">撤回</el-link>
        </div>
      </div>
    </div>
    <div v-if="couresList.length > 0" style="text-align: center; margin-top:57px;">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageIndex"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="count"
      ></el-pagination>
    </div>
    <div v-else>
       <div v-if="couresList.length == 0">
              <div v-if="isSearh" class="zan-wu">没有查询到相关内容</div>
              <div v-else class="zan-wu">暂无数据</div>
            </div>
    </div>
    <div></div>
    <el-dialog title="审核记录" :visible.sync="dialogVisible" width="900px" custom-class="g-dialog">
      <div>
        <el-table max-height="500" border :data="inviteTeacher" style="width: 100%;">
          <el-table-column prop="sysCreateBy" label="姓名" width="180"></el-table-column>
          <el-table-column prop="type" label="审核状态">
            <template slot-scope="scope">
              {{ scope.row.auditState ? '通过' : '不通过' }}
            </template>
          </el-table-column>
          <el-table-column prop="auditTime" label="审核时间"></el-table-column>
          <el-table-column prop="auditRemark" label="备注"></el-table-column>
        </el-table>
      </div>
      <span slot="footer" class="dialog-footer"><el-button @click="dialogVisible = false">取 消</el-button></span>
    </el-dialog>
    <course-form ref="courseForm" @submitSuccess="getList"></course-form>
  </div>
</template>

<script>
import courseImage from '@/components/Course/courseImage.vue';
import { mapGetters } from 'vuex';
import studyItem from '@/components/Course/studyItem.vue';
import courseForm from '@/components/Course/courseForm.vue';
import apiCourse from '@/api/modules/course.js';
import { courseType } from '@/utils/tools.js';
import apiAudit from '@/api/system/audit.js';
import apiHRBP from '@/api/boe/HRBP.js';
import apiOrg from '@/api/system/organiza.js';
export default {
  name: 'ucStudyIndex',
  components: { studyItem, courseForm, courseImage },
  computed: {
    ...mapGetters(['userInfo'])
  },
  data() {
    return {
      inviteTeacher: [],
      dialogVisible: false,
      courseType: courseType,
      fileBaseUrl: process.env.VUE_APP_FILE_BASE_URL,
      count: 0,
      params: { keyword: '', type: '', status: '', createUser: '', pageIndex: 1, pageSize: 10, sysCreateAid: '' },
      couresList: [],
      flag: true,
      isSearh:false,
    };
  },
  mounted() {
    if(this.$route.query && this.$route.query.open && this.$route.query.open == 'new') {
       this.addNewCourse();
    }
    this.getList();
  },
  watch:{
    // '$route.query.open':function(val){
    //   if(val == 'new') {
    //      this.addNewCourse();
    //   }
    // }
  },
  methods: {
    // 撤回接口
    withdraw(item) {
      this.$confirm('此操作将撤回审核中的课程, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          apiCourse.revokeSubmit(item.id).then(res => {
            if (res.status == 200) {
              if (res.result) {
                this.$message({
                  type: 'success',
                  message: '撤回成功!'
                });
                this.getList();
              }
            }
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消撤回'
          });
        });
    },
    reset() {
      this.params.keyword = '';
      this.params.status = '';
      this.params.type = '';
      this.params.pageIndex = 1;
      this.getList();
       this.isSearh = false;
    },
    toExamine(row) {
      // this.detailType = row.type;
      this.dialogVisible = true;
      apiAudit.page(1, row.id).then(res => {
        if (res.status === 200) {
          this.inviteTeacher = res.result;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    examine(row) {
      if(!row.orgId){
        this.$message.error("课程还未设置资源归属，请先设置资源归属");
        return;
      }
      let $this=this;
      this.$confirm('您确定要直接提交审核所选课程吗？', '友情提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(()=>{
            apiOrg.getSimple(row.orgId).then(rrs=>{
               if(rrs.status==200){
                  apiHRBP.getHRBP(rrs.result.kid).then(rs=>{
                    if(rs.status==200 && rs.result.length>0){
                        let hrbpUser=rs.result[0];
                        let req={
                           courseId:row.id,
                           email:hrbpUser.email,
                           courseUser:row.sysCreateBy,
                           courseName:row.name,
                           ucode:hrbpUser.user_no,
                           auditUser:hrbpUser.real_name,
                           ukid:hrbpUser.user_id,
                           orgId:row.orgId,
                           orgName:rs.result.orgnization_name_path+'/'+rrs.result.name
                        }
                        apiCourse.sumbits(req).then(res=>{
                          if(res.status==200){
                            $this.$message.success('提交成功');
                            row.status=2
                          }
                          if(res.status==400){
                            $this.$message.error('提交失败:'+res.message);
                          }
                        })
                    }else{
                      $this.$message.error("获取HRBP审核人员失败:"+rs.message);
                    }
                  })
               }else{
                 $this.$message.error("处理资源归属失败，请重新设置资源归属");
               }
            })
      })
    },
    delItem(row) {
      this.$confirm('您确定要删除所选课程吗？', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let params = {
            id: row.id,
            title: row.name
          };
          try {
            //  {id:课程id,多个使用逗号分隔,Boolean erasable 是否物理删除,title:课程的名称, remark 备注}
            const { status } = await apiCourse.del(params);
            if (status === 200) {
              this.$message.success('操作成功！');
              this.getList();
            }
          } catch (error) {
            console.log(error);
          }
        })
        .catch(err => {
          this.$message({ type: 'info', message: '已取消删除', offset: 50 });
        });
    },
    jumpRouter(item) {
      if (item.published) {
        if (item.type == 10) {
          // let routeData = this.$router.resolve({ path: '/course/micro?id='+item.id});
          // window.open(this.webBaseUrl+routeData.href, '_blank');
        //  window.open(this.webBaseUrl + '/course/micro?id=' + item.id, '_blank');
          this.$router.push({path:'/course/micro',query:{id:item.id}})
        }
        if (item.type == 20) {
          // let routeData = this.$router.resolve({ path: '/course/detail?id='+item.id});
          // window.open(this.webBaseUrl+routeData.href, '_blank');
         // window.open(this.webBaseUrl + '/course/detail?id=' + item.id, '_blank');
         this.$router.push({path:'/course/detail',query:{id:item.id}})
        }
      } else {
        if (item.type == 10) {
        //  window.open(`${this.webBaseUrl}/course/microPreview?id=${item.id}`);
         this.$router.push({path:'/course/microPreview',query:{id:item.id}})
        } else {
      //    window.open(`${this.webBaseUrl}/course/rePreview?id=${item.id}`);
       this.$router.push({path:'/course/rePreview',query:{id:item.id}})
        }
      }
    },
    findList() {
      this.params.pageIndex = 1;
      this.isSearh = true;
      this.getList();
    },
    getList() {
      this.params.aid = this.userInfo.aid;
      apiCourse.pageList(this.params).then(res => {
        if (res.status == 200) {
          this.couresList = res.result.list;
          this.count = res.result.count;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    handleSizeChange(val) {
      this.params.pageSize = val;
      // this.params.pageIndex = 1;


      this.getList();
    },
    handleCurrentChange(val) {
      this.params.pageIndex = val;
       this.getList();
    },
    addNewCourse() {
      this.$refs.courseForm.initShow();
    },
    editCourse(row) {
      this.$refs.courseForm.initShow(row);
    },
    lastTabChange(tab, event) {
      console.log(tab.name);
    }
  }
};
</script>

<style scoped lang="scss">
.list-wu{
  text-align: center;
  margin: 40px;
  color: #333;
}
.yishenhe {
  color: #2ab28b;
  font-size: 18px;
}
.daishenhe {
  color: #ffb30f;
  font-size: 18px;
}
.weitongguo {
  font-size: 18px;
  color: #e62e38;
}

.caogao {
  font-size: 18px;
  color: #cecece;
}

.Create-coures {
  position: absolute;
  right: 32px;
}
// .uc-course-type1{
//     padding: 3px 15px;
//     background: #3e7fff;
//     color: #fff;
//     border-radius: 2px
// }
// .uc-course-type2{
//   padding: 3px 15px;
//   background: #ff8e00;
//   color: #fff;
//   border-radius: 2px
// }
.uc-badge {
  margin-top: 10px;
  margin-right: 40px;
}
.uc-course {
  cursor: pointer;
  display: flex;
  justify-content: space-around;
  border-bottom: 1px solid #e8e8e8;
  padding: 20px 0px 15px 0px;
  margin: 0 32px 0 22px;
 ::v-deep .uc-course-img {
    width: 212px;
    img {
      width: 212px;height:119px;
      border: 1px solid #f4f4f5;
    }
  }
  .uc-course-info {
    flex: 1;
    line-height: 28px;
    padding: 0px 10px;
    .summary-item {
      height: 40px;
      line-height: 20px;
      color: #7d7d7d;
      font-size: 14px;
      overflow: hidden;
      width: 90%;
      word-break:break-all;
      display: -webkit-box;
      // overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }
    .uc-course-name {
      font-size: 16px;
      display: -webkit-box;
  white-space:pre-wrap;
  // word-wrap: break-word;
  overflow: hidden;
  text-overflow:ellipsis;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
      word-break:break-all;
      // font-weight: 700;
    }
    .uc-course-text {
      color: #666;
      margin-top: 28px;
    }
  }
  .uc-course-btns {

    text-align: right;
    .btn1 {
      height: 44px;
      padding: 10px 15px;
    }
    .btn2 {
      height: 44px;
      padding: 10px 15px;
    }
    .btn {
      height: 44px;
      padding: 10px 15px;
    }
  }
}
</style>
