/**
 * [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/05/07 by CL $
*/
var _inst_oeadddy = null;
$(function() {
    
    //发布类
    function OEAdd() {
        this._this_dialog = null;
        this.slide_time = 200; //0.05.07
        this.api_url = WAP_APPFILE + "&c=dynamic";
        this.up_elm = 'but_init_up'; //上传按钮ID元素
        this.up_maxnum = 9; //最多上传张数
        this.photo_data = 'photo_list'; //图集容器
        
        this.wake();
        this.clickFace();
    }
    
    
    OEAdd.prototype = {
        //触发
        wake:function() {
            var _this = this;
            
            //弹出发表框
            $(document).on("click", "[f='but_open_adddynamic']", function(){
                if (_LOGIN_STATUS == '0') {
                    _this.login();
                    return;
                }
                _this.initAdd();
            });
            //关闭窗
            $(document).on("click", "[f='but_close_adddynamic']", function(){
                if (_this._this_dialog) {
                    _this._this_dialog.hide();
                }
            });
            //隐藏表情框
            $(document).on("click", "#content", function(){
                $("#face_box").hide();
            });
            //统计字数
            $(document).on("keyup", "#content", function(){
                _this.countSize();
            });
            $(document).on("keydown", "#content", function(){
                _this.countSize();
            });
            
            //点击上传
            $(document).on("click", "#"+_this.up_elm, function(){
                var $has_click = $(this).attr("data-hasclick");
                if (typeof($has_click) == 'undefined') {
                    $has_click = '0';
                }
                //微信还是H5端上传
                var $up_from = $(this).attr("data-up");
                if (typeof($up_from) == 'undefined') {
                    $up_from = '';
                }
                if ($up_from == 'wx') {
                    $(this).attr("f", "but_upload_wximage");
                }
                else {
                    $(this).attr("f", "but_upload");
                }
                
                //判断是否已达上限
                var $up_nums = 0;
                $("#"+_this.photo_data+" img").each(function(iv, el){
                    $up_nums++;
                });
                if ($up_nums >= _this.up_maxnum) {
                    $(this).attr("f", "");
                    OEUI.toast({
                        type: 'bottom', 
                        text: '最多只能上传'+_this.up_maxnum+'张图片哦！'
                    });
                    return;
                }
                
                if ($has_click != '1') {
                    $(this).attr("data-hasclick", "1");
                    $(this).click();
                }
            });
            //点击移除图片
            $(document).on("click", "[f='but_remove_img']", function(){
                $(this).remove();
                //判断是否已达上限
                var $up_nums = 0;
                $("#"+_this.photo_data+" img").each(function(iv, el){
                    $up_nums++;
                });
                var $up_from = $("#"+_this.up_elm).attr("data-up");
                if (typeof($up_from) == 'undefined') {
                    $up_from = '';
                }
                if ($up_nums < _this.up_maxnum) {
                    if ($up_from == 'wx') {
                        $("#"+_this.up_elm).attr("f", "but_upload_wximage");
                    }
                    else {
                        $("#"+_this.up_elm).attr("f", "but_upload");
                    }
                }
                else {
                    $("#"+_this.up_elm).attr("f", "");
                }
            });
            
            //点击发布
            $(document).on("click", "[f='but_submit_add']", function(){
                _this.obj_id = $(this).attr("id");
                var $content = $("#content").val();
                if ($content == '') {
                    OEUI.toast({
                        type: 'bottom', 
                        text: '请填写发布内容'
                    });
                    return;
                }
                
                //图集...
                var $img_data = [];
                var $img2_data = [];
                $("#"+_this.photo_data+" img").each(function(iv, el){
                    $img_data[iv+1] = $(this).attr("src");
                    $img2_data[iv+1] = $(this).attr("data-src");
                });
                var $send_data = {
                    'a':'saveadd', 'content':$content, 
                    'thumbimg':$img_data[1] || '', 'drawimg':$img2_data[1] || '', 
                    'thumbimg2':$img_data[2] || '', 'drawimg2':$img2_data[2] || '', 
                    'thumbimg3':$img_data[3] || '', 'drawimg3':$img2_data[3] || '', 
                    'thumbimg4':$img_data[4] || '', 'drawimg4':$img2_data[4] || '', 
                    'thumbimg5':$img_data[5] || '', 'drawimg5':$img2_data[5] || '', 
                    'thumbimg6':$img_data[6] || '', 'drawimg6':$img2_data[6] || '', 
                    'thumbimg7':$img_data[7] || '', 'drawimg7':$img2_data[7] || '', 
                    'thumbimg8':$img_data[8] || '', 'drawimg8':$img2_data[8] || '', 
                    'thumbimg9':$img_data[9] || '', 'drawimg9':$img2_data[9] || ''
                };
                _this.sendAdd($send_data);
            }); 
        },
        
        //表情处理
        clickFace:function() {
            var _this = this;
            
            //展开/关闭表情
            $(document).on("click", "[f='but_open_face']", function(){
                if ($("#face_box").css("display") == 'none') {
                    $("#face_box").show();
                }
                else {
                    $("#face_box").hide();
                }
            });
            //选择表情
            $(document).on("click", "[f='selectem']", function(){
                var id = $(this).attr("data-id");
                var tag_em = "{emf_"+ id +"}";
                var myField = document.getElementById("content");
                if (document.selection) {
                    //myField.focus();
                    sel = document.selection.createRange();
                    sel.text = tag_em;
                    //myField.focus();
                }
                else if (myField.selectionStart || myField.selectionStart == '0') {
                    var startPos = myField.selectionStart;
                    var endPos = myField.selectionEnd;
                    var cursorPos = endPos;
                    myField.value = myField.value.substring(0, startPos)
                                  + tag_em
                                  + myField.value.substring(endPos, myField.value.length);
                    cursorPos += tag_em.length;
                    //myField.focus();
                    myField.selectionStart = cursorPos;
                    myField.selectionEnd = cursorPos;
                }
                else {
                    myField.value += tag_em;
                    //myField.focus();
                }
                
                _this.countSize(); //统计字数
            });
        },
        
        //统计字数
        countSize:function() {
            var $size = 0;
            var $content = $("#content").val();
            if ($content != '') {
                $size = strQuantity($content);
            }
            $("#tips_size").html($size);
        },
        
        //跳到登录页
        login:function() {
            var _this = this;
            var $back_url = _this.api_url+'&nofollow=1';
            window.location.href = WAP_APPFILE+'&c=passport&a=login&forward='+encodeURIComponent($back_url);
        },
        
        initAdd:function() {
            //判断是否已载入
            var $da = $("#adddynamic_data").html();
            if ($da != '') {
                if ($("#adddynamic_data").find("but_submit_add").length > 0) {
                    _this._this_dialog = OEUI.Dialog({
                        el: '#adddynamic_wrap',
                        animated: 'slideRightIn',
                        time:_this.slide_time
                    });
                    return;
                }
            }
            var _this = this;
            $.ajax({
                type: "POST",
                url: _this.api_url,
                cache: false,
                data: {
                    a:'add'
                },
                dataType: "json",
                success: function($data) {
                    var $json = eval($data);
                    var $response = $json.response;
                    var $result = $json.result;
                    
                    if ($response == "1") {
                        $("#adddynamic_data").html($result);
                        _this._this_dialog = OEUI.Dialog({
                            el: '#adddynamic_wrap',
                            animated: 'slideRightIn',
                            time:_this.slide_time
                        });
                    }
                    else {
                        if ($result.length == 0 || $result == '') {
                            $result = '加载失败，请检查！';
                        }
                        OEUI.toast({
                            type: 'bottom',
                            text: $result
                        }); 
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //保存动态
        sendAdd:function($send_data) {
            var _this = this;
            if (!$("#"+_this.obj_id).hasClass("forbid_submit")) {
                var loading = null;
                var timer = setTimeout(function () {
                    loading = OEUI.showLoading({
                        type:2
                    });
                }, 200);
                $("#"+_this.obj_id).addClass("forbid_submit");
                
                $.ajax({
                    type: "POST",
                    url: _this.api_url,
                    cache: false,
                    data: $send_data,
                    dataType: "json",
                    success: function($data) {
                        var $json = eval($data);
                        var $response = $json.response;
                        var $result = $json.result;
                        
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        
                        if ($response == "1") {
                            OEUI.toast({
                                type: 'bottom',
                                text: '发布成功'
                            });
                            if (_this._this_dialog) {
                                _this._this_dialog.hide();
                            }
                            setTimeout(function(){
                                $("#content").val("");
                                $("#"+_this.photo_data).html("");
                                window.location.reload();
                            }, 800);
                        }
                        else if ($response == '2') {
                            OEUI.toast({
                                type: 'bottom',
                                text: '发布成功，请等待网站审核'
                            });
                            if (_this._this_dialog) {
                                _this._this_dialog.hide();
                            }
                            setTimeout(function(){
                                $("#content").val("");
                                $("#"+_this.photo_data).html("");
                            }, 800);
                        }
                        else {
                            if ($result.length == 0 || $result == '') {
                                $result = '发布失败，请检查！';
                            }
                            OEUI.toast({
                                type: 'bottom',
                                text: $result
                            }); 
                        }
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                    },
                    error: function() {
                        $("#"+_this.obj_id).removeClass("forbid_submit");
                        clearTimeout(timer);
                        loading && loading.hideLoading();
                        timer = null;
                        OEUI.toast({
                            type: 'bottom',
                            text: '网络繁忙，请稍后再试！'
                        });
                    }
                });
            }
        }
    }
    
    _inst_oeadddy = new OEAdd();
});

//上传完后回调
function upCallBack($data) {
    if (typeof($data) == 'undefined') {
        $data = null;
        return;
    }
    
    var $thumbimg = $data.thumbimg;
    var $drawimg = $data.drawimg;
    
    var $html = "<span class='inline_block bo_f7 img_70 lh70 t_center mlr5 mb10 dc oh pr' f='but_remove_img'>"+
                "<i class='iconfont icon-guan_bi pa rt color_f40 lh18 fz18'></i>"+
                "<img class='w100 h_100 ob_c' src='"+$thumbimg+"' data-src='"+$drawimg+"' />"+
                "</span>";
    $("#photo_list").append($html);
    
    //判断是否已经上传9张图
    var $up_nums = 0;
    $("#photo_list img").each(function(iv, el){
        $up_nums++;
    });
    if ($up_nums >= 9) {
        $("#but_init_up").attr("f", "");
    }          
}