/**
 * [OElove] (C)2010-2099 oelove.com  biz
 * Email: service@phpcoo.com ,phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2020/11/07 by BO $
*/
$(function () {
    function Profile () {
        this.helpMeWrite();
        this.calIntro();
        this.submit();
    }
    Profile.prototype = {
      calText: function (s){
          var l = 0;
          var a = s.split("");
          for (var i=0;i<a.length;i++){
              if (a[i].charCodeAt(0)<299){
                l++;
              } else {
                l++;
              }
          }
          return l;
        },
        helpMeWrite: function () {
            var _this = this;
            var is_http = false;

            $('[f="help_me_write"]').on('click', function () {
                var el = this;
                if (is_http) return;
                is_http = true;

                var timer, loading;
                timer = setTimeout(function () {
                    loading = OEUI.loading({
                        el: this
                    });
                    loading.show();
                }, 200);

                $.ajax({
                    type: "POST",
                    url: _OE_FILE + "&c=profile",
                    data: {a:"helpmewrite", datatype:"json"},
                    success: function(json) {
                        is_http = false;
                        loading && loading.remove();
                        clearTimeout(timer);
                        timer = null;
                        var res = JSON.parse(json);
                        if (res.response == "1") {
                            $("#content").val(res.result);
                            var num = _this.calText(res.result);
                            $('#tips_num').html(num);
                        } else {
                            if (res.result.length > 0) {
                                OEUI.message.msg({text: res.result});
                            }
                            else {
                                OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                            }
                        }
                    },
                    error: function() {
                        is_http = false;
                        loading && loading.remove();
                        clearTimeout(timer);
                        timer = null;
                        OEUI.message.msg({text: '网络繁忙，请稍后再试'});
                    }
                });
            });
        },
        calIntro: function () {
            var text = $('#content').val();
            var num = this.calText(text);
            $('#tips_num').html(num);
        },
        submit:function () {
            $('[f="save_profile"]').on('click', function () {
                var intro = $('#content').val();
                if (intro == '') {
                    OEUI.message.msg({text: '请输入个人介绍'});
                    return;
                }
                $('#profile').submit();
            });
        }
    }
    new Profile();
});