/** 
    @ Name : Careff Javascript Library 
    @ Author : 
    @ Date : 2017-5-1
    @ Email : 
    @ Version : 1.0 
    @ Update : 
    功能： 开始建立自己的JS类库，需要按照功能进行分类；
     */
(function () {
    if (!window.ussave) {
        window.ussave = {};
    };
    window.ussave = {
        NAME: "ussave",
        VERSION: '1.0',
        DATE: '2017-12-16',
        INDEX:10,
        /** 
       @ Name :保存数据
       @ Update : 
       */
        UsSaveEditValue: function (sdata) {

            //alert(JSON.stringify(sdata));
            var url = urlAdd;
            if (paraID != null) {
                url = urlUpdate + "&id=" + paraID;
            }
            sdata = JSON.stringify(sdata);
            ushttp.UsAjaxJSON(url, "" + sdata, true,
                function (data) {
                    //alert(JSON.stringify(data));
                    var d = US.BASIC.UsParseJSON(data);
                    if (d.result == 0) {
                        //US.BASIC.UsAlert2017("系统提示", JSON.stringify(data));
                        //parent.UsRefreshTable(data.msg);
                        //var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        //parent.layer.close(index); //再执行关闭 
                        //parent.UsRefreshMsg(data.msg);
                        try {
                            if (typeof parent.UsRefreshMsg() != "undefined") {
                                parent.UsRefreshMsg(data.msg);
                            }
                            //
                        } catch (e) {
                            US.BASIC.UsAlert2018(data.msg);
                        }
                        //US.BASIC.UsAlert2018(data.msg);

                    }
                    else {
                        //parent.UsRefreshMsg(data.msg);
                        US.BASIC.UsAlert2018(data.msg);
                    }
                });
        },
        /** 
       @ Name :获取数据字典
       @ Update : 
       */
        UsGetDictionary: function (form, _usselecturl, callback) {
            //console.debug(form);
            var code = "";
            $("select[data-dict]").each(function (i, n) {
                var info = $(this).attr("data-dict");
                var text = $(this).find("option").val();//如果是空，则不删除，为了让用户增加请选择
                if (text != '') {
                    $(this).find("option").remove();
                }

                code += info + ",";
            });

            if (code == "") {
                ussave.UsGetSelect(form,_usselecturl, function () { callback(); });
                return;
            }
            var data = '{"idlist":"' + code + '","oprkey":"dictionary","other":""}';
            ushttp.UsAjaxJSON("/usmain/usmain/usgetdictionary", data, true,
                function (data) {
                    $.each(data, function (i, item) {

                        var CodeOf = item.CodeOf;
                        var S = "select[data-dict='" + CodeOf + "']";
                        var S1 = "select[data-dict='" + CodeOf + "'] option";
                        //alert($(S1).length)
                        if ($(S1).length == 0) {
                            $(S).append("<option value='" + item.CodeID + "' selected='selected'>" + item.CodeName + "</option>");
                            //alert();
                        }
                        else {
                            $(S).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
                        }
                        //console.debug($(S).html());
                    });
                    if (form != null) {
                        form.render("select");
                    }
                    //自动填充自定义属性
 
                    ussave.UsToolsEditDict(form);
                    ussave.UsGetSelect(form,_usselecturl, function () { callback(); });
                });
        }
            ,
        /** 
      @ Name :获取Select
      @ Update : 
      */
        UsGetSelect: function (form,_usselecturl, callback) {
            var code = "";
            $("select[data-select]").each(function (i, n) {
                var info = $(this).attr("data-select");
                var text = $(this).find("option").val();//如果是空，则不删除，为了让用户增加请选择
                if (text != '') {
                    $(this).find("option").remove();
                }

                code += info + ",";
            });
             
            if (code == "") {
                callback();
                return;
            }
            var data = '{"idlist":"' + code + '","oprkey":"select","other":""}';
            ushttp.UsAjaxJSON(_usselecturl, data, true,
                function (data) {
                    
                    $.each(data, function (i, item) {
                        
                        var CodeOf = item.CodeOf;
                        var S = "select[data-select='" + CodeOf + "']";
                        var S1 = "select[data-select='" + CodeOf + "'] option";
                        //alert($(S1).length)
                        if ($(S1).length == 0) {
                            $(S).append("<option value='" + item.CodeID + "' selected='selected'>" + item.CodeName + "</option>");
                            //alert();
                        }
                        else {
                            $(S).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
                        }
                        //console.debug($(S).html());
                        
                    });
                    
                    //console.debug("form1");
                    if (form != null) {
                        //console.debug("form");
                        form.render("select");
                    }
                    callback();
                });

        }
            , UsVerson: function () {
                return US.VERSION;
            }
        //自动填充Select,d=数据 cid=用户控件，不需要带#,value字段名valuename
        ,UsToolsDrpSetValue: function (d1, cid, value, name, form) {
                var ncid = "#" + cid;
                $(ncid).empty();

                var d = d1;
                $.each(d, function (i, item) {
                    $(ncid).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
                });
                $(ncid + " option:last").attr("selected", true);

                if (form != null)
                {
                    form.render();
                }
            }, UsAutoGet: function (value, selectsavekey, selecturl, form, cb) {
                var skey = value;
                usbasic.UsOpenLoad();
                var data1 = "{'key':'" + skey + "'}";
                ushttp.UsAjaxJSONV2(selecturl, data1, false, true, function (config) {
                    //console.debug(config)
                    ussave.UsToolsDrpSetValueAuto(selectsavekey, config, form)
                    if (cb != null) {
                        cb();
                    }
                    usbasic.UsCloseLoad();
                });
            }
        , UsAutoGetID: function (value, selectsavekey, selecturl, form, cb) {
            var skey = value;
            usbasic.UsOpenLoad();
            var data1 = "{'key':'" + skey + "'}";
            ushttp.UsAjaxJSONV2(selecturl, data1, false, true, function (config) {
                //console.debug(config)
                ussave.UsToolsDrpSetValueAutoID(selectsavekey, config, form)
                if (cb != null) {
                    cb();
                }
                usbasic.UsCloseLoad();
            });
        }, UsToolsDrpSetValueAutoID: function (tname, tdate, form) {
            var ncid = "#"+tname;
            $(ncid).empty();
            //console.debug($(ncid));
            var d = tdate;
            $.each(d, function (i, item) {
                $(ncid).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
            });
            $(ncid + " option:first").attr("selected", true);

            if (form != null) {
                form.render();
            }
        }
       , UsToolsDrpSetValueAuto: function (tname, tdate, form) {
           var ncid = "select[name='" + tname + "']";
           $(ncid).empty();
           //console.debug($(ncid));
           var d = tdate;
           $.each(d, function (i, item) {
               $(ncid).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
           });
           $(ncid + " option:first").attr("selected", true);

           if (form != null) {
               form.render();
           }
       }/** 
      @ Name :数据字典自动增加配置
      @ Update : 
      */
       , UsToolsEditDict: function (form) {

           //alert(_uinfo.roleid)
           //console.debug(_uinfo.roleid)

           //只有管理员可以编辑
           if (_uinfo.roleid != "1002") {
               return;
           }
           var pageurl = window.location.href.toLowerCase();
           //只有编辑页面可以
           if (pageurl.indexOf("edit") > 0 && pageurl.indexOf("ustype") > 0) {
               //可以编辑
           }
           else {
               return;
           }
           //console.debug(pageurl)
            
            var uskey = "_1_2_a_$_8_3_7_g_5_m_B";//自定义Key,不能和系统的重复
            var uskeyoption = "<option value='" + uskey + "'>" + "自定义属性" + "</option>";
            $("select[data-dict]").each(function (i, n) {
                var info = $(this).attr("data-dict");
                var _sinfo = info.toLowerCase();
                //需要排除一些字典，方式都设置
                if (",skydidcode,skytid,codeyesno,ustate2013,sysisselect,workflowstate,studentexpulsion,rstate,roleid wxcolumntype".indexOf(_sinfo) < 0) {
                    //console.debug(info)
                    $(this).append(uskeyoption);
                }
                
                
            });
            form.render("select");//不绘制的话，增加页面不显示；
            form.on('select', function (data) {
            //console.debug(data.value);
            if (data.value == uskey) {
                var dict = $(data.elem).attr("data-dict");
                //打开设置页面
                var url = "/usmain/systemdictionary/onelist?useof=" + dict;
                usopenvieweducbself(url, "&#xe67f;", "修改数据字典配置", "w3", "660", function () {
                    //关闭后，重新加载页面里面的数据字典;
                    var data = '{"idlist":"' + dict + '","oprkey":"dictionary","other":""}';
                    ushttp.UsAjaxJSON("/usmain/usmain/usgetdictionary", data, true,
                        function (data) {
                            //清除所有字典；
                            var kselect = "select[data-dict='" + dict + "']";
                            $(kselect + " option").remove();
                            //插入数据
                            $.each(data, function (i, item) {
                                var CodeOf = item.CodeOf;
                                var S = "select[data-dict='" + CodeOf + "']";
                                var S1 = "select[data-dict='" + CodeOf + "'] option";
                                if ($(S1).length == 0) {
                                    $(S).append("<option value='" + item.CodeID + "' selected='selected'>" + item.CodeName + "</option>");
                                }
                                else {
                                    $(S).append("<option value='" + item.CodeID + "'>" + item.CodeName + "</option>");
                                }
                            });

                            //增加配置
                            $(kselect).append(uskeyoption);
                            
                            if (form != null) {
                                form.render("select");
                            }
                        });
                });
            }
            
        });
       }
    };

})();