//销售发票
function viewSaleInvoice(number,id) {
    invoiceQuery.listPage = $('#contractList').getGridParam('page');
    invoiceQuery.detailId = number;
    invoiceQuery.detailId2 = id;
    invoiceQuery.source = 'sale';

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

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

    saleInvoiceAllQuery.listPage = 1;
    saleInvoiceAllQuery.startDate = '';
    saleInvoiceAllQuery.endDate = '';
    saleInvoiceAllQuery.status = '';
    saleInvoiceAllQuery.searchName = '';
    $.get('/invoiceManage/pendingSaleList',function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

//采购发票
function viewPurchInvoice(number,id) {
    invoiceQuery.listPage = $('#contractList').getGridParam('page');
    invoiceQuery.detailId = number;
    invoiceQuery.detailId2 = id;
    invoiceQuery.source = 'purch';

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

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

    purchInvoiceAllQuery.listPage = 1;
    purchInvoiceAllQuery.startDate = '';
    purchInvoiceAllQuery.endDate = '';
    purchInvoiceAllQuery.status = '';
    purchInvoiceAllQuery.searchName = '';
    $.get('/invoiceManage/pendingPurchList',function(data){
        $('.yk_rightcontent').empty().html(data)
    });
}

$(function () {

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


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

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

    // 状态变化
    $("#businessStatus").change(function(){
        invoiceQuery.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( $('#salesInvoice').length>0 ) {

            btnstr += '<p><span>销售发票</span><i style="font-size: 14px;" class="icon-file-archive" onclick="viewSaleInvoice(\'' + rowObject.businessNumber + '\',' + rowObject.id + ')"></i></p>';
        }
        if( $('#purchaseInvoice').length>0 ) {
            btnstr += '<p><span>采购发票</span><i style="font-size: 16px;" class="icon-file-alt" onclick="viewPurchInvoice(\''+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: 'settlementAmount', label: '结算金额', sortable: false, align: 'center',  title: false,formatter:amountShow},
            {name: 'invoiceAmount', 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: '/invoiceManage/getBusinessList',
        pager: "#contractListPage",
        multiselect: false,
        rownumbers: false,
        postData: {
            'beginTime':invoiceQuery.startDate,
            'endTime':invoiceQuery.endDate,
            'status':invoiceQuery.status,
            'condition':invoiceQuery.searchName
        },
        page: invoiceQuery.listPage,
        loadError: function () {
            new $.zui.Messager('列表加载失败', {
                type: 'warning'
            }).show();
        }
    }, true);

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

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

})

