<?php 
$template_id  = $_REQUEST['id'];
?>
<form id="domesticForm" name="domesticForm" enctype="multipart/form-data" class="form-horizontal" method="post"
    action="javascript:;">
    <div class="row">
        <div class="col-lg-6">
            <div class="form-group row">
                <label class="control-label col-lg-6">Invoice No<em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_no" name="inv_no" class="form-control " />
                    <label id="dc_avail_result" for="dc_avail_result" style="display:none;">Please Enter Invoice
                        No</label>
                    <input type="hidden" id="templ_id" name="templ_id" value="<?php echo $template_id; ?>" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Invoice Date<em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_date" name="inv_date" class="form-control form_date" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6 float-left">Billed To <em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" name="bill_name" id="bill_name" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <div class="col-lg-6 float-left">
                    <label class="control-label float-left">Billed To Address<em class="required">&nbsp;*</em> </label>
                    <br />
                    <br />
                </div>
                <div class="col-lg-6 float-right">
                    <input type="text" name="bill_addr[]" class="form-control " />
                    <input type="text" name="bill_addr[]" class="form-control " />
                    <input type="text" name="bill_addr[]" class="form-control " />
                    <input type="text" name="bill_addr[]" class="form-control " />
                </div>
            </div>

        </div>
        <div class="col-lg-6">
            <div class="form-group row">
                <label class="control-label col-lg-6">GSTIN<em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="bill_gstin" name="bill_gstin" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">PAN<em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="bill_pan" name="bill_pan" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Place of supply<em class="required">&nbsp;*</em> </label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_pos" name="inv_pos" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">State Code</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_statecode" name="inv_statecode" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">PO Reference</label>
                <div class="col-lg-6 float-right">
                    <textarea id="inv_poref" name="inv_poref" form="invoiceForm" class="form-control "></textarea>
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">IRN No</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_irn" name="inv_irn" class="form-control " />
                </div>
            </div>
        </div>
    </div>
    <div class="row justify-content-center">
        <div class="col-12">
            <table id="product_table" class="table col-12">
                <thead>
                    <th>Particulars</th>
                    <th>HSN /Service Chapter Heading (Tariff)</th>
                    <th>Amount</th>
                    <th></th>
                </thead>
                <tbody>
                    <tr>
                        <td>
                            <input type="text" name="inv_Particulars[]" class="form-control" />
                        </td>
                        <td>
                            <input type="text" name="inv_Hsn[]" class="form-control" />
                        </td>
                        <td>
                            <input type="text" name="inv_Amount[]" class="form-control" />
                        </td>
                        <td>
                            <input value="Remove" class="button btn-danger btnDelete" type='button' />
                        </td>
                    </tr>
                    <tr class="ui-widget-header">
                        <td colspan="4">
                            <input value="Add another details" class="button btn-primary" type='button'
                                onclick="addProduct();" />
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <div class="row col-lg-12">
        <div class="col-lg-6">
            <div class="form-group row">
                <label class="control-label col-lg-6">Net Taxable Value</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_netVal" name="inv_netVal" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">CGST (Percentage)</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_taxCGST" Value="0%" name="inv_taxCGST" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">SGST (Percentage)</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_taxSGST" Value="0%" name="inv_taxSGST" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">IGST (Percentage)</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_taxIGST" Value="0%" name="inv_taxIGST" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Chess (Percentage)</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_taxChess" Value="0%" name="inv_taxChess" class="form-control " />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Chess (Amount)</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_taxChessAmt" Value="0.00" name="inv_taxChessAmt" class="form-control " />
                </div>
            </div>
            
        </div>
        <div class="col-lg-6 float-right">
            <div class="form-group row">
                <label class="control-label col-lg-6">CGST Value</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="taxCGST_val" name="taxCGST_val" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">SGST Value</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="taxSGST_val" name="taxSGST_val" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">IGST Value</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="taxIGST_val" name="taxIGST_val" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Round Off</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="roundOff_val" name="roundOff_val" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Total</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_total" name="inv_total" class="form-control prMoney" />
                </div>
            </div>
            <div class="form-group row">
                <label class="control-label col-lg-6">Whether Tax payable under Reverse Charge</label>
                <div class="col-lg-6 float-right">
                    <input type="text" id="inv_reverseCharge" Value="No" name="inv_reverseCharge"
                        class="form-control " />
                </div>
            </div>

        </div>
    </div>
    <div class="row">
        <div class="col-12">
            <div class="row">
                <div class="col-6">
                    <a id="validate" class="btn btn-warning col-12">Validate</a>
                </div>
                <div class="col-6 float-right">
                    <input type="submit" class="btn btn-primary col-12" value="Submit" />
                </div>
            </div>
        </div>
    </div>
</form>

<script>
function addProduct() {
    var $lastRow = $("[id$=product_table] tr:not('.ui-widget-header'):last");
    var $newRow = $lastRow.clone();
    $newRow.find(":text").val(""); //clear out textbox values    
    $lastRow.after($newRow);
}
$("#product_table").on('click', '.btnDelete', function() {
    $(this).closest('tr').remove();
});
$('#validate').on('click', function(e) {
    e.preventDefault();
    if ($('#inv_no').val() == '') {
        swal('Require', 'Please Enter Invoice Number');
    } else if ($('#inv_date').val() == '') {
        swal('Require', 'Please Enter Invoice Date');
    } else if ($('#bill_name').val() == '') {
        swal('Require', 'Please Enter Biller Name');
    } else if ($('#inv_gstin').val() == '') {
        swal('Require', 'Please Enter GSTIN');
    } else if ($('#inv_pan').val() == '') {
        swal('Require', 'Please Enter PAN');
    } else if ($('#inv_pos').val() == '') {
        swal('Require', 'Please Enter Place of supply');
    } else if ($('#inv_statecode').val() == '') {
        swal('Require', 'Please Enter State Code');
    } else if ($('#inv_irn').val() == '') {
        swal('Require', 'Please Enter IRN Number');
    } else if ($('#inv_netVal').val() == '') {
        swal('Require', 'Please Enter Net Value');
    }
})


$(".prMoney").focusout(function() {
    var val = $(this).val();
    var num = parseFloat(val).toFixed(2);
    num = parseFloat(num).toLocaleString('en-IN');
    if (num.indexOf('.') < 0) {
        num = num + '.00';
    }
    $(this).val(num);
});
</script>
<script>
$('#domesticForm').on('submit', function(event) {
    event.preventDefault();
    var formdata = new FormData($(this)[0]);
    $.ajax({
        url: 'pages/domestic/save_domestic.php',
        method: 'POST',
        data: formdata,
        contentType: false,
        processData: false,
        dataType: 'json',
        success: function(response) {
            console.log(response);
            if (response.success) {
                swal('Success', 'Template created successfully')
                // $("#tmpl_Form")[0].reset();                    
            } else {
                // alert(response.error_derail);
                console.log(response.error_derail);
                swal('error', response.error_derail);
            }
        }
    })
});
</script>
<script type="text/javascript" src="scripts/savestorage.js" > </script>
<script type="text/javascript" src="scripts/savestorage.min.js" ></script>
<script>
$('#domesticForm').saveStorage({
    exclude: ['inv_Particulars','inv_Hsn','inv_Amount']
});
console.log('domestic form called');
</script>