!function () {
  window.CertificationController = {
    isClick: false,
    token: Util.getCookie('token') || '',
    imgUrl: [],
    init: function () {
      this.created(this).uploadFun(this).mounted(this).handleBind(this);
    },
    created: function (opt) {
      
      var id = Util.getRequest('id');
      
      IcController.getData(apis.authInfo, 'GET', {token: opt.token}, function (res) {
        
        if (res.errcode === 0) {
          
          //更新时间
          $('.time').html('更新时间:' + res.data.update_time);
          //更新认证信息
          if (id) {
            $(".certisno").show();
            layui.form.val("editauth", {
              "auth_type": res.data.auth_type,
              "company_type": res.data.company_type,
              "company_name": res.data.company_name
            });
            if (res.data.auth_img) {
              
              CertificationController.imgUrl=res.data.auth_img;
              var getTpl = piclist.innerHTML;
              layui.laytpl(getTpl).render(CertificationController.imgUrl, function (html) {
                if (CertificationController.imgUrl.length < 6) {
                  $("#uploadbox").show();
                } else {
                  $("#uploadbox").hide();
                }
                $("#pp").empty().html(html);
              });
              
              
            }
            layui.form.render();
          } else {
            if (res.data) {
              $(".certis").show();
              var getTpl = listHtml.innerHTML;
              layui.laytpl(getTpl).render(res.data, function (html) {
                $(".certis").empty().html(html);
              });
            } else {
              $(".certisno").show();
            }
          }
          
        } else if (res.errcode === 107012) {
          $(".certisno").show();
        }
        
      });
      
      
      return this;
    },
    mounted: function (opt) {
      
      return this;
    },
    uploadFun: function () {
      var self = this;
      layui.upload.render({
        elem: '#uploadbox',
        url: apis.ossupload,
        multiple: true,
        number: 6,
        data: {
          token: Util.getCookie('token'),
          source: 2
        },
        field: "upload",
        before: function (obj) {
          layer.load(1, {
            offset: ['50%', "50%"],
            shade: false
          });
        },
        done: function (res, index, upload) {
          layer.closeAll('loading');
          
          if (CertificationController.imgUrl.length > 5) {
            layer.msg('只允许上传6张图片');
            return false;
          }
          
          if (res.errcode == 103200) {
            CertificationController.imgUrl.push(res.data[0]);
            var getTpl = piclist.innerHTML;
            layui.laytpl(getTpl).render(CertificationController.imgUrl, function (html) {
              $("#pp").empty().html(html);
            });
          } else {
            layer.msg(res.errmsg)
          }
        },
        allDone: function (obj) {
          if (CertificationController.imgUrl.length < 6) {
            $("#uploadbox").show();
          } else {
            $("#uploadbox").hide();
          }
        },
        err: function () {
          layer.closeAll('loading');
          layer.msg('上传失败');
        }
      });
      
      return this;
      
    },
    isIe: function (opt) {
      if (navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE8.0" || navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE9.0")) {
        //ie版本
        $('#fileLoad').show();
        document.domain = cookieHostname; //设置domian跨域拿到iframe的内容
        var iframe = document.getElementById("test");
        if (iframe.attachEvent) {
          iframe.attachEvent("onload", opt.iframeOnload);
        } else {
          iframe.onload = opt.iframeOnload;
        }
        opt.formSubmit();
        $('#mineFiles').on('change', function () {
          layer.load(1, {
            offset: ['50%', "50%"],
            shade: false
          });
          $('#fileLoad').submit();
        })
      } else {
        //非ie低版本
        $('.sc_div').show();
        opt.uploadFun();
      }
      return this;
    },
    formSubmit: function () {
      $('#fileLoad').submit(function () {
        var uploadFileForm = document.getElementById("fileLoad");
        uploadFileForm.action = apis.ossupload;
        $('#token').val(Util.getCookie('token'));
        $('#ie').val(cookieHostname);
      })
    },
    iframeOnload: function () {
      //表单上传完成后进入iframe获取返回的值
      var iframe = document.getElementById("test");
      var html = "",
        json = null;
      try {
        var el = (iframe.contentWindow || iframe.contentDocument.parentWindow).document.body;
        while (el && el.nodeType !== 3) {
          el = el.firstChild;
        }
        html = el.nodeValue;
        json = html ? eval("(" + html + ")") : false;
      } catch (e) {
        console.log(e)
        layer.closeAll('loading');
        layer.msg('上传失败');
      }
      if (json) {
        layer.closeAll('loading');
        if (json.errcode == 103200) {
          $('#fileLoad').hide();
          $('.upload_result').show();
          $('.upload_result img').attr('src', json.data[0])
          CertificationController.imgUrl = json.data[0] || '';
        } else {
          layer.msg(json.errmsg)
          
        }
      }
    },
    handleBind: function (opt) {
      
      //认证类型
      layui.form.on('radio(auth_type)', function (data) {
        
        var value = data.value;
        
        if (value == 1) {
          $(".txx").text('*请上传身份证、驾驶证、工牌、名片等扫描图片或照片,所提交资料越详尽,审核通过越快(单张图片大小不超过2M)');
        } else if (value == 2) {
          $(".txx").text('*请上传企业营业执照、商用租赁合同、相关证明资料等扫描图片或照片,所提交资料越详尽,审核通过越快(单张图片大小不超过2M)');
        }
        
        var getTpl = certtHtml.innerHTML;
        
        layui.laytpl(getTpl).render(data.value, function (html) {
          
          $(".cert_data_box").empty().html(html);
          
          layui.form.render();
          
        });
        
      });
      
      //新增会员认证信息
      layui.form.on('submit(addauth)', function (data) {
        
        $(data.elem).addClass("layui-btn-disabled");
        
        if (!opt.isClick) {
          
          opt.isClick = true;
          
          
          var params = $.extend({}, data.field, {token: opt.token, auth_img: JSON.stringify(opt.imgUrl)}); //新增参数
          
          IcController.getData(apis.addAuth, 'GET', params, function (res) {
            
            if (res.errcode === 0) {
              
              layer.msg(res.errmsg, {time: 2000}, function () {
                
                window.location.href = '/cert';
                
              })
              
            } else {
              
              layer.msg(res.errmsg);
              
              opt.isClick = false;
              
              $(data.elem).removeClass("layui-btn-disabled");
              
            }
          });
        }
      });
      
      //更新认证信息按钮
      $(document).on('click', '.edit_info', function (e) {
        
        var id = $(this).attr('data-id');
        
        window.location.href = '/cert?id=' + id;
        
        
      });
      
      //删除图片
      $('.certification_content').on('click', '.deleteicon', function () {
        /*if (navigator.appName == "Microsoft Internet Explorer" && (navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE8.0" || navigator.appVersion.split(";")[1].replace(/[ ]/g, "") == "MSIE9.0")) {
          //ie版本
          $('#fileLoad').show();
        } else {
          //非ie低版本
          $('#upload_section').show();
        }
        $('.upload_result').hide();
        $('.upload_result img').attr('src', '');
        opt.imgUrl = "";*/
        
        var index = $(this).parent().parent().attr('data-index');
        var getTpl = piclist.innerHTML;
        
        CertificationController.imgUrl.splice(index, 1);
        
        layui.laytpl(getTpl).render(CertificationController.imgUrl, function (html) {
          $("#pp").empty().html(html);
        });
  
        if (CertificationController.imgUrl.length < 6) {
          $("#uploadbox").show();
        } else {
          $("#uploadbox").hide();
        }
        
        
      })
      
      return this;
    },
  }, $(function () {
    CertificationController.init();
  })
}();