/**
 * [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/03/23 by CL $
*/
var _inst_oevar = null;
$(function(){
    
    function OEVar() {
        this.api_url = WAP_APPFILE+'&c=var';
        this.var_dialog = null;
        this.slide_time = 300;
        this.callback = ''; //回调函数
        this.elm = ''; //当前元素
        
        this.varMulti();
        this.varAbove();
        this.varInput();
        this.varTextArea();
        this.varSchool();
    }
    
    OEVar.prototype = {
        
        //多选
        varMulti:function() {
            var _this = this;
            //弹出
            $(document).on("click", "[f='get_var_multi']", function(){
                var $item = $(this).attr("data-item");
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $title = $(this).attr("data-title");
                if (typeof($title) == 'undefined') {
                    $title = '';
                }
                //回调
                var $callback = $(this).attr("data-callback");
                if (typeof($callback) == 'undefined') {
                    $callback = '';
                }
                _this.callback = $callback;
                _this.elm = $(this);
                
                var $value = $("#"+$name).val();
                
                var $send_data = {
                    'a':'varmulti', 
                    'item':$item, 'name':$name, 'value':$value, 'text':$text, 'title':$title
                };
                _this.getVarMulti($send_data);
                
            });
            //关闭
            $(document).on("click", "[f='close_var_multi']", function(){
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
            });
            
            //选中
            $(document).on("click", "[f='sel_var_multi']", function(){
                if ($(this).hasClass('current')) {
                    $(this).removeClass('current');
                }
                else {
                    $(this).addClass('current');
                }
            });
            //确定
            $(document).on("click", "[f='confirm_var_multi']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                
                var $vals = '';
                var $texts = '';
                
                $("[f='var_multi_item'] span").each(function(iv, elm){
                    if ($(this).hasClass('current')) {
                        var $val = $(this).attr("data-val");
                        var $txt = $(this).attr("data-text");
                        if ($vals != '') {
                            $vals += ','+$val;
                        }
                        else {
                            $vals = $val;
                        }
                        if ($texts != '') {
                            $texts += ' '+$txt;
                        }
                        else {
                            $texts = $txt;
                        }
                    }
                });
                $("#"+$name).val($vals);
                if ($texts == '') {
                    if ($text != '') {
                        $texts = $text;
                    }
                }
                $("#"+$name+"_result").html($texts);
                
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
                
                //回调
                var $callback_data = {
                    'value':$vals, 'text':$texts
                };
                if (typeof window[_this.callback] == 'function') {
                    window[_this.callback]($callback_data, _this.elm);
                }
            });
        }, 
        getVarMulti:function($send_data) {
            var _this = this;
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.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") {
                        $("#var_multi_data").html($result);
                        _this.var_dialog = OEUI.Dialog({
                            el: "#var_multi_wrap",
                            animated: 'slideBottomIn',
                            time:_this.slide_time
                        });
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //单选+或以上
        varAbove:function() {
            var _this = this;
            //弹出
            $(document).on("click", "[f='get_var_above']", function(){
                var $item = $(this).attr("data-item");
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $title = $(this).attr("data-title");
                if (typeof($title) == 'undefined') {
                    $title = '';
                }
                //回调
                var $callback = $(this).attr("data-callback");
                if (typeof($callback) == 'undefined') {
                    $callback = '';
                }
                _this.callback = $callback;
                _this.elm = $(this);
                
                
                var $value1 = $("#"+$name+"1").val();
                var $value2 = $("#"+$name+"2").val();
                
                var $send_data = {
                    'a':'above', 
                    'item':$item, 'name':$name, 'value1':$value1, 'value2':$value2, 
                    'text':$text, 'title':$title
                };
                _this.getVarAbove($send_data);
                
            });
            //关闭
            $(document).on("click", "[f='close_var_above']", function(){
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
            });
            //单选
            $(document).on("click", "[f='sel_var_radio']", function(){
                if ($(this).hasClass('current')) {
                    $("[f='sel_var_radio']").removeClass('current');
                }
                else {
                    $("[f='sel_var_radio']").removeClass('current');
                    $(this).addClass('current');
                }
            });
            //勾选或以上
            $(document).on("click", "[f='sel_var_above']", function(){
                if ($(this).hasClass('color_main')) {
                    $(this).removeClass('color_main').addClass('color_9');
                    $(this).find("i").removeClass('color_main').addClass('color_9');
                }
                else {
                    $(this).removeClass('color_9').addClass('color_main');
                    $(this).find("i").removeClass('color_9').addClass('color_main');
                }
            });
            //确定
            $(document).on("click", "[f='confirm_var_above']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                
                var $val = '';
                var $txt = '';
                $("[f='var_radio_item'] span").each(function(iv, elm){
                    if ($(this).hasClass('current')) {
                        $val = $(this).attr("data-val");
                        $txt = $(this).attr("data-text");
                        return true;
                    }
                });
                var $val2 = '';
                //勾选或以上
                if ($("[f='sel_var_above']").hasClass("color_main")) {
                    $val2 = '1';
                }
                else {
                    $val2 = '0';
                }
                
                if ($txt == '') {
                    if ($text != '') {
                        $txt = $text;
                    }
                }
                else {
                    if ($val2 == '1') {
                        $txt += "(及以上)";
                    }
                }
                $("#"+$name+"1").val($val);
                $("#"+$name+"2").val($val2);
                $("#"+$name+"_result").html($txt);
                
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
                
                //回调
                var $callback_data = {
                    'value1':$val, 'value2':$val2, 'text':$txt
                };
                if (typeof window[_this.callback] == 'function') {
                    window[_this.callback]($callback_data, _this.elm);
                }
                
            });
        },
        getVarAbove:function($send_data) {
            var _this = this;
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.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") {
                        $("#var_above_data").html($result);
                        _this.var_dialog = OEUI.Dialog({
                            el: "#var_above_wrap",
                            animated: 'slideBottomIn',
                            time:_this.slide_time
                        });
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        
        //input输入框
        varInput:function() {
            var _this = this;
            //弹出
            $(document).on("click", "[f='get_var_input']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $title = $(this).attr("data-title");
                if (typeof($title) == 'undefined') {
                    $title = '';
                }
                //回调
                var $callback = $(this).attr("data-callback");
                if (typeof($callback) == 'undefined') {
                    $callback = '';
                }
                _this.callback = $callback;
                _this.elm = $(this);
                
                var $value = $("#"+$name).val();
                
                var $send_data = {
                    'a':'input', 
                    'name':$name, 'value':$value, 'text':$text, 'title':$title
                };
                _this.getVarInput($send_data);
                
            });
            //关闭
            $(document).on("click", "[f='close_var_input']", function(){
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
            });
            //确定
            $(document).on("click", "[f='confirm_var_input']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $input_value = $("#var_input_content").val();
                $("#"+$name).val($input_value);
                $("#"+$name+"_result").html($input_value);
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
                
                //回调
                var $callback_data = {
                    'value':$input_value, 'text':$input_value
                };
                if (typeof window[_this.callback] == 'function') {
                    window[_this.callback]($callback_data, _this.elm);
                }
                
            });
        }, 
        getVarInput:function($send_data) {
            var _this = this;
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.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") {
                        $("#var_input_data").html($result);
                        _this.var_dialog = OEUI.Dialog({
                            el: "#var_input_wrap",
                            animated: 'slideBottomIn',
                            time:_this.slide_time
                        });
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        
        //textarea输入框
        varTextArea:function() {
            var _this = this;
            //弹出
            $(document).on("click", "[f='get_var_textarea']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $title = $(this).attr("data-title");
                if (typeof($title) == 'undefined') {
                    $title = '';
                }
                //回调
                var $callback = $(this).attr("data-callback");
                if (typeof($callback) == 'undefined') {
                    $callback = '';
                }
                _this.callback = $callback;
                _this.elm = $(this);
                
                var $value = $("#"+$name).val();
                
                var $send_data = {
                    'a':'textarea', 
                    'name':$name, 'value':$value, 'text':$text, 'title':$title
                };
                _this.getVarTextArea($send_data);
                
            });
            //关闭
            $(document).on("click", "[f='close_var_textarea']", function(){
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
            });
            //确定
            $(document).on("click", "[f='confirm_var_textarea']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $input_value = $("#var_textarea_content").val();
                $("#"+$name).val($input_value);
                $("#"+$name+"_result").html($input_value);
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
                
                //回调
                var $callback_data = {
                    'value':$input_value, 'text':$input_value
                };
                if (typeof window[_this.callback] == 'function') {
                    window[_this.callback]($callback_data, _this.elm);
                }
                
            });
        }, 
        getVarTextArea:function($send_data) {
            var _this = this;
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.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") {
                        $("#var_textarea_data").html($result);
                        _this.var_dialog = OEUI.Dialog({
                            el: "#var_textarea_wrap",
                            animated: 'slideBottomIn',
                            time:_this.slide_time
                        });
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //毕业院校联动选择
        varSchool:function() {
            var _this = this;
            //弹出
            $(document).on("click", "[f='get_var_school']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                var $title = $(this).attr("data-title");
                if (typeof($title) == 'undefined') {
                    $title = '';
                }
                //回调
                var $callback = $(this).attr("data-callback");
                if (typeof($callback) == 'undefined') {
                    $callback = '';
                }
                _this.callback = $callback;
                _this.elm = $(this);
                
                var $areaid = $("#"+$name+"area1").val();
                var $schid = $("#"+$name+"id").val();
                
                var $send_data = {
                    'a':'school', 
                    'name':$name, 'areaid':$areaid, 'schid':$schid, 'text':$text, 'title':$title
                };
                _this.getVarSchool($send_data);
                
            });
            //关闭
            $(document).on("click", "[f='close_var_school']", function(){
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
            });
            
            //联动
            $(document).on("click", "[f='var_school_child']", function(){
                var $areaid = $(this).attr("data-areaid");
                var $schid = $(this).attr("data-schid");
                if (typeof($schid) == 'undefined') {
                    $schid = '';
                }
                $("[f='var_school_child']").removeClass('bg_f color_f40 fb current');
                $(this).addClass('bg_f color_f40 fb current');
                var $send_data = {
                    'a':'schoolchild', 
                    'areaid':$areaid, 'schid':$schid
                };
                _this.getVarSchoolChild($send_data);
            });
            //选中学校
            $(document).on("click", "[f='sel_var_school']", function(){
                $("[f='sel_var_school']").removeClass('color_f40 fb current');
                $(this).addClass('color_f40 fb current');
            });
            
            
            //确定
            $(document).on("click", "[f='confirm_var_school']", function(){
                var $name = $(this).attr("data-name");
                var $text = $(this).attr("data-text");
                if (typeof($text) == 'undefined') {
                    $text = '';
                }
                //地区ID
                var $areaid = '';
                $("#var_school_area_data div").each(function(iv, elm){
                    if ($(this).hasClass('current')) {
                        $areaid = $(this).attr("data-areaid");
                        return;
                    }
                });
                //院校ID
                var $schid = '';
                var $schname = '';
                $("#var_school_list_data div").each(function(iv, elm){
                    if ($(this).hasClass('current')) {
                        $schid = $(this).attr("data-val");
                        $schname = $(this).attr("data-text");
                        return;
                    }
                });
                
                $("#"+$name+"area1").val($areaid);
                $("#"+$name+"id").val($schid);
                $("#"+$name+"_result").html($schname);
                if (_this.var_dialog) {
                    _this.var_dialog.hide();
                }
                
                //回调
                var $callback_data = {
                    'value1':$areaid, 'value2':$schid, 'text':$schname
                };
                if (typeof window[_this.callback] == 'function') {
                    window[_this.callback]($callback_data, _this.elm);
                }
                
            });
        }, 
        getVarSchool:function($send_data) {
            var _this = this;
            var loading = null;
            var timer = setTimeout(function () {
                loading = OEUI.showLoading({
                    type:2
                });
            }, 200);
            $.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") {
                        $("#var_school_data").html($result);
                        _this.var_dialog = OEUI.Dialog({
                            el: "#var_school_wrap",
                            animated: 'slideRightIn',
                            time:_this.slide_time
                        });
                        _this.scrollSchoolArea();
                        _this.scrollSchoolList();
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        //联动院校
        getVarSchoolChild:function($send_data) {
            var _this = this;
            $.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;
                    
                    if ($response == "1") {
                        $("#var_school_list_data").html($result);
                    }
                },
                error: function() {
                    OEUI.toast({
                        type: 'bottom',
                        text: '网络繁忙，请稍后再试！'
                    });
                }
            });
        },
        
        //地区滚动
        scrollSchoolArea:function() {
            var select = $('.current[f="var_school_child"]');
            var height = window.screen.height
            if(select.length > 0) {
                var top = select.offset().top;
                if(top > height- 100) {
                    $('#var_school_area_data').scrollTop(top - height /2);
                }
            }
        },
        //院校滚动
        scrollSchoolList:function() {
            var select = $('.current[f="sel_var_school"]');
            var height = window.screen.height
            if(select.length > 0) {
                var top = select.offset().top;
                if(top > height- 100) {
                    $('#var_school_list_data').scrollTop(top - height /2);
                }
            }
        }
        
        
    }
    
    _inst_oevar = new OEVar();
    
});
