$(document).ready(function() {
    checkSessionStat();
});
function checkSessionStat(){
    var status = sessionStorage.getItem("alert_status");
    var message = sessionStorage.getItem("alert_message");
    if(status == "success"){
        successAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    } else if(status == "failed"){
        errorAlert(message);
        sessionStorage.removeItem("alert_status");
        sessionStorage.removeItem("alert_message");
    }
}
$('#date-filter').daterangepicker({
    autoUpdateInput: false,
    locale: {
        cancelLabel: 'Clear'
    }
});
$('#date-filter').on('apply.daterangepicker', function(ev, picker) {
    $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
    var start_date = picker.startDate.format('DD-MM-YYYY');
    var end_date = picker.endDate.format('DD-MM-YYYY');
    $("#start-date").val(start_date);
    $("#end-date").val(end_date);
    var redir_url = $("#redir-url").val();
    var csdate = $("#csdate").val();
    var cedate = $("#cedate").val();
    var cregion = $("#cregion-select").val();//region
    var full_url = redir_url+"/"+csdate+"/"+cedate+"/"+cregion+"/"+start_date+"/"+end_date+"/";
    window.location.href = full_url;
    win_loader("open");
    //window.open( full_url, "_blank");
    detectFilter();
});
$('#date-filter').on('cancel.daterangepicker', function(ev, picker) {
    $(this).val('');
    $("#start-date").val("");
    $("#end-date").val("");
    detectFilter();
    var redir_url = $("#redir-url").val();
    window.location.href = redir_url;
    win_loader("open");
});
$('#cdate-filter').daterangepicker({
    autoUpdateInput: false,
    locale: {
        cancelLabel: 'Clear'
    }
});
$('#cdate-filter').on('apply.daterangepicker', function(ev, picker) {
    $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
    var start_date = picker.startDate.format('DD-MM-YYYY');
    var end_date = picker.endDate.format('DD-MM-YYYY');
    var redir_url = $("#redir-url").val();
    var cregion = $("#cregion-select").val();
    var cstatus = $("#cstatus-select").val();
    var full_url = redir_url+start_date+"/"+end_date+"/"+cregion+"/"+cstatus+"/";
    window.location.href = full_url;
    win_loader("open");
    //window.open( full_url, "_blank");
    detectFilter();
});
$('#cdate-filter').on('cancel.daterangepicker', function(ev, picker) {
    $(this).val('');
    detectFilter();
    var redir_url = $("#redir-url").val();
    window.location.href = redir_url;
    win_loader("open");
});
$('#bdate-filter').daterangepicker({
    autoUpdateInput: false,
    locale: {
        cancelLabel: 'Clear'
    }
});
$('#bdate-filter').on('apply.daterangepicker', function(ev, picker) {
    $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
    var start_date = picker.startDate.format('DD-MM-YYYY');
    var end_date = picker.endDate.format('DD-MM-YYYY');
    var redir_url = $("#redir-url").val();
    var full_url = redir_url+"?bstart="+start_date+"&bend="+end_date;
    window.location.href = full_url;
    win_loader("open");
    //window.open( full_url, "_blank");
    detectFilter();
});
$('#bdate-filter').on('cancel.daterangepicker', function(ev, picker) {
    $(this).val('');
    detectFilter();
    var redir_url = $("#redir-url").val();
    window.location.href = redir_url;
    win_loader("open");
});
$('#bstatus-select').on('change', function() {
    win_loader("open");
    var bsdate = $("#bsdate").val();
    var bedate = $("#bedate").val();
    var redir_url = $("#redir-url").val();
    var bstatus = $("#bstatus-select").val();
    var full_url = redir_url+"?bstart="+bsdate+"&bend="+bedate+"&bstatus="+bstatus;
    window.location.href = full_url;
});
$("#btn-clear").click(function(){
    win_loader("open");
    // clearFilter();
    // detectFilter();
    // $("#btn-clear").css("display","none");
});
$("#cbtn-clear").click(function(){
    win_loader("open");
    // clearFilter();
    // detectFilter();
    // $("#btn-clear").css("display","none");
});
function clearFilter(){
    $("#start-date").val("");
    $("#end-date").val("");
    $('#date-filter').val("");
}
function detectFilter(){
    var start_date = $("#start-date").val();
    var end_date = $("#end-date").val();

    $("#btn-clear").css("display","none");
    if(start_date != ""){
        $("#btn-clear").css("display","inline-block");
    }
    if(end_date != ""){
        $("#btn-clear").css("display","inline-block");
    }
}
$('#cregion-select').on('change', function() {
    win_loader("open");
    var csdate = $("#csdate").val();
    var cedate = $("#cedate").val();
    var redir_url = $("#redir-url").val();
    var cregion = $("#cregion-select").val();
    var cstatus = $("#cstatus-select").val();
    var full_url = redir_url+csdate+"/"+cedate+"/"+cregion+"/"+cstatus+"/";
    window.location.href = full_url;
});
$('#cstatus-select').on('change', function() {
    win_loader("open");
    var csdate = $("#csdate").val();
    var cedate = $("#cedate").val();
    var redir_url = $("#redir-url").val();
    var cregion = $("#cregion-select").val();
    var cstatus = $("#cstatus-select").val();
    var full_url = redir_url+csdate+"/"+cedate+"/"+cregion+"/"+cstatus+"/";
    window.location.href = full_url;
});
function confirmExportSummary(){
    var ship_type = $("#ship-type").val();
    var csdate = $("#csdate").val();
    var cedate = $("#cedate").val();
    var cregion = $("#cregion-select").val();
    var redir_url = $("#redir-url").val();
    var full_url = redir_url+"export-summary.php?type="+ship_type+"&csdate="+csdate+"&cedate="+cedate+"&cregion="+cregion;

    swal({
        title: "Export Report Shipment Summary?",
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#008d4c",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.open( full_url, "_blank");
        } else {
            //nothing
        }
    });
}
function confirmExportStatus(){
    var ship_type = $("#ship-type").val();
    var sdate = $("#start-date").val();
    var edate = $("#end-date").val();
    var redir_url = $("#redir-url").val();
    var full_url = redir_url+"export-status.php?type="+ship_type+"&sdate="+sdate+"&edate="+edate;

    swal({
        title: "Export Report Shipment Status?",
        text: "",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#008d4c",
        confirmButtonText: "Process ! ",
        cancelButtonText: "Cancel !",
        closeOnConfirm: true,
        closeOnCancel: true
    },
    function (isConfirm) {
        if (isConfirm) {
            window.open( full_url, "_blank");
        } else {
            //nothing
        }
    });
}