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

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

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

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


$(function () {

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


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

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

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

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

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

})
