$(function () {
  var accessToken;
  var industry;
  var appeal;
  var type;
  var list;
  var tag = "";
  var pageIndex = 1;
  var total = 0;
  var state = 0;
  var isLogin = 0;
  var $logWrapper = $('.login_wrapper');
  var $loadMore = $('.load_more');
  var $nomore = $('.nomore');
  var $phoneMsg = $("#phone-msg");
  var category = 1;
  var complainLi = "";//手机号验证
  if (localStorage.getItem("accessToken")) {
      $('.goto-complain').text(localStorage.getItem("nickname"));
      getLikelist();
  }
  function getLikelist(){
    $.ajax({
      "url": "http://getway.cnr.cn/cnr45609411d2c5a16/54df76307a4334e9fd3d46eba9dbc743/f76a0411ae1ff31be9f9e28f0b51348b",
      "method": "POST",
      "timeout": 0,
      "dataType": "json",
      "headers": {
        "accessToken": localStorage.getItem('accessToken'),
        "appName": "ygw",
        "Content-Type": "text/plain"
      },
      "data": JSON.stringify({}),
      success: function (res) {
        if (res.code == "P0000") {
          var list = res.data.objectid;
          var $unlike = $('.unlike');
          for(var i=0;i<$unlike.length;i++){
            var obj = $($unlike[i]).attr('data-obj');
            if(list.indexOf(obj) != '-1'){
              $($unlike[i]).removeClass('unlike').addClass('like').unbind('click')
            }
          }
        }else{
          $('.goto-complain').text("登录")
          console.log(res.des)
        }
      }
    })
  }
  function ifnull(txt) {//封装函数，验证手机号是否为空或者是否都是空格
      if (txt.length == 0) {
          return true;
      }
      var $reg = /\s+/;
      return $reg.test(txt);
  }
  var $mobile_prompt_phone1 = $("#mobile_prompt_phone1");
  var $mobile_prompt_phone2 = $("#mobile_prompt_phone2");
  function fastphone() {
      if (ifnull($("#phone-msg").val())) {
          $mobile_prompt_phone1.html("手机号不能为空");
          $mobile_prompt_phone1.css({
              "color": "#E0281E",
              "margin-top": "12px",
              "margin-bottom": "12px",
              "font-size": "14px"
          })
      } else {
          var $reg = /^[1]([3-9])[0-9]{9}$/;
          if (!$reg.test($("#phone-msg").val())) {
              $mobile_prompt_phone1.html("手机号格式不正确");
              $mobile_prompt_phone1.css({
                  "color": "#E0281E",
                  "margin-top": "12px",
                  "margin-bottom": "12px",
                  "font-size": "14px"
              })
          } else {
              $mobile_prompt_phone1.html("");
              $mobile_prompt_phone1.css({
                  "background-color": "#fff",
                  "border": 0,
                  "margin-top": 0,
                  "margin-bottom": 0
              });
              return true;
          }
      }
  }
  $phoneMsg.on("blur", function () {
      fastphone()
  });
  function usercode1() {
      if (ifnull($(".user-code1").val())) {
          $mobile_prompt_phone2.html("验证码不能为空");
          $mobile_prompt_phone2.css({
              "color": "#E0281E",
              "margin-top": "12px",
              "margin-bottom": "12px",
              "font-size": "14px"
          })
      } else {
          $mobile_prompt_phone2.html("");
          $mobile_prompt_phone2.css({
              "background-color": "#fff",
              "border": 0,
              "margin-top": 0,
              "margin-bottom": 0
          });
          return true
      }
  }
  $(".user-code1").on("blur", function () {
      usercode1()
  });
  //短信验证码
  var InterValObj1;
  var curCount1;
  $(".get-code1").click(function () {
      if (!ifnull($phoneMsg.val()) && /^[1]([3-9])[0-9]{9}$/.test($phoneMsg.val())) {
          var count = 60; //间隔函数，1秒执行
          //当前剩余秒数
          curCount1 = count;
          $(".get-code1").attr("disabled", "true");
          $(".get-code1").val(+curCount1 + "秒再获取");
          InterValObj1 = window.setInterval(SetRemainTime1, 1000); //启动计时器，1秒执行一次
          if (fastphone() == true) {
              var phone = $phoneMsg.val();
              $.ajax({
                  url: 'https://getway.cnr.cn/cnr45609411d2c5a16/964bc2bce6493b6c845f62ea1facb7ae/f76a0411ae1ff31be9f9e28f0b51348b',
                  type: 'post',
                  dataType: 'json',
                  headers: {
                      "appName": "ygwmyh",
                      "Content-Type": "application/json"
                  },
                  data: JSON.stringify({
                      "mobile": phone,
                  }),
                  success: function (res) {
                      if (res.rt == 1) {
                          console.log('验证码发送成功')
                      } else { alert(res.des); curCount1 == 0; window.clearInterval(InterValObj1); $(".get-code1").removeAttr("disabled"); $(".get-code1").val("重新发送"); }
                  }
              });
          }
          function SetRemainTime1() {
              if (curCount1 == 0) {
                  window.clearInterval(InterValObj1);//停止计时器
                  $(".get-code1").removeAttr("disabled");//启用按钮
                  $(".get-code1").val("重新发送");
              }
              else {
                  curCount1--;
                  $(".get-code1").val(+ curCount1 + "秒再获取");
              }
          }
      }
  });
  $('.goto-complain').click(function () {
      isUser = sessionStorage.getItem('userToken');
      category = 1;
      $logWrapper.show();
  });
  $('.close-login').click(function () {
      //category = 1;
      $logWrapper.hide();
  });
  //登录
  $('.login-btn').click(function () {
      if (fastphone() == true && usercode1()) {
          complainLi = "";
          tag = "";
          pageIndex = 1;
          $.ajax({
              url: 'https://getway.cnr.cn/cnr2560fac9ba531c0/d6db508930e9b77426d7e73a8449bd63/48a2790e2695aa76440929a2511da2ae',
              type: 'post',
              dataType: 'json',
              headers: {
                  "appName": "ygwmyh",
                  "Content-Type": "application/json"
              },
              data: JSON.stringify({
                  "mobile": $phoneMsg.val(),
                  "verifyCode": $(".user-code1").val()
              }),
              success: function (res) {
                if(res.code == 'P0000'){
                  console.log('昵称：' + res.data.nickname);
                  isLogin = 1;
                  if (isLogin == 1) {
                    accessToken = res.data.accessToken;
                    localStorage.setItem("accessToken", accessToken);
                    localStorage.setItem("nickname", res.data.nickname);
                    $logWrapper.hide();
                    $('.goto-complain').text(res.data.nickname);
                    getLikelist();
                  }
                }else{
                  alert(res.des);
                }
              }
          });
      }
  });
  function setNickName() {
      var nickName = localStorage.getItem("nickname");
      if (nickName != null && nickName.length > 0)
          $('.goto-complain').text(res.data.nickname);
  }
})
