/**
 * 通用js
 */

var leftMenu;

jQuery(function ($) {

//定时查询自己的未读的任务，回款，系统信息，消息的数目
    ajaxGetMessages();
    //class为.open-dialog的元素弹出窗口
    $(document).on('click', '.open-dialog', function () {
        var params = {
            url: $(this).data('url'),
            width: $(this).data('width'),
            height: $(this).data('height'),
            title: $(this).data('title'),
            redirectUrl: $(this).data('redirectUrl'),
            isReload: true,
            isSubmit: true,
        }
        if ($(this).data('reload') == false) {
            params.isReload = false;
        }
        if ($(this).data('submit') == false) {
            params.isSubmit = false;
        }
        openDialog(params);
        /*var url = $(this).data('url');
         var width = $(this).data('width');
         var height = $(this).data('height');
         var title = $(this).data('title');
         var isReload = true;
         if ($(this).data('reload') == false) {
         isReload = false;
         }
         var isSubmit = true;
         if ($(this).data('submit') == false) {
         isSubmit = false;
         }
         openDialog(url, width, height, title, isreload, issubmit);*/
    });
    //class为.open-only-see-dialog的元素弹出窗口
    $(document).on('click', '.open-only-see-dialog', function () {
        var params = {
            url: $(this).data('url'),
            width: $(this).data('width'),
            height: $(this).data('height'),
            title: $(this).data('title'),
            isReload: true,
            isSubmit: true
        }
        if ($(this).data('reload') == false) {
            params.isReload = false;
        }
        if ($(this).data('submit') == false) {
            params.isSubmit = false;
        }
        openOnlySeeDialog(params);
    });

    //添加合同
    $(document).on('click', '.open-contract-dialog', function () {
        var params = {
            url: $(this).data('url'),
            width: $(this).data('width'),
            height: $(this).data('height'),
            title: $(this).data('title'),
            isReload: true,
            isSubmit: true,
        }
        if ($(this).data('reload') == false) {
            params.isReload = false;
        }
        if ($(this).data('submit') == false) {
            params.isSubmit = false;
        }
        openContractDialog(params);
    });

    //删除提示
    $(document).on('click', '.link-delete', function () {
        if (!confirm('确认删除?'))
            return false;
    });
    //删除全选
    $(document).on('click', '.box-select-all', function () {
        if ($(this).is(":checked"))
            $(".box-delete").prop('checked', true);
        else
            $(".box-delete").prop('checked', false);
    });
    //批量删除
    $(document).on('click', '.batch-delete', function () {
        var length = $(".box-delete:checked").length;
        if (length == 0) {
            alert('请至少选择一项');
            return false;
        }
        if (!confirm('确认删除?'))
            return false;
        var url = $(this).data('url');
        $("#main-form").attr('action', url).trigger('submit');
    });

    //批量更新地址
    $(document).on('click', '.batch-update-address', function () {
        var length = $(".box-delete:checked").length;
        if (length == 0) {
            alert('请至少选择一项');
            return false;
        }
        if (!confirm('确认更新?'))
            return false;
        var url = $(this).data('url');
        $("#main-form").attr('action', url).trigger('submit');
    });

    //批量更新负责人
    $(document).on('click', '.batch-update-leader', function () {
        var length = $(".box-delete:checked").length;
        if (length == 0) {
            alert('请至少选择一项');
            return false;
        }
        if (!confirm('确认转移?'))
            return false;
        var url = $(this).data('url');
        $("#main-form").attr('action', url).trigger('submit');
    });


    /**
     * 重置操作
     * HuLimin
     */
    $(document).on('click', '.reset_search', function () {
        $('#search-form').find(':input').not(':button,:submit,:reset,:hidden').val('').removeAttr('checked').removeAttr('selected').submit();
    });

    /**
     * 自动给table添加序号，tr内在添加序号的地方加class key_num
     */
    var len = $('table tr').length;
    for (var i = 1; i < len; i++) {
        $('table tr:eq(' + i + ') .key_num').text(i);
    }


});
//弹窗
function openDialog(params) {
    art.dialog.open(params.url, {
        id: 'main-dialog',
        title: params.title,
        lock: true,
        width: params.width || 650,
        height: params.height || 450,
        ok: function () {
            if (params.isSubmit) {
                var iframe = this.iframe.contentWindow;
                if (!iframe.document.body) {
                    alert('正在加载中...')
                    return false;
                }
                iframe.$("#main-form").trigger('submit');
            }
            return false;
        },
        okVal: '保存',
        close: function () {
            var top = art.dialog.opener;
            if (params.redirectUrl !== undefined) {
                top.location.href = params.redirectUrl;
            }
            else if (params.isReload) {
                top.location.reload();
            }
        },
        cancel: true,
        cancelVal: '放弃'
    });
    return false;
}

