<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="utf-8" %>
<%@include file="/WEB-INF/ehr/commons/jsp/commonInclude.jsp" %>
<script type="text/javascript">

    /* *************************** 模块初始化 ***************************** */
    var oldFileId = "";
    var editorParam = {
        //form
        actionUrl: "${contextRoot}/app/addApp",

        init: function () {
            this.toDisable();
            this.bindEvents();
            this.initForm();
        },
        toDisable: function () {
            if ($("#flag").val() == 'disabled') {
                $("#btnEditor").show();
                $("#btnSave").hide();
                $("input[name='name']").attr("disabled", "disabled");
                $("input[name='code']").attr("disabled", "disabled");
                $("input[name='icon']").attr("disabled", "disabled");
                $($(".btnGrayDo")[0]).attr("disabled", "disabled");
                $("input[name='appUrl']").attr("disabled", "disabled");
                $("input[name='developer']").attr("disabled", "disabled");
                $("input[name='appKey']").attr("disabled", "disabled");
                $("input[name='appSecret']").attr("disabled", "disabled");
                $("input[name='status']").attr("disabled", "disabled");
            }else{
                $("#btnEditor").hide();
                $("#btnSave").show();
            }
        },
        initForm: function () {
            var me = this;
            var data;
            var modelString = "${model.id}";
            if (modelString != undefined && modelString != null && modelString.length > 0) {
                var status = "${model.status}";
                liger.get("status").selectValue(status);
                data = {
                    id: "${model.id}",
                    name: "${model.name}",
                    code: "${model.code}",
                    status: status,
                    developer: "${model.developer}",
                    icon: "${model.icon}",
                    appUrl: "${model.appUrl}",
                    appKey: "${model.appKey}",
                    appSecret: "${model.appSecret}",
                };

                me.actionUrl = "${contextRoot}/app/updateApp";

                //旧图ID
                if("${model.icon}"!=""){
                    oldFileId = "${model.icon}";
                }

            } else {
                liger.get("status").selectValue("1");//默认有效
            }
            $("#div_info_form").ligerAutoForm({
                data: data,
                validate: {
                    name: "required",
                    appUrl: "required",
                    developer: "required",
                    code: "required",
                    icon: {
                        required: true
                    },
                    appKey: "required",
                    appSecret: {
                        required: true
                    }

                },

            });
            var iconVal = $("#name_icon").val();
            if (iconVal != '') {
                $("#form1").closest("div").find("span").html("重新上传<i></i>");
            }
        },
        bindEvents: function () {
            var me = this;
            $(".m-form-bottom").on("click", "#btnSave", function () {
//                $("#btnSave").css("pointer-events", "none");
                $("#name_icon").removeClass("required");
                if ($("#name_icon").val() == "" || $("#name_icon").val().indexOf("fakepath")>0) {
                    $("#name_icon").addClass("required");
                    $.ligerDialog.error("请上传图标");
                    if (!$("#div_info_form").ligerAutoForm("validate")) {
                        return;
                    }
                    return;
                }
                if (!$("#div_info_form").ligerAutoForm("validate")) {
                    return;
                }
                var data = $("#div_info_form").ligerAutoForm("getData");
                $.ajax({ //ajax处理
                    type: "POST",
                    url: me.actionUrl,
                    dataType: "json",
                    data: data,
                    cache: false,
                    success: function (data) {
                        if (data.successFlg) {
                            parent.app.dialogSuccess(data.message);
                        }
                        else {
                            $.ligerDialog.error(data.message);
                        }
                        $("#btnSave").css("pointer-events", "");
                    },
                    error: function (data) {
                        $.ligerDialog.error("Status:" + data.status + "(" + data.statusText + ")");
                        $("#btnSave").css("pointer-events", "");
                    }
                });
            });
            $(".m-form-bottom").on("click", "#btnEditor", function () {
                $("#btnEditor").hide();
                $("#btnSave").show();
                $("input[name='name']").removeAttr("disabled");
                $("input[name='code']").removeAttr("disabled");
                $("input[name='icon']").removeAttr("disabled");
                $($(".btnGrayDo")[0]).removeAttr("disabled");
                $("input[name='appUrl']").removeAttr("disabled");
                $("input[name='developer']").removeAttr("disabled");
                $("input[name='appKey']").removeAttr("disabled");
                $("input[name='appSecret']").removeAttr("disabled");
                $("input[name='status']").removeAttr("disabled");
                $("#flag").val("");
            });
            $(".m-form-bottom").on("click", "#btnCancel", function () {
                parent.app.dialog.close();
            });

            $("#status").ligerComboBox({
                data: [{"value": "有效", "code": "1"}, {"value": "无效", "code": "0"}],
                cancelable: false,
                onSuccess: function (data) {
                }
            });

            $("#form1").on("click", ".btnGrayUp", function () {
                var inputVal = $(this).closest("div").find("input[type=text]");
                var obj = $(this).closest("div").find("input[type=file]")
                obj.change(function () {
                    inputVal.val(obj.val());
                });
                obj.click();
            });
            $(".btnGrayDo").bind("click", function () {
                $("#name_icon").removeClass("required");
                $("#btnSave").css("pointer-events", "");
                if ($("#flag").val() == 'disabled') {
                    return;
                }
                var id = $(this).attr("data-id");
                var formId = '#form' + id;
                var formObj = $(formId);//要提交的表单
                var Obj = $(".btnGrayDo");
                var upObj = formObj.closest("input").find(".btnGrayUp");//浏览的按钮
                var formsay = formObj.find(".say" + id);//提示文字
                if (formObj.closest("div").find("input[type=text]").val().length != 0) {//判断是否选择了文件
                    formsay.html("上传中");
                    formObj.ajaxForm({
                        dataType: 'text',
                        type: "POST",
                        success: function processJson(data) {
                            // 上传成功后删除旧的
                            console.log(oldFileId);
                            if(oldFileId){
                                _deleteFileAfterUpdate(oldFileId);
                            }

                            formsay.html("上传成功");
                            formObj.find("input[type=text]").attr("data-id", data)//设置已上传的文件的ID
                            formObj.find("input[type=text]").val(data)//设置已上传的文件的ID
                            if(data == ""){
                                $("#img_icon").hide();//隐藏图标
                            }else{
                                $("#img_icon").show();//显示图标
                                $("#img_icon").attr("src", "${dfs}/" + data)//设置已上传的文件的ID
                            }
                            formObj.closest("div").find("span").html("重新上传<i></i>");
                            //console.log(eval('('+data.data+')'));
                        },
                        error: function processJson(data) {
                            formsay.html("上传失败");
                        }
                    });
                    formObj.submit();
                } else {
                    $.ligerDialog.error("请选择要上传的文件");
                }
            });

        }
    };

    function _deleteFileAfterUpdate(fileId){
        if(null == fileId || fileId.length == 0){
            return;
        }
        var url = "${dfs}/" + fileId;
        $.ajax({
            url: url,
            type: 'DELETE',
            async: false,
            success: function(){
                console.log("旧文件删除成功");
            }
        });
    }

    $(function () {
        editorParam.init();

    });
</script>
<script type="text/javascript" src="${staticRoot}/lib/jquery/jqueryform.js"></script>
