// 业务管理--新建业务
function addBusiness(){
    businessQuery.listPage = $('#businessList').getGridParam('page');
    $.get('/business/management/addPage',function(data){
        $('.yk_rightcontent').empty().html(data)
    });
};

// 业务管理--修改业务
function updateBusiness(id) {
    businessQuery.listPage = $('#businessList').getGridParam('page');
    $.get('/business/management/turnToUpdate?businessBasicInfoId='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

//查看业务
function viewBusiness(id) {
    businessQuery.listPage = $('#businessList').getGridParam('page');
    businessQuery.detailId = id;
    $.get('/business/management/findDetail?businessBasicInfoId='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}


// 业务管理--关闭
function closeBusiness(id,name){
    bootbox.confirm({
        title : '确认关闭业务',
        message : '<div style="text-align:left;font-size:0.12rem">是否关闭'+name+'？</div>',
        size : 'small',
        buttons : {
            cancel : {
                label : '取消',
                className:'btn-white btn-info'
            },
            confirm : {
                label : '确定',
                className:'btn-white btn-info colorbtn'
            }
        },
        callback:function(result){
            if (result) {
                businessQuery.listPage = $('#businessList').getGridParam('page');
                $.ajax({
                    url:'/business/management/closeBusiness',
                    type:'get',
                    data:{
                        'businessBasicInfoId':id
                    },
                    success:function(data){
                        if( data.success ){
                            if( data.data ){
                                new $.zui.Messager('关闭成功', {
                                    type: 'success'
                                }).show();
                                $('#businessList').trigger('reloadGrid');
                            }else{
                                new $.zui.Messager('关闭失败', {
                                    type: 'danger'
                                }).show();
                            }
                        }else{
                            new $.zui.Messager('关闭请求失败:'+data.msg, {
                                type: 'danger'
                            }).show();
                        }
                    },
                    error:function(status){
                        new $.zui.Messager('关闭时发生错误:'+status, {
                            type: 'danger'
                        }).show();
                    }
                })
            };
        }
    })
};

// 重新加载列表
function reloadList(){
    $('#businessList').setGridParam({
        mtype:'GET',
        url : '/business/management/findByCondition',
        datatype : 'json',
        postData: {
            'beginTime':businessQuery.startDate,
            'endTime':businessQuery.endDate,
            'status':businessQuery.status,
            'condition':businessQuery.searchName
        },
        page : 1
    });
    $('#businessList').trigger('reloadGrid');
};

 // 业务管理--移交
 function changeCompany(id) {
    chooseDepartOrPerson({
        dialogId:'.myModal-person',//弹框的选择器
        chooseSure:'.personChooseSure',//弹框确认按钮的选择器
        saveResultBox:'.yijiao',//点确定后存放选择结果的容器的选择器
        ajaxurl:'/org/findOrgAndUserTreeNodeAndAdmin',//获取包括超管在内的部门和人员
        atLeastOne: false, //是否需要至少选择一个部门或成员, 默认为false
        singleCheck: true,
        chooseDepart: false,//是否选择部门
        choosePerson: true, //是否选择人员
        closeAllSelect: true,  // 是否全选
        isConfirmStatus: true,  // 确实的回调函数
        confirmFunc: function handlerConfirmChange(confirmObj) {
            $.ajax({
                url:'/business/management/transfer',
                type:'post',
                data:{
                    'businessBasicInfoId':id,
                    'id': confirmObj.id
                },
                success:function(data){
                    if( data.success ){
                        new $.zui.Messager(data.data, {
                            type: 'success'
                        }).show();
                        reloadList();
                    }else{
                        new $.zui.Messager('移交失败:'+data.msg, {
                            type: 'danger'
                        }).show();
                    }
                },
                error:function(status){
                    new $.zui.Messager('关闭时发生错误:'+status, {
                        type: 'danger'
                    }).show();
                }
            })

        }
    })
}



$(function () {

    reloadList()
    //初始化日期
    function setDate(select,startDate,endDate){
        $(select).datetimepicker(
            {
                weekStart: 1,
                autoclose: 1,
                todayHighlight: 0,
                startView: 2,
                minView: 2,
                forceParse: 0,
                showMeridian: 1,
                startDate:startDate,
                endDate:endDate,
                format: "yyyy-mm-dd"
            }
        );
        $(select).datetimepicker('setStartDate',startDate);
        $(select).datetimepicker('setEndDate',endDate);
    };
    setDate('#businessStartDate,#businessEndDate','1970-01-01','2116-12-31');

    $("#businessStartDate").val(businessQuery.startDate);
    $("#businessEndDate").val(businessQuery.endDate);
    $("#businessStatus").val(businessQuery.status);
    $("#searchName").val(businessQuery.searchName);


    // '开始日期'的变化
    $("#businessStartDate").change(function(){
        businessQuery.startDate = $(this).val();
        if( $(this).val()=='' ){return}
        setDate('#businessEndDate',$(this).val(),'2116-12-31');
        reloadList();
    });
    $("#businessStartDate").change();

    // '结束日期'的变化
    $("#businessEndDate").change(function(){
        businessQuery.endDate = $(this).val();
        if( $(this).val()=='' ){return}
        setDate('#businessStartDate','1918-01-01',$(this).val());
        reloadList();
    });
    $("#businessEndDate").change();

    // 日期输入框可删除
    $('.deleteTime').each(function(i,ele){
        $(ele).prev().change(function(){
            if( $(this).val()!=='' ){
                $(ele).show();
            }else{
                $(ele).hide();
            }
        });
        $(ele).prev().change();
    });

    // 删除日期
    $('.deleteCreateTime').click(function(){
        $(this).prev().val('');
        $(this).hide();
        if( $("#businessEndDate").val()=='' && $("#businessStartDate").val()=='' ){
            setDate('#businessEndDate,#businessStartDate','1970-01-01','2116-12-31');
        };
        if( $("#businessEndDate").val()=='' && $("#businessStartDate").val()!='' ){
            setDate('#businessStartDate','1970-01-01','2116-12-31');
            setDate('#businessEndDate',$("#businessStartDate").val(),'2116-12-31');
        };
        if( $("#businessEndDate").val()!='' && $("#businessStartDate").val()=='' ){
            setDate('#businessStartDate','1970-01-01',$("#businessEndDate").val());
            setDate('#businessEndDate','1970-01-01','2116-12-31');
        };
        switch( $(this).prev().attr('id') ){
            case 'businessStartDate':businessQuery.startDate = '';break;
            case 'businessEndDate':businessQuery.endDate = '';break;
        };
        reloadList();
    });

    // 状态变化
    $("#businessStatus").change(function(){
        businessQuery.status = $(this).val();
        reloadList();
    });

    function status(cellValue,options,rowObject) {
        if(rowObject.status) {
            switch (rowObject.status) {
                case 1: return '已创建'
                break;
                case 2: return '贸易中'
                    break;
                case 3: return '已结清'
                    break;
                case 4: return '已结束'
                    break;
            }
        }else {
            return ''
        }
    }

    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';
        if( $('#updateBusiness').length>0 ){
            btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateBusiness('+rowObject.id+')"></i></p>';
        };
        if( $('#viewBusiness').length>0 ){
            btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewBusiness('+rowObject.id+')"></i></p>';
        };
        // if( $('#closeBusiness').length>0 ){
        //     if( rowObject.businessBasicDict.name == '已创建' || rowObject.businessBasicDict.name == '已结清' ){
        //         btnstr += '<p><span>关闭</span><i class="operateIconfont operateIcon-zuofei" onclick="closeBusiness('+rowObject.id+',\''+rowObject.businessName+'\')"></i></p>';
        //     };
        // };
        if( $('#transferBusiness').length>0 ){
            btnstr += '<p><span>移交</span><i class="iconfont icon-icon_yijiao" onclick="changeCompany('+rowObject.id+')"></i></p>';
        };
        return '<div class="operateIcon noHover">'+btnstr+'</div>';
    };

    sxUtil.jqGrid('#businessList', {
        colNames: ['ID', '序号','业务编号', '核心企业名称', '业务名称', '最近更新时间', '最长货物未结算天数', '操作'],
        colModel: [
            {name: 'id', label: 'ID', hidedlg: true, hidden: true, sortable: false, title: false},
            {name: 'orderNumber', label: '序号', sortable: false, width: 50, align: 'center', title: false},
            {name: 'businessNumber', label: '业务编号', sortable: false,align: 'center', title: false},
            {name: 'customer.enterpriseName', label: '核心企业名称', sortable: false, align: 'center', title: false},
            {name: 'businessName', label: '业务名称', sortable: false, align: 'center', title: false},
            {name: 'gmtModified', label: '最近更新时间', sortable: false, align: 'center', title: false},
            {name: 'unsettledDays', label: '最长货物未结算天数', sortable: false, align: 'center',title: false},
            {
                name: 'remark',
                label: '操作',
                sortable: false,
                align: 'left',
                width: 140,
                title: false,
                formatter: operateBusiness
            }
        ],
        height: 'auto',
        mtype: 'GET',
        url: '/business/management/findByCondition',
        pager: "#businessListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'beginTime':businessQuery.startDate,
            'endTime':businessQuery.endDate,
            'status':businessQuery.status,
            'condition':businessQuery.searchName
        },
        page: businessQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

    //搜索
    $("#searchBusiness").click(function () {
        businessQuery.startDate = $("#businessStartDate").val();
        businessQuery.endDate = $("#businessEndDate").val();
        businessQuery.status = $("#businessStatus").val();
        businessQuery.searchName = $("#searchName").val();
        businessQuery.listPage = 1;
        reloadList();
    })

    //刷新
    $("#refreshBusiness").click(function () {
        businessQuery.startDate = '';
        businessQuery.endDate = '';
        businessQuery.status = '';
        businessQuery.searchName = '';
        businessQuery.listPage = 1;
        $("#businessStartDate,#businessEndDate,#businessStatus,#searchName").val("");
        $(".deleteCreateTime").hide();
        reloadList();
    })

    //新建业务
    $("#addBusiness").click(function () {
        addBusiness();
    })

})