//弹窗添加合同
function openContractDialog(params) {
    art.dialog.open(params.url, {
        id: 'main-dialog',
        title: params.title,
        lock: true,
        width: params.width || 650,
        height: params.height || 450,
        ok: function () {
            if (params.isSubmit) {
                var totalSum = 0;
                var iframe = this.iframe.contentWindow;
                var contract_money = iframe.$("#contract-money").val();//合同金额必填
                var hardware_amount = iframe.$("#contract-hardware_amount").val();//硬件金额
                var purchase_amount = iframe.$("#contract-purchase_amount").val();//外采购金额
                var objSum = iframe.$("td input.sum");

                if (!iframe.document.body) {
                    alert('正在加载中...')
                    return false;
                }
                if (contract_money <= 0 || contract_money == null) {
                    art.dialog({
                        title: '失败提示',
                        time: 1.5,
                        icon: 'error',
                        content: "请填写有效的合同金额！"
                    });
                    return false;
                }
                if (hardware_amount < 0) {
                    art.dialog({
                        title: '失败提示',
                        time: 1.5,
                        icon: 'error',
                        content: "请填写有效的硬件金额！"
                    });
                    return false;
                }
                if (purchase_amount < 0) {
                    art.dialog({
                        title: '失败提示',
                        time: 1.5,
                        icon: 'error',
                        content: "请填写有效的外采购金额！"
                    });
                    return false;
                }
                objSum.each(function () {
                    totalSum += parseFloat($(this).val());
                });
                if (totalSum == 0 || totalSum != contract_money) {
                    art.dialog({
                        title: '失败提示',
                        time: 1.5,
                        icon: 'error',
                        content: "付款计划总额与合同金额不符！"
                    });
                    return false;
                }
                iframe.$("#main-form").trigger('submit');
            }
            return false;
        },
        okVal: '保存',
        close: function () {
            var top = art.dialog.opener;
            if (params.isReload) {
                top.location.reload();
            }
        },
        cancel: true,
        cancelVal: '放弃'
    });
    return false;
}




//弹窗
function openOnlySeeDialog(params) {
    art.dialog.open(params.url, {
        id: 'main-dialog',
        title: params.title,
        lock: true,
        width: params.width,
        height: params.height,
        close: function () {
            var top = art.dialog.opener;
            if (params.isReload) {
                top.location.reload();
            }
        },
        cancel: true,
        cancelVal: '关闭'
    });
    return false;
}

function ajaxGetMessages() {
    var url = GV.announceAjaxUrl;
    $.getJSON(url, function (json) {
        if (json.taskAmount > 0) {
            $("#task_amount").closest("li").attr("class", "colortips");
        }
        if (json.receivableAmount > 0) {
            $("#receivable_amount").closest("li").attr("class", "colortips");
        }
        if (json.otherAmount > 0) {
            $("#other_amount").closest("li").attr("class", "colortips");
        }
        if (json.messageAmount > 0) {
            $("#message_amount").closest("li").attr("class", "colortips");
        }
        $("#task_amount").html('(' + json.taskAmount + ')');
        $("#receivable_amount").html('(' + json.receivableAmount + ')');
        $("#other_amount").html('(' + json.otherAmount + ')');
        $("#message_amount").html('(' + json.messageAmount + ')');
    });
    setTimeout('ajaxGetMessages()', 120000);
}


//打开新窗口
function openWin(url, name, w, h) {
    w = w || screen.width * 0.5;
    h = h || screen.height * 0.7;
    window.open(url, name, "top=100,left=400,width=" + w + ",height=" + h + ",toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no");
}

/**
 * 修改状态
 */

//启用禁用
$(".set-status").live('click', function () {
    var obj = $(this)
    var url = obj.data('url');
    $.get(url, {}, function (data) {
        if (data.status == 1) {
            obj.parent().html(data.url);
            art.dialog({
                title: '成功提示',
                time: 3,
                icon: 'succeed',
                content: data.info,
            });
        } else {
            art.dialog({
                title: '失败提示',
                time: 3,
                icon: 'error',
                content: data.info
            });
        }
    });
});
if(leftMenu==0){
    $('.manage-right').css({"margin-left": "0px","border-left": "0px solid #dde6ef"});
}

