//查看业务
function viewBusiness(id,number) {
    settleQuery.listPage = $('#contractList').getGridParam('page');
    settleQuery.detailId = id;
    settleQuery.detailId2 = number;

    settlePendingQuery.listPage = 1;
    settlePendingQuery.startDate = '';
    settlePendingQuery.endDate = '';
    settlePendingQuery.status = '';
    settlePendingQuery.searchName = id;

    settleProcessedQuery.listPage = 1;
    settleProcessedQuery.startDate = '';
    settleProcessedQuery.endDate = '';
    settleProcessedQuery.status = '';
    settleProcessedQuery.searchName = '';

    settleAllQuery.listPage = 1;
    settleAllQuery.startDate = '';
    settleAllQuery.endDate = '';
    settleAllQuery.status = '';
    settleAllQuery.searchName = '';
    $.get('/bill/pendingList?businessBasicId='+number,function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}


$(function () {

    // 重新加载列表
    function reloadList(){
        $('#contractList').setGridParam({
            mtype:'GET',
            url : '/bill/getBusinessList',
            datatype : 'json',
            postData: {
                'beginTime':settleQuery.startDate,
                'endTime':settleQuery.endDate,
                'status':settleQuery.status,
                'condition':settleQuery.searchName
            },
            page : 1
        });
        $('#contractList').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(settleQuery.startDate);
    $("#businessEndDate").val(settleQuery.endDate);
    $("#businessStatus").val(settleQuery.status);
    $("#searchName").val(settleQuery.searchName);


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

    // '结束日期'的变化
    $("#businessEndDate").change(function(){
        settleQuery.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':settleQuery.startDate = '';break;
            case 'businessEndDate':settleQuery.endDate = '';break;
        };
        reloadList();
    });

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

    function amountShow(cellValue,options,rowObject) {
        if(cellValue !== null) {
            return commaDelimiter(cellValue)
        }else {
            return ''
        }

    }

    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';

        //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>';
    };

    sxUtil.jqGrid('#contractList', {
        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: 'enterpriseName', label: '核心企业名称', sortable: false, align: 'center', title: false},
            {name: 'businessName', label: '业务名称', sortable: false, align: 'center', title: false},
            {name: 'dicName', label: '状态', sortable: false, align: 'center',  title: false},
            {name: 'pendingSettlementAmount', label: '待结算金额', sortable: false, align: 'center',  title: false,formatter:amountShow},
            {name: 'gmtCreate', 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: '/bill/getBusinessList',
        pager: "#contractListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'beginTime':settleQuery.startDate,
            'endTime':settleQuery.endDate,
            'status':settleQuery.status,
            'condition':settleQuery.searchName
        },
        page: settleQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

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

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

})

