<script>
    $("#trip").select2();
    $("#party_name").select2();
    $("#vehicle").select2();
    $("#broker_vehicle").select2();
    $("#broker").select2();
    $("#pump_vehicle").select2();
    $("#pump").select2();
    $("#head").select2();

    function validate_date() {
        var error = 0;
        var starting_date = $("#starting_date").val();
        var ending_date = $("#ending_date").val();
        if (starting_date == '') {
            $("#starting_date_error").text('Starting Date is required');
            error = parseFloat(error) + 1;
        }
        if (ending_date == '') {
            $("#ending_date_error").text('Ending Date is required');
            error = parseFloat(error) + 1;
        }
        return error;
    }
    function head_wise_report() {
        var date_error = validate_date();
        var er = 0;
        var head_id = $("#head").val();
        if (head_id == "") {
            er = parseFloat(er) + 1;
            $("#head_error").text('Maintenance Head is required.');
        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('head_wise_report', [':id',':s_date', ':e_date']) }}";
            url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function head_wise_details_report() {
        var date_error = validate_date();
        var er = 0;
        var head_id = $("#head").val();
        if (head_id == "") {
            er = parseFloat(er) + 1;
            $("#head_error").text('Maintenance Head is required.');
        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('head_wise_details_report', [':id',':s_date', ':e_date']) }}";
            url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function all_maintenance_report() {
        var date_error = validate_date();
        var er = 0;
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('overall_maintenance_report', [':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function new_tyre_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        var eror = validate_date(starting_date, ending_date);
        if (vehicle == '') {
            $("#vehicle_error").text('Vehicle is Required.');
            eror = parseFloat(eror) + 1;
        }
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('new_tyre_report', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                //      window.location.reload();
            }, 200);
        }
    }
    function old_tyre_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        var eror = validate_date(starting_date, ending_date);
        if (vehicle == '') {
            $("#vehicle_error").text('Vehicle is Required.');
            eror = parseFloat(eror) + 1;
        }
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('old_tyre_report', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                //      window.location.reload();
            }, 200);
        }
    }

    function pump_pending_slip() {
        $(".error").text('');
        var date_error = validate_date();
        var er = 0;
        var vehicle = $("#pump_vehicle").val();
        var broker = $("#broker").val();
        if (vehicle == '') {
            $("#pump_vehicle_error").text("Vehicle is Required.");
            er = parseFloat(er) + 1
        }
        if (broker == '') {
            $("#broker_error").text("Broker is Required.");
            er = parseFloat(er) + 1
        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('all_vehicle_pump_pending_slip_report', [':vehicle',':broker',':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':broker', broker);
            url = url.replace(':vehicle', vehicle);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }
   function broker_pending_slip() {
        $(".error").text('');
        var date_error = validate_date();
        var er = 0;
        var vehicle = $("#broker_vehicle").val();
        var pump = $("#broker").val();
        if (vehicle == '') {
            $("#broker_vehicle_error").text("Vehicle is Required.");
            er = parseFloat(er) + 1
        }
        if (pump == '') {
            $("#broker_error").text("Broker is Required.");
            er = parseFloat(er) + 1
        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('all_vehicle_broker_pending_slip_report', [':vehicle',':pump',':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':pump', pump);
            url = url.replace(':vehicle', vehicle);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function vehicle_wise_pump_pending_slip() {
        var date_error = validate_date();
        var er = 0;
        var vehicle = $("#vehicle").val();
        $('.error').text('');
        if (vehicle == '') {
            $("#vehicle_error").text('Vehicle is Required');
            er = parseFloat(er) + 1;

        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('specific_vehicle_pump_pending_slip_report', [':id',':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':id', vehicle);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function opening_balance_report() {
        var date_error = validate_date();
        var er = 0;
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('opening_balance_report', [':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function trip_adjustment_report() {
        var date_error = validate_date();
        var er = 0;
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('trip_adjustment_report', [':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function broker_pending_amount_report() {
        var date_error = validate_date();
        var er = 0;
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('broker_pending_amount_report', [':s_date', ':e_date']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function city_wise_report() {
        var date_error = validate_date();
        var er = 0;
        var from_city = $("#from_city").val();
        var to_city = $("#to_city").val();
        $('.error').text('');
        if (from_city == '') {
            $("#from_city_error").text('From City is Required');
            er = parseFloat(er) + 1;

        }
        if (to_city == '') {
            $("#to_city_error").text('To City is Required');
            er = parseFloat(er) + 1;
        } else {

            if (from_city == to_city) {
                $("#to_city_error").text('From City and To City should not be same.');
                er = parseFloat(er) + 1;
            }
        }

        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('city_wise_trip_report', [':s_date', ':e_date', ':from_city', ':to_city']) }}";
            // url = url.replace(':id', head_id);
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            url = url.replace(':from_city', from_city);
            url = url.replace(':to_city', to_city);
            window.open(url, '_blank');
        }
    }

    function all_trip_report() {
        date_error = validate_date();
        var er = 0;
        if (date_error == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('all_trips_report', [':s_date', ':e_date']) }}";
            url = url.replace(':s_date', starting_date);
            url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function trip_report() {
        date_error = 0;
        var er = 0;
        var trip = $("#trip").val();
        if (trip == '') {

            $("#trip_error").text("Trip is Required");
            er = parseFloat(er) + 1;

        }
        if (date_error == 0 && er == 0) {
            var starting_date = $("#starting_date").val();
            var ending_date = $("#ending_date").val();
            $(".error").text('');
            var url = "{{ route('trip_report', ':id') }}";
            url = url.replace(':id', trip);
            // url = url.replace(':e_date', ending_date);
            window.open(url, '_blank');
        }
    }

    function party_ledger()
    {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var party_name = $("#party_name").val();
        var table = $('#party_name').find(':selected').data('table')

        var eror = validate_date(starting_date, ending_date)
        if (party_name == '') {
            $("#party_name_error").text('Party Name is Required');
            eror = eror + 1;
        }
        // alert(table);

        if (eror == 0) {
            $(".error").text('');
            if (table == 'pump') {
                var url = "{{ route('pump_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'other_account') {
                var url = "{{ route('otherAccountLedger', [':id', ':start', ':end']) }}";
            }
            if (table == 'bank') {
                var url = "{{ route('bank_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'cash') {
                var url = "{{ route('cash_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'broker') {
                var url = "{{ route('broker_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'vehicle') {
                var url = "{{ route('vehicle_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'customer') {
                var url = "{{ route('customer_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'driver') {
                var url = "{{ route('driver_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'workshop') {
                var url = "{{ route('workshop_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'supplier') {
                var url = "{{ route('supplier_ledger', [':id', ':start', ':end']) }}";
            }
            url = url.replace(':id', party_name);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            // console.log(url);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }
    }

     function party_ledger_date_wise()
    {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var party_name = $("#party_name").val();
        var table = $('#party_name').find(':selected').data('table')

        var eror = validate_date(starting_date, ending_date)
        if (party_name == '') {
            $("#party_name_error").text('Party Name is Required');
            eror = eror + 1;
        }
        // alert(table);

        if (eror == 0) {
            $(".error").text('');
            if (table == 'pump') {
                var url = "{{ route('pump_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'other_account') {
                var url = "{{ route('otherAccountLedger', [':id', ':start', ':end']) }}";
            }
            if (table == 'bank') {
                var url = "{{ route('bank_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'cash') {
                var url = "{{ route('cash_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'broker') {
                var url = "{{ route('broker_ledger_date_wise', [':id', ':start', ':end']) }}";
            }
            if (table == 'vehicle') {
                var url = "{{ route('vehicle_ledger_date_wise', [':id', ':start', ':end']) }}";
            }
            if (table == 'customer') {
                var url = "{{ route('customer_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'driver') {
                var url = "{{ route('driver_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'workshop') {
                var url = "{{ route('workshop_ledger', [':id', ':start', ':end']) }}";
            }
            if (table == 'supplier') {
                var url = "{{ route('supplier_ledger_date_wise', [':id', ':start', ':end']) }}";
            }
            url = url.replace(':id', party_name);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            // console.log(url);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }
    }

    function trail_balance() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var eror = validate_date(starting_date, ending_date)
        if (eror == 0) {
            $('.error').text('');
            var url = "{{ route('trail_balance_report', [ ':start', ':end']) }}";
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }
    }

    function annual_closing_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        var eror = validate_date(starting_date, ending_date);
        if (vehicle == '') {
            $("#vehicle_error").text('Vehicle is required.');
            eror = parseFloat(eror) + 1;
        }
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('annual_closing_report', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }

    }

    function vehicle_p_l_summary() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        if (vehicle == '') {
            vehicle = 'all';
        }
        var eror = validate_date(starting_date, ending_date);
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('vehicle_p_l_summary', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }
    }

    function vehicle_wise_trip_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        if (vehicle == '') {
            vehicle = 'all';
        }
        var eror = validate_date(starting_date, ending_date);
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('vehicle_wise_trip_report', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                // window.location.reload();
            }, 200);
        }
    }

    function vehicle_porfit_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        var eror = validate_date(starting_date, ending_date);
        if (vehicle == '') {
            $("#vehicle_error").text('Vehicle is Required.');
            eror = parseFloat(eror) + 1;
        }
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('vehicle_profit_ledger', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')
            setTimeout(() => {
                //      window.location.reload();
            }, 200);
        }
    }

    function trip_expense_report() {
        var ending_date = $("#ending_date").val();
        var starting_date = $("#starting_date").val();
        var vehicle = $("#vehicle").val();
        if (vehicle == '') {
            vehicle = 'all';
        }
        var eror = validate_date(starting_date, ending_date);
        if (eror == 0) {
            $(".error").text('');
            var url = "{{ route('vehicle_wise_trip_expense_report', [':id', ':start', ':end']) }}";
            url = url.replace(':id', vehicle);
            url = url.replace(':start', starting_date);
            url = url.replace(':end', ending_date);
            window.open(url, '_blank')

        }
    }
</script>
