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

    acceptancePendingQuery.listPage = 1;
    acceptancePendingQuery.startDate = '';
    acceptancePendingQuery.endDate = '';
    acceptancePendingQuery.status = '';
    acceptancePendingQuery.searchName = number;

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

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


$(function () {

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


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

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

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

    // 操作列
    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: '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: '/acceptance/getBusinessList',
        pager: "#contractListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'beginTime':acceptanceQuery.startDate,
            'endTime':acceptanceQuery.endDate,
            'status':acceptanceQuery.status,
            'condition':acceptanceQuery.searchName
        },
        page: acceptanceQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

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

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

})

