function  viewBusiness(id) {
    window.open('/acceptance/basicView?viewInitiation=1&historyId='+id)
    return false;
}

//处理
function dealBusiness(taskId,businessKey) {
    acceptanceAllQuery.listPage = $('#contractList').getGridParam('page');
    $(".yk_rightcontent").load('/businessProcessingTask/dispatchMyTask?taskId='+taskId+"&businessKey="+businessKey)
    //$(".yk_rightcontent").load("/salesContract/add?taskId="+taskId + '&id='+ businessKey)
}

//下载
function dowmLoadBusiness(id) {
    $("#export").attr("href",'acceptance/downloadAllfile?id='+id);
    $("#export").get(0).click();
}

//下载出证证明
// function dowmLoadProve(id) {
//     $("#export").attr("href",'/bill/download/report?id='+id);
//     $("#export").get(0).click();
// }

//删除按钮
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) {
				acceptanceAllQuery.listPage = $('#contractList').getGridParam('page');
				$.ajax({
					url:'/acceptance/deleteAcceptance',
					type:'get',
					data:{
                        'id':id
                    },
					success:function(data){
						if( data.success ){
							if( data.data ){
								new $.zui.Messager('删除成功', {
								    type: 'success'
								}).show();
								//刷新表格(当前页)
								//刷新表格
								$('#contractList').setGridParam({
									mtype:'GET',
									url : '/businessProcessingTask/doList?simpleClassName=AcceptanceSheet',
									datatype : 'json',
									postData: {
                                        'sourceBusinessId':acceptanceQuery.detailId2,
                                        'startDate':acceptanceAllQuery.startDate,
                                        'endDate':acceptanceAllQuery.endDate,
                                        'statusId':acceptanceAllQuery.status,
                                        'fastSearch':acceptanceAllQuery.searchName
									},
									page: enterpriseQuery.listPage
								});
								$('#contractList').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 timeLime(id){
    $('.progressTimelinebox').empty();
    $.ajax({
        url:'/businessProcessingTask/doTimelineList',
        type:'get',
        data:{
            'businessKey':id,
            'businessType':'AcceptanceSheet'
        },
        success:function(data){
            if( data.success ){
                var text = '';
                for( var i=0;i<data.data.length;i++ ){
                    text += '<div class="progressTimeline"><div><span></span>'+data.data[i].formBusinessName+'</div>';
                    for( var j=0;j<data.data[i].logs.length;j++ ){
                        var assigneeName = data.data[i].logs[j].assigneeName ? data.data[i].logs[j].assigneeName : ''
                        text += '<p><span>'+data.data[i].logs[j].assigneeTypeName+'：'+ assigneeName +'</span>';
                        if( data.data[i].logs[j].status ){
                            text += '<span>状态：'+data.data[i].logs[j].status+'</span>';
                        };
                        if( data.data[i].logs[j].startTime ){
                            text += '<span style="width: 2.3rem">开始时间：'+data.data[i].logs[j].startTime+'</span>';
                        };
                        if( data.data[i].logs[j].endTime ){
                            text += '<span style="width: 2.3rem">完成时间：'+data.data[i].logs[j].endTime+'</span>';
                        };
                        text += '</p>'
                    };
                    j = null;
                    text += '</div>';
                };
                i = null;
                $('.progressTimelinebox').append(text);
            }else{
                new $.zui.Messager('获取时间轴信息失败:'+data.msg, {
                    type: 'danger'
                }).show();
            }
        },
        error:function(error){
            new $.zui.Messager('获取时间轴信息发生错误:'+error, {
                type: 'danger'
            }).show();
        }
    })
    $("#myModal-progressTimeline").modal('show')
};



$(function () {

    // 重新加载列表
    function reloadList(){
        $('#contractList').setGridParam({
            mtype:'GET',
            url : '/businessProcessingTask/doList?simpleClassName=AcceptanceSheet',
            datatype : 'json',
            postData: {
                'sourceBusinessId':acceptanceQuery.detailId2,
                'startDate':acceptanceAllQuery.startDate,
                'endDate':acceptanceAllQuery.endDate,
                'statusId':acceptanceAllQuery.status,
                'fastSearch':acceptanceAllQuery.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(acceptanceAllQuery.startDate);
    $("#businessEndDate").val(acceptanceAllQuery.endDate);
    $("#businessStatus").val(acceptanceAllQuery.status);
    $("#searchName").val(acceptanceAllQuery.searchName.split("%")[1]);


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

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

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

    // 操作列
    function operateBusiness(cellValue,options,rowObject){
        var btnstr = '';
        //if( $('#timeLine').length>0 ){
        btnstr += '<p><span>时间进度轴</span><i class="operateIconfont operateIcon-shijianzhou" onclick="timeLime(' + rowObject.id + ')"></i></p>';
        //};
        //if( $('#viewContract').length>0 ){
        btnstr += '<p><span>查看</span><i class="operateIconfont operateIcon-chakan" onclick="viewBusiness('+rowObject.id+')"></i></p>';
        //};
        if( $('#downloadAllAcceptanceFile').length>0 ) {
            if( rowObject.downloadFileFlag ){
                btnstr += '<p><span>下载</span><i class="operateIconfont operateIcon-xiazai" onclick="dowmLoadBusiness( '+rowObject.id+')"></i></p>';
            }; 
        }
        // if( rowObject.status.code !== 'purch_contract_create' && rowObject.status.code !=='purch_contract_input' && rowObject.status.code !=='purch_contract_first_approval' && rowObject.status.code !=='purch_contract_overrule'){
        //     btnstr += '<p><span>下载出证证明</span><i class="operateIconfont operateIcon-xiazai" onclick="dowmLoadProve( '+rowObject.id+')"></i></p>';
        // };
        if($('#deleteAcceptance').length>0){
            btnstr += '<p><span>删除</span><i class="operateIconfont operateIcon-shanchu" onclick="deleteBusiness(\''+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: 'moduleNumber', label: '验收单号', sortable: false,align: 'center', title: false},
            {name: 'businessNumber', label: '业务编号', sortable: false,align: 'center', title: false},
            {name: 'customerName', label: '核心企业名称', sortable: false,align: 'center', title: false},
            {name: 'businessName', label: '业务名称', sortable: false,align: 'center', title: false},
            {name: 'supplierName', label: '供应商名称', sortable: false, align: 'center', title: false},
            {name: 'expectedSettlementTime', label: '预估结算日期', sortable: false, align: 'center', title: false},
            {name: 'taskName', label: '当前阶段', sortable: false, align: 'center',  title: false},
            {name: 'assigneeName', label: '执行人', sortable: false, align: 'center',  title: false},
            {name: 'status.name', label: '状态', sortable: false, align: 'center',  title: false},
            // {name: 'gmtModified', label: '最近更新时间', sortable: false, align: 'center', title: false,width: '220px'},
            {
                name: 'remark',
                label: '操作',
                sortable: false,
                align: 'left',
                width: 140,
                title: false,
                formatter: operateBusiness
            }
        ],
        height: 'auto',
        mtype: 'GET',
        url: '/businessProcessingTask/doList?simpleClassName=AcceptanceSheet',
        pager: "#contractListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'sourceBusinessId':acceptanceQuery.detailId2,
            'startDate':acceptanceAllQuery.startDate,
            'endDate':acceptanceAllQuery.endDate,
            'statusId':acceptanceAllQuery.status,
            'fastSearch':acceptanceAllQuery.searchName
        },
        page: acceptanceAllQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

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

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

        if ($(this).text() == '待我处理') {
            $(".yk_rightcontent").load('/acceptance/pendingList?businessBasicId='+acceptanceQuery.detailId2);
        } else if ($(this).text() == '我已处理') {
            $(".yk_rightcontent").load('/acceptance/findMyCompletedList?businessBasicId='+acceptanceQuery.detailId2);
        }else if ($(this).text() == '全部') {
            $(".yk_rightcontent").load('/acceptance/allList?businessBasicId='+acceptanceQuery.detailId2);
        }
    });

    $(".newBackBtn").click(function () {
        $(".yk_rightcontent").load('/acceptance/businessList')
    })


})
