var params = {}
if(typeof SCHOOL_ID === 'number'){
	params.schoolid = SCHOOL_ID
}
var components = new KS_GetCompoents(params);
function play(config) {
  var star =
    (config.coursePF == 0 && "0px") ||
    (config.coursePF == 1 && "14px") ||
    (config.coursePF == 2 && "28px") ||
    (config.coursePF == 3 && "42px") ||
    (config.coursePF == 4 && "56px") ||
    (config.coursePF == 5 && "70px");
  $(".tab-course__star i").css("width", star);
  var div = document.createElement("div");
  if (config.photoUrl) {
    div.style.backgroundImage = "url(" + config.photoUrl + ")";
  } else {
    div.style.backgroundImage = "url(/SysImg/player/images/default-img.png)";
  }
  div.className = "course-image";
  $(".tab-course__image").html(div);
  var popup = components.popup;
  var playerUrlList = [];
  var index;
  var playerInfo;
  var courseId = config.courseId;
  var playType;
  var periodId = config.periodId;
  var share = 0;
  handlerData(config.data);

  /**学过了 */
  $(".player-bottom__btn").click(function() {
    components.getUserAppToken(function(apptoken) {
      $.post(
        "/webapi/course/SetMark",
        {
          courseid: courseId,
          periodid: periodId,
          apptoken: apptoken
        },
        function(data) {
          if (data == 1) {
            index++;
            if (index < playerUrlList.length) {
              popup.confirm("太棒了！你已经顺利学完了该课时。", {
                title: "提示",
                icon: "success",
                confirmButtonText: "进入下一个课时",
                confirm: function() {
                  location.href = playerUrlList[index].url;
                }
              });
            } else {
              index = playerUrlList.length - 1;
              popup.confirm("恭喜你！你已经顺利学完了整个课程。", {
                title: "提示",
                icon: "success",
                confirmButtonText: "确定"
              });
            }
          } else {
            popup.confirm("你还没有学完该课时", {
              title: "提示",
              icon: "warning"
            });
          }
        },
        "json"
      );
    });
  });
  function handlerData(data) {
    playType = data.data.Course.teachway;

    var list = data.periodlist.map(function(item) {
      /*过滤章节只保留课时*/
      if (item.addtype == 1 || item.addtype == 4) {
        return item;
      } else {
        return false;
      }
    });

    for (var i = 0; i < list.length; i++) {
      if (list[i].periodid == data.current) {
        playerInfo = list[i];
      }
    }
	 /*
    if(playerInfo.teachWay == 5){
     $.post(
        "/WebApi/course/GetVodChat",
        {
          Periodid: playerInfo.periodid,
          TeachWay: '5'
        },
        function(data) {
          if(data.result){
            $('#commentList').hide()
          }
        },
        "json"
      );
    }
	*/
    
    if (playerInfo.$textID.slice(0, 2) != "课时") {
      $(".title-tree").css("margin-left", "15px");
      $(".title-tree").html(playerInfo.$textID);
    }
    var keshiCount = playerInfo.$unit.slice(2);
    $(".player-title__keshi span")
      .first()
      .html(keshiCount);

    /**去重 */
    for (var i = 0, len = list.length; i < len; i++) {
      if (playerUrlList.indexOf(list[i]) < 0) {
        playerUrlList.push(list[i]);
      }
    }
    /**去false */
    if (playerUrlList.indexOf(false) != -1) {
      playerUrlList.splice(playerUrlList.indexOf(false), 1);
    }

    index = playerUrlList.indexOf(playerInfo);

    if (index != playerUrlList.length - 1) {
      dropurl = playerUrlList[index + 1].url;
    } else {
      dropurl = "#";
    }
    if (index == 0) {
      $(".player-prev__btn").css("cursor", "not-allowed");
    }
    if (index == playerUrlList.length - 1) {
      $(".player-next__btn").css("cursor", "not-allowed");
    }
    if (playType == 5) {
      $.post(
        "/WebApi/course/GetLiveType",
        {
          Periodid: playerInfo.periodid,
        },
        function(data) {
          if(!data.result){
            var reg = new RegExp("/", "g");
      var title = config.name;
      var end = playerInfo.invaliddate;
      var live = playerInfo.livetime;
      var liveTime = live.replace(reg, "-");
      var endTime = end.replace(reg, "-");
      var starTime = new Date(liveTime).getTime();
      /*直播未开始*/
      if (playerInfo.$liveState == 0) {
        var setTime = setInterval(function() {
          var currentTime = new Date().getTime();
          var downTime = Math.floor((starTime - currentTime) / 1000);

          if (downTime > 0) {
            var day =
              Math.floor(downTime / 86400) >= 10
                ? Math.floor(downTime / 86400)
                : "0" + Math.floor(downTime / 86400);

            var hour =
              Math.floor((downTime % 86400) / 3600) >= 10
                ? Math.floor((downTime % 86400) / 3600)
                : "0" + Math.floor((downTime % 86400) / 3600);

            var minute =
              Math.floor(((downTime % 86400) % 3600) / 60) >= 10
                ? Math.floor(((downTime % 86400) % 3600) / 60)
                : "0" + Math.floor(((downTime % 86400) % 3600) / 60);

            var second =
              Math.floor(((downTime % 86400) % 3600) % 60) >= 10
                ? Math.floor(((downTime % 86400) % 3600) % 60)
                : "0" + Math.floor(((downTime % 86400) % 3600) % 60);

            var tipsHtml =
              '<div class="player-down">' +
              '<div class="tips-box">' +
              "<div>" +
              "距离开课还有" +
              "<span>" +
              day +
              "</span>天<span>" +
              hour +
              "</span>时<span>" +
              minute +
              "</span>分<span>" +
              second +
              "</span>秒" +
              "</div>" +
              "<div>" +
              title +
              "</div>" +
              "<div>" +
              liveTime +
              "--" +
              endTime +
              "</div>" +
              "</div>" +
              "</div>";
            $("#player").html(tipsHtml);
          } else {
            clearInterval(setTime);
          }
        }, 1000); /**待留项 */
      } else if (playerInfo.$liveState == 3) {
        var tipsHtml =
          '<div class="player-down">' +
          '   <div class="tips-box">' +
          '       <div style="font-size:36px;">' +
          "       直播即将开始，请稍候..." +
          "       </div>" +
          "   </div>" +
          "</div>";
        $("#player").html(tipsHtml);
      } else if (playerInfo.$liveState == 2 && !playerInfo.isbackplay) {
        var tipsHtml =
          '<div class="player-down">' +
          '<div class="tips-box">' +
          "<div>" +
          "直播已结束" +
          "</div>" +
          "<div>" +
          title +
          "</div>" +
          "<div>" +
          liveTime +
          "--" +
          endTime +
          "</div>" +
          "</div>" +
          "</div>";
        $("#player").html(tipsHtml);
      }
          }
        },
        "json"
      );
      
    }
  }
  /**获取笔记数据 */
  function getData(index) {
    index = index || 0;
    components.getUserAppToken(function(apptoken) {
      $.post(
        "/webapi/course/GetBj",
        {
          Periodid: periodId,
          apptoken: apptoken
        },
        function(data) {
          var bjList;

          if (index == 0) {
            bjList = data.WDbjlist;
          } else {
            bjList = data.bjlist;
          }
          if (bjList.length > 0) {
            components.getArtTemplate(function(template) {
              var render = template.compile(
                "{{each list}}" +
                  '<div class="bj-item">' +
                  '<div class="bj-item__top">' +
                  '<div class="bj-item__image">' +
                  '<img src="{{$value.UserFace}}" />' +
                  "</div>" +
                  '<div class="bj-item__name">' +
                  "{{$value.username}}" +
                  "</div>" +
                  '<div class="bj-item__date">' +
                  "{{$value.adddate}}" +
                  "</div>" +
                  "</div>" +
                  '<div class="bj-item__content">' +
                  "{{$value.bjcontent}}" +
                  "</div>" +
                  "</div>" +
                  "{{/each}}"
              );
              var list = render({ list: bjList });

              $(".bj-list").html(list);
            });
          } else {
            var text = '<div class="empty">暂无内容</div>';
            $(".bj-list").html(text);
          }
        },
        "json"
      );
    });
  }

  $(document).ready(function() {
    $(".player-prev__btn").click(function() {
      index--;
      if (index >= 0) {
        location.href = playerUrlList[index].url;
      } else {
        index = 0;
      }
    });
    $(".player-next__btn").click(function() {
      index++;
      if (index < playerUrlList.length) {
        location.href = playerUrlList[index].url;
      } else {
        index = playerUrlList.length - 1;
      }
    });
    /**切换功能菜单 */
    $(".player-menu__item")
      .unbind("click")
      .bind("click", function() {
        var index = $(this).index();
        if (index == 3) {
          getData();
        }
        if ($(this).attr("active") == 1) {
          $(".player-tabs").css("width", "0");
          $(".player").css("width", "100%");
          $(".player-menu__item").removeClass("curr");
          $(this).attr("active", 0);
        } else {
          $(".player-tabs").css("width", "320px");
          $(".player").css("width", "calc(100% - 320px)");
          $(".player-tab")
            .hide()
            .eq(index)
            .show();
          $(this)
            .addClass("curr")
            .siblings()
            .removeClass("curr");
          $(this)
            .attr("active", 1)
            .siblings()
            .attr("active", 0);
        }
      });
    /**切换笔记 */
    $(".tab-list__tabs").on("click", ".tab-list__tab", function() {
      var index = $(this).index();
      if (index == 1) {
        $(".bj-content").hide();
      } else {
        $(".bj-content").show();
      }

      $(this)
        .addClass("tab-curr")
        .siblings()
        .removeClass("tab-curr");
      /**获取数据 */

      getData(index);
    });
    /**选中分享 */
    $(".sharebtn-bg").click(function() {
      share = share == 0 ? 1 : 0;
      if (share == 1) {
        $(".sharebtn-bg").html("√");
      } else {
        $(".sharebtn-bg").html("");
      }
    });
    /**自动播放下一课时 */
    $(".autonext-bg")
      .unbind("click")
      .bind("click", function() {
        autoPlay = autoPlay == 0 ? 1 : 0;
        if (autoPlay == 1) {
          $(".autonext-bg").html("√");
        } else {
          $(".autonext-bg").html("");
        }
      });
    /**手机二维码 */
    $(".qrcode").mouseover(function() {
      $(".player-qrcode").css("display", "block");
    });
    $(".qrcode").mouseout(function() {
      $(".player-qrcode").css("display", "none");
    });

    /**发表笔记 */

    $(".tab-list__bjbtn")
      .unbind("click")
      .bind("click", function() {
        var text = $(".tab-list__textarea").val();
        components.getUserAppToken(function(apptoken) {
          $.post(
            "/index.aspx?c=course&a=play&action=savebj",
            {
              courseid: courseId,
              pid: periodId,
              Intro: text,
              share: share,
              apptoken: apptoken
            },
            function(data) {
              if (data.result == true) {
                $(".tab-list__textarea").val("");
                getData();
              }
            },
            "json"
          );
        });
      });
    /**下载 */
    function myBrowser() {
      var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
      var isOpera = userAgent.indexOf("Opera") > -1;
      if (isOpera) {
        return "Opera";
      } //判断是否Opera浏览器
      if (userAgent.indexOf("Firefox") > -1) {
        return "FF";
      } //判断是否Firefox浏览器
      if (userAgent.indexOf("Chrome") > -1) {
        return "Chrome";
      }
      if (userAgent.indexOf("Safari") > -1) {
        return "Safari";
      } //判断是否Safari浏览器
      if (
        userAgent.indexOf("compatible") > -1 &&
        userAgent.indexOf("MSIE") > -1 &&
        !isOpera
      ) {
        return "IE";
      } //判断是否IE浏览器
      if (userAgent.indexOf("Trident") > -1) {
        return "Edge";
      } //判断是否Edge浏览器
    }

    function oDownLoad(url, id, title) {
      if (myBrowser() === "IE" || myBrowser() === "Edge") {
        var oPop = window.open(
          url,
          "",
          "width=1, height=1, top=5000, left=5000"
        );
        for (; oPop.document.readyState != "complete"; ) {
          if (oPop.document.readyState == "complete") break;
        }
        oPop.document.execCommand("SaveAs");
        oPop.close();
      } else {
        //!IE

        var odownLoad = document.getElementById(id);

        odownLoad.href = url;
        odownLoad.download = title;
      }
    }

    /**做题跳转 */

    $(".paper-list").on("click", ".paper-btn", function() {
      var el = $(this);
      location.href = el[0].dataset.url;
    });
    $(".data-list").on("click", ".data-btn", function() {
      var el = $(this);
      location.href = el[0].dataset.url;
    });
    $(".data-list").on("click", ".data-downbtn", function() {
      var el = $(this);
      oDownLoad(el[0].dataset.url, "downFile", el[0].dataset.title);
    });
    /** */
    $(".pv-progress-wrap").click(function() {});
  });
}
