<?php

require_once './admin_header.php';
?>


<style>
    th{
        border-top: 1px solid black !important;
        border-bottom: 1px solid black !important;
    }
</style>
<div class="app-main__outer">
    <div class="app-main__inner" style="padding-top: 30px;padding-bottom: 0px;padding-left: 0px;padding-right: 30px !important">
        <div class="tab-content">
            <div class="tab-pane tabs-animation fade show active" id="tab-content-0" role="tabpanel">

                <div class="tab-content">
                    <div class="tab-pane tabs-animation fade active show" id="tab-content-0" role="tabpanel">
                        <div class="main-card mb-3 card">
                            <div class="card-body"><h5 class="card-title">Generate Bill</h5>
                                <form method="post">
                                    <div class="form-row">
                                        <div class="col-md-4">
                                            <label for="vehicle_Owner_Name" class="">Vehicle Owner Name</label>
                                            <input name="Vehicle Owner Name" id="vehicle_owner_name" tabindex="1" placeholder="Vehicle Owner Name" type="text" class="form-control">
                                            <div id="msg_vehicle_owner_name"></div>
                                        </div>
                                        <div class="col-md-4">
                                            <label for="vehicle_no" class="">Vehicle No</label>
                                            <input name="vehicle_no" id="vehicle_no" placeholder="vehicle_no" tabindex="2" type="text" class="form-control">
                                            <div id="msg_vehicle_no"></div>
                                        </div>
                                        <div class="col-md-4">
                                            <label for="Address" class="">Address</label>
                                            <textarea name="address" id="address" placeholder="Address" tabindex="3" class="form-control"></textarea>
                                            <div id="msg_address"></div>
                                        </div>
                                    </div>
                                    <div class="form-row">
                                        <div class="col-md-4">
                                            <label for="state-code" class="">State-Code</label>
                                            <select name="select" id="select-state-code" tabindex="4" class="form-control">
                                                <option name="Select"> Select </option>
                                                <option name="GJ-24"> Gujarat (GJ-24) </option>
                                                <option name="MH-27"> Maharashtra (MH-27) </option>
                                            </select>
                                            <div id="msg_select_state_code"></div>
                                        </div>
                                        <div class="col-md-4">
                                            <label for="state-code" class="">Place of Supply</label>
                                            <select name="select" id="select_place_of_supply" tabindex="5" class="form-control">
                                                <option name="Select"> Select </option>
                                                <option name="GJ(24)"> Gujarat (GJ-24) </option>
                                                <option name="MH(27)"> Maharashtra (MH-27) </option>
                                            </select>
                                            <div id="msg_select_place_of_supply"></div>
                                        </div>

                                    </div>
                                    <hr>
                                    <div class="form-row form-group">
                                        <div class="col-md-4">
                                            <label for="Address" class="">HSN Code</label>
                                            <label for="Address" class="form-control" ><b id="hsn-code">2505</b></label>
                                        </div>
                                        <div class="col-md-4">
                                            <label for="product_name" class="">Product Name</label>
                                            <select name="select" id="select_product_name" tabindex="6" class="form-control">
                                                <option name="Select"> Select </option>
                                                <option name="odanary sand"> odanary sand </option>
                                            </select>
                                            <div id="msg_select_product_name"></div>
                                        </div>
                                        <div class="col-md-4">
                                            <label for="unit_price" class="">Unit Price</label>
                                            <input name="unit_price" id="unit_price" tabindex="7" placeholder="Unit Price" type="number" class="form-control">
                                            <div id="msg_unit_price"></div>
                                        </div>

                                    </div>
                                    <div class="form-row form-group">
                                        <div class="col-md-3">
                                            <label for="quantity" class="">Quantity</label>
                                            <input name="quantity" id="quantity" tabindex="8" placeholder="Quantity" type="number" class="form-control">
                                            <div id="msg_quantity"></div>
                                        </div>
                                        <div class="col-md-3">
                                            <label for="cgst" class="">CGST </label><label  style="font-size: 14px;background-color: yellow"><b>&nbsp; 2.5%</b></label>
                                            <input name="cgst" id="cgst" placeholder="CGST" disabled="true" value="00.00" type="number" class="form-control">
                                            <div id="msg_cgst"></div>
                                        </div>
                                        <div class="col-md-3">
                                            <label for="sgst" class="">SGST</label><label  style="font-size: 14px;background-color: yellow"><b>&nbsp; 2.5%</b></label>
                                            <input name="sgst" id="sgst" placeholder="SGST" disabled="true" value="00.00" type="number" class="form-control">
                                            <div id="msg_sgst"></div>
                                        </div>
                                        <div class="col-md-3">
                                            <label for="igst" class="">IGST</label><label  style="font-size: 14px;background-color: yellow"><b>&nbsp; 5%</b></label>
                                            <input name="igst" id="igst" placeholder="IGST" disabled="true" value="00.00" type="number" class="form-control">
                                            <div id="msg_igst"></div>
                                        </div>


                                    </div>

                                    <div class="form-row form-group">

                                        <div class="col-md-3">
                                            <label for="t_amount" class="">Total Amount</label>
                                            <input name="t_amount" id="t_amount" disabled="true" placeholder="Total Amount" class="form-control">
                                            <div id="msg_t_amount"></div>
                                        </div>
                                        <div class="col-md-2">
                                            <br>
                                            <button type="button" class="mt-2 btn btn-primary form-control font-size-lg" tabindex="9" id="btn_generate_bill">Generate Bill</button>
                                        </div>
                                    </div>

                                </form>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="tab-content">
                    <div class="tab-pane tabs-animation fade active show" id="tab-content-0" role="tabpanel">
                        <div class="main-card mb-3 card">
                            <h5 class="card-title" style="float: none;text-align: center;margin-top: 10px">Bill</h5>
                            <div class="card-body" id="print_divtag" tabindex="10">

                            </div>
                            <div class="row col-md-2">
                                <button type="button" class="btn btn-primary form-control" tabindex="11" style="margin: 10px;font-size: 18px" onFocus="inputFocus();" id="print_button">Print Bill</button>
                            </div>

                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
