//查看入账
function viewEntry(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;

    $('#viewEntryDialog').load('/cashFlow/entryView?cashFlowManageBaseId='+id,function () {
        $("#myModal-viewEntryInfo").modal("show");
    })
}
//修改入账
function updateBusiness(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;
    $.get('/cashFlow/update?cashFlowManageBaseId='+id,function(data){
        if(data.code == '500') {
            new $.zui.Messager(data.msg, {
                type: 'warning'
            }).show();
        }else {
            $('.yk_rightcontent').empty().html(data)
        }
    });

}

//查看出账
function viewOut(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;

    $('#viewOutDialog').load('/cashFlow/outView?cashFlowManageBaseId='+id,function () {
        $("#myModal-viewOutInfo").modal("show");
    })

}
function viewOutOther(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;

    $('#viewOutDialog').load('/cashFlow/outViewOther?cashFlowManageBaseId='+id,function () {
        $("#myModal-viewOutInfo").modal("show");
    })

}

//处理
function dealBusiness(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;

    $.get('/cashFlow/deal?cashFlowManageBaseId='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}
function dealBusinessOther(id) {
    cashFlowQuery.listPage = $('#businessList').getGridParam('page');
    cashFlowQuery.detailId = id;

    $.get('/cashFlow/dealOther?cashFlowManageBaseId='+id,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

//删除按钮
function deleteBusiness(id){
	bootbox.confirm({
		title : '删除',
		message : '<div style="text-align:left;font-size:0.12rem">确认删除吗？</div>',
		size : 'small',
		buttons : {
			cancel : {
				label : '取消',
				className:'btn-white btn-info'
			},
			confirm : {
				label : '确定',
				className:'btn-white btn-info colorbtn'
			}
		},
		callback:function(result){
			if (result) {
				cashFlowQuery.listPage = $('#businessList').getGridParam('page');
                cashFlowQuery.detailId = id;
				$.ajax({
					url:'/cashFlow/doDelete?id='+id,
					type:'get',
					data:{},
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								//刷新表格
								$('#businessList').setGridParam({
									mtype:'GET',
									url : '/cashFlow/select',
									datatype : 'json',
									postData: {
                                        'startDate':cashFlowQuery.startDate,
                                        'endDate':cashFlowQuery.endDate,
                                        'cashStatus':cashFlowQuery.status,
                                        'entryOutType':cashFlowQuery.type,
                                        'fastSearch':cashFlowQuery.searchName
									},
									page: cashFlowQuery.listPage
								});
								$('#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 () {

    // 重新加载列表
    function reloadList(){
        $('#businessList').setGridParam({
            mtype:'GET',
            url : '/cashFlow/select',
            datatype : 'json',
            postData: {
                'startDate':cashFlowQuery.startDate,
                'endDate':cashFlowQuery.endDate,
                'cashStatus':cashFlowQuery.status,
                'entryOutType':cashFlowQuery.type,
                'fastSearch':cashFlowQuery.searchName
            },
            page : 1
        });
        $('#businessList').trigger('reloadGrid');
    };

    //初始化日期
    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(cashFlowQuery.startDate);
    $("#businessEndDate").val(cashFlowQuery.endDate);
    $("#businessStatus").val(cashFlowQuery.status);
    $("#businessType").val(cashFlowQuery.type);
    $("#searchName").val(cashFlowQuery.searchName);


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

    // '结束日期'的变化
    $("#businessEndDate").change(function(){
        cashFlowQuery.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',$("#planEndDate").val());
            setDate('#businessEndDate','1970-01-01','2116-12-31');
        };
        switch( $(this).prev().attr('id') ){
            case 'businessStartDate':cashFlowQuery.startDate = '';break;
            case 'businessEndDate':cashFlowQuery.endDate = '';break;
        };
        reloadList();
    });

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

    // 类型变化
    $("#businessType").change(function(){
        cashFlowQuery.type = $(this).val();
        reloadList();
    });

    // 显示列表'银行名称'
    function showBillingDate(cellValue,options,rowObject){
        var bankStr = '';
        if( rowObject.cashFlowManageBankVo && rowObject.cashFlowManageBankVo.length>0 ){
            for( var i=0;i<rowObject.cashFlowManageBankVo.length;i++ ){
                bankStr += '<span class="tableRowMultipleSpan">'+(rowObject.cashFlowManageBankVo[i].gmtCreate?rowObject.cashFlowManageBankVo[i].gmtCreate:'')+'</span>'
            };
            i = null;
        };
        return bankStr;
    };

    // 显示列表'银行名称'
    function showBillingBank(cellValue,options,rowObject){
        var bankStr = '';
        if( rowObject.cashFlowManageBankVo && rowObject.cashFlowManageBankVo.length>0 ){
            for( var i=0;i<rowObject.cashFlowManageBankVo.length;i++ ){
                bankStr += '<span class="tableRowMultipleSpan">'+(rowObject.cashFlowManageBankVo[i].bankName?rowObject.cashFlowManageBankVo[i].bankName:'')+'</span>'
            };
            i = null;
        };
        return bankStr;
    };

    // 显示列表'卡号'
    function showBillingBankNo(cellValue,options,rowObject){
        var bankNoStr = '';
        if( rowObject.cashFlowManageBankVo && rowObject.cashFlowManageBankVo.length>0 ){
            for( var i=0;i<rowObject.cashFlowManageBankVo.length;i++ ){
                bankNoStr += '<span class="tableRowMultipleSpan">'+(rowObject.cashFlowManageBankVo[i].bankAccount?rowObject.cashFlowManageBankVo[i].bankAccount:'')+'</span>'
            };
            i = null;
        };
        return bankNoStr;
    };

    // 显示列表'出入账金额'
    function showBillingMoney(cellValue,options,rowObject){
        // var moneyStr = '';
        // if( rowObject.cashFlowManageBankVo && rowObject.cashFlowManageBankVo.length>0 ){
        //     for( var i=0;i<rowObject.cashFlowManageBankVo.length;i++ ){
        //         moneyStr += '<span class="tableRowMultipleSpan" style="text-align:left;">'+commaDelimiter(rowObject.cashFlowManageBankVo[i].amount)+'</span>'
        //     };
        //     i = null;
        // };
        // return moneyStr;
        if(cellValue == 0) {
            return  rowObject.currencyDict.code + ' ' + '0.00'
        }else {
            return handleListAmountFn(cellValue, rowObject.currencyDict ? rowObject.currencyDict.code : '')
        }

    };

    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';
        if(rowObject.entryOutType== '1') { //入账
            if(rowObject.cashStatus == '1') {
                btnstr += '<p><span>修改</span><i class="operateIconfont operateIcon-bianji" onclick="updateBusiness('+rowObject.id+')"></i></p>';
                if($('#deleteRecashflow').length>0){
                    btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteBusiness(\''+rowObject.id+'\')"></i></p>';
                }
            }
            if($('#viewRecashflow').length) {
                btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewEntry('+rowObject.id+')"></i></p>';
            }
        }else {
            if(rowObject.cashTypeDict.name == '保理本息') {
                if(rowObject.cashStatus !== '4') {
                    btnstr += '<p><span>处理</span><i class="operateIconfont operateIcon-chuli" onclick="dealBusiness('+rowObject.id+')"></i></p>';
                }
                // if(rowObject.cashStatu == '4') {
                    if($('#viewRecashflow').length) {
                        btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewOut('+rowObject.id+')"></i></p>';
                    }
                // }
            }else {
                if(rowObject.cashStatus !== '4') {
                    btnstr += '<p><span>处理</span><i class="operateIconfont operateIcon-chuli" onclick="dealBusinessOther('+rowObject.id+')"></i></p>';
                }
                // if(rowObject.cashStatu == '4') {
                    if($('#viewRecashflow').length) {
                        btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewOutOther('+rowObject.id+')"></i></p>';
                    }
                // }
            }
        }
        //if( $('#viewContract').length>0 ){
        // btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewBusiness(\''+rowObject.businessNumber+'\','+rowObject.id+')"></i></p>';
        //};
        return '<div class="operateIcon noHover">'+btnstr+'</div>';
    };
    function showType(cellValue,options,rowObject) {
        if(cellValue == '1') {
            return '入账'
        }else if(cellValue == '2') {
            return '出账'
        }
    }

    function showStatus(cellValue,options,rowObject) {
        if(cellValue == '1') {
            return '已创建'
        }else if(cellValue == '2') {
            return '已分配'
        }
        else if(cellValue == '3') {
            return '待处理'
        }
        else if(cellValue == '4') {
            return '已完成'
        }
    }

    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: 'gmtCreate', label: '日期', sortable: false,align: 'center', title: false,classes:'tableRowMultiple',formatter: showBillingDate},
            {name: 'bankName', label: '银行名称', sortable: false, align: 'center', title: false,classes:'tableRowMultiple',formatter: showBillingBank},
            {name: 'bankAccount', label: '卡号', sortable: false, align: 'center', title: false,classes:'tableRowMultiple',formatter: showBillingBankNo},
            {name: 'customerName', label: '核心企业名称', sortable: false, align: 'center',  title: false},
            {name: 'businessName', label: '业务名称', sortable: false, align: 'center',  title: false},
            {name: 'entryOutType', label: '出入账类型', sortable: false, align: 'center',title: false,formatter:showType},
            {name: 'cashTypeDict.name', label: '类型', sortable: false,align: 'center', title: false},
            {name: 'amount', label: '金额', sortable: false, align: 'center', title: false,formatter: showBillingMoney},
            {name: 'cashStatus', label: '状态', sortable: false, align: 'center', title: false,formatter:showStatus},
            {name: 'gmtModified', label: '最近更新时间', sortable: false, align: 'center', title: false},
            {
                name: 'remark',
                label: '操作',
                sortable: false,
                align: 'left',
                width: 150,
                title: false,
                formatter: operateBusiness
            }
        ],
        height: 'auto',
        mtype: 'GET',
        url: '/cashFlow/select',
        pager: "#businessListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'startDate':cashFlowQuery.startDate,
            'endDate':cashFlowQuery.endDate,
            'cashStatus':cashFlowQuery.status,
            'entryOutType':cashFlowQuery.type,
            'fastSearch':cashFlowQuery.searchName
        },
        page: cashFlowQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

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

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

    //添加
    $("#addBusiness").click(function () {
        $.get('/cashFlow/add',function(data){
            $('.yk_rightcontent').empty().html(data)
        });
    })

    //切换表格
    $(".projectListTab li").click(function () {

        if ($(this).text() == '现金流管理') {
            $(".yk_rightcontent").load('/cashFlow/list');
        } else if ($(this).text() == '银行信息设置') {
            $(".yk_rightcontent").load('/bankCardInfoSet/list');
        }
    });

})