</div>


<script type="text/javascript">

    function inputFocus() {
        $(".print_button").css("border-bottom", "1px solid #ffba00");
    }
    ;

    $(document).ready(function () {
        $('#print_button').hide();
        $('#print_button').click(function () {
//            alert('print');
//            $("#print_divtag").clone().insertAfter("#print_divtag:last");
            printDiv();
        })

        
        

        function printDiv()
        {
            //                                                alert('click');
            var divToPrint = document.getElementById('print_divtag');
//            var newWin = window.open('', '_blank');
            var newWin = window.open('', 'Print-Window');

            newWin.document.open();

            newWin.document.write('<html> <body onload="window.print()"><link href="./main.css" rel="stylesheet">' + divToPrint.innerHTML + '</body></html>');

            newWin.document.close();

            setTimeout(function () {
                newWin.close();
            }, 10);

        }

//        alert('hello');
        $("#vehicle_owner_name").blur(function () {
//            alert('hello '+$("#course_category").val());
            if ($("#vehicle_owner_name").val() == "")
            {
                $("#msg_vehicle_owner_name").fadeIn(500);
                $("#msg_vehicle_owner_name").html("* Vehicle Owner Name is Empty").css({'color': 'red'});
                $("#msg_vehicle_owner_name").focusin();
            } else
            {
                $("#msg_vehicle_owner_name").fadeOut(1000);
            }
        });

        $("#vehicle_no").blur(function () {
//            alert('hello '+$("#course_category").val());
            if ($("#vehicle_no").val() == "")
            {
                $("#msg_vehicle_no").fadeIn(500);
                $("#msg_vehicle_no").html("* Vehicle No is Empty").css({'color': 'red'});
                $("#msg_vehicle_no").focusin();
            } else
            {
                $("#msg_vehicle_no").fadeOut(1000);
            }
        });
        $("#address").blur(function () {
//            alert('hello '+$("#course_category").val());
            if ($("#address").val() == "")
            {
                $("#msg_address").fadeIn(500);
                $("#msg_address").html("* Address is Empty").css({'color': 'red'});
                $("#msg_address").focusin();
            } else
            {
                $("#msg_address").fadeOut(1000);
            }
        });

        $("#select-state-code").blur(function () {
//            alert('hello '+$("#select-state-code").val());
            if ($("#select-state-code").val() == "Select")
            {
                $("#msg_select_state_code").fadeIn(500);
                $("#msg_select_state_code").html("* State Code Not Selected").css({'color': 'red'});
                $("#msg_select_state_code").focusin();
            } else
            {
                $("#msg_select_state_code").fadeOut(1000);

            }
        });

        $("#select_place_of_supply").blur(function () {
//            alert('hello '+$("#select-state-code").val());
            if ($("#select_place_of_supply").val() == "Select")
            {
                $("#msg_select_place_of_supply").fadeIn(500);
                $("#msg_select_place_of_supply").html("* Place Of Supply Not Selected").css({'color': 'red'});
                $("#msg_select_place_of_supply").focusin();
            } else
            {
                $("#msg_select_place_of_supply").fadeOut(1000);
            }
        });

        $("#select_product_name").blur(function () {
//            alert('hello '+$("#select-state-code").val());
            if ($("#select_product_name").val() == "Select")
            {
                $("#msg_select_product_name").fadeIn(500);
                $("#msg_select_product_name").html("* Product Name Not Selected").css({'color': 'red'});
                $("#msg_select_product_name").focusin();
            } else
            {
                $("#msg_select_product_name").fadeOut(1000);
            }
        });

        $("#unit_price").blur(function () {
//            alert('hello '+$("#course_category").val());
            if ($("#unit_price").val() == "")
            {
                $("#msg_unit_price").fadeIn(500);
                $("#msg_unit_price").html("* Unit Price is Empty").css({'color': 'red'});
                $("#msg_unit_price").focusin();
            } else
            {
                $("#msg_unit_price").fadeOut(1000);
            }
        });

        $("#quantity").blur(function () {
//            alert('hello '+$("#course_category").val());
            if ($("#quantity").val() == "")
            {
                $("#msg_quantity").fadeIn(500);
                $("#msg_quantity").html("* Quantity is Empty").css({'color': 'red'});
                $("#msg_quantity").focusin();
            } else
            {
                $("#msg_quantity").fadeOut(1000);
//                alert('quantity ' + $("#quantity").val());
                var qty = $('#quantity').val();
                var unit_price = $('#unit_price').val();
                var cgst = $('#cgst').val();
                var sgst = $('#sgst').val();
                var igst = $('#igst').val();
                var t_amt = unit_price * qty;
                var net_amount;
                var state_code = $("#select-state-code").val();
                if (state_code == 'Gujarat (GJ-24)')
                {
                    var cgst_per = t_amt * 2.5 / 100;
                    var sgst_per = t_amt * 2.5 / 100;
                    $('#cgst').val(cgst_per);
                    $('#sgst').val(sgst_per);
                    $('#igst').val('0.00');
                    net_amount = t_amt + cgst_per + sgst_per;
                } else if (state_code == 'Maharashtra (MH-27)')
                {
                    var igst_per = t_amt * 5 / 100;
                    $('#cgst').val('0.00');
                    $('#sgst').val('0.00');
                    $('#igst').val(igst_per);
                    net_amount = t_amt + igst_per;
                }

                $('#t_amount').val(net_amount);


            }
        });

        function clear()
        {
            $("#vehicle_owner_name").val('');
            $("#vehicle_no").val('');
            $("#address").val('');
            $("#select-state-code").val('Select');
            $("#select_place_of_supply").val('Select');
            $("#select_product_name").val('Select');
//            $("#hsn-code").text();
            $("#unit_price").val('');
            $("#quantity").val('');
            $("#cgst").val('');
            $("#sgst").val('');
            $("#igst").val('');
            $("#t_amount").val('');
        }

        $('#btn_generate_bill').click(function () {
            var vehicle_owner_name = $("#vehicle_owner_name").val();
            var vehicle_no = $("#vehicle_no").val();
            var address = $("#address").val();
            var select_state_code = $("#select-state-code").val();
            var select_place_of_supply = $("#select_place_of_supply").val();
            var select_product_name = $("#select_product_name").val();
            var hsn_code = $("#hsn-code").text();
            var unit_price = $("#unit_price").val();
            var quantity = $("#quantity").val();
            var cgst = $("#cgst").val();
            var sgst = $("#sgst").val();
            var igst = $("#igst").val();
            var total_amount = $("#t_amount").val();

            var send = {'vehicle_owner_name': vehicle_owner_name, 'vehicle_no': vehicle_no, 'address': address, 'select_state_code': select_state_code, 'select_place_of_supply': select_place_of_supply, 'hsn_code': hsn_code, 'select_product_name': select_product_name, 'unit_price': unit_price, 'quantity': quantity, 'cgst': cgst, 'sgst': sgst, 'igst': igst, 'total_amount': total_amount};

            if (vehicle_owner_name == '')
            {
                $("#msg_vehicle_owner_name").html(" * Vehicle Owner Name is Empty").css({'color': 'red'});
            } else if (vehicle_no == '')
            {
                $("#msg_vehicle_no").html(" * Vehicle No is Empty").css({'color': 'red'});
            } else if (address == '')
            {
                $("#msg_address").html(" * Address is Empty").css({'color': 'red'});
            } else if (select_state_code == 'Select')
            {
                $("#msg_select_state_code").html(" * State Code Not Selected").css({'color': 'red'});
            } else if (select_place_of_supply == 'Select')
            {
                $("#msg_select_place_of_supply").html(" * Place Of Supply Not Selected").css({'color': 'red'});
            } else if (select_product_name == 'Select')
            {
                $("#msg_select_product_name").html(" * Product Name Not Selected").css({'color': 'red'});
            } else if (unit_price == '')
            {
                $("#msg_unit_price").html(" * Unit Price is Empty").css({'color': 'red'});
            } else if (quantity == '')
            {
                $("#msg_quantity").html(" * Quantity is Empty").css({'color': 'red'});
            } else
            {
//                alert('Success ');
                debugger;
                $.ajax({
                    url: './PHP_Code/action_register.php',
                    type: 'POST',
                    data: send,
                    success: function (data)
                    {
                        debugger;
//                        alert('data ' + data);
                        $('#print_divtag').html('');
                        $('#print_divtag').html(data);
                        $('#print_button').fadeIn(1000);

                        clear();
                        $('html, body').animate({
                            scrollTop: $("#btn_generate_bill").offset().top
                        }, 500);
                    },
                    error: function (err) {
                        alert('error ' + err);
                    }
                });
            }
        });
    });

</script>

<?php

require_once './admin_footer.php';
?>



