<?php 
    include 'config.php';
    $id = $_POST['id'];
    $hawb = $_POST['hawb'];
    $ref_no = $_POST['ref_no'];
    $name = $_POST['name'];
    $address = $_POST['address'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $net = $_POST['net'];
    $gross = $_POST['gross'];
    $inv_no = $_POST['inv_no'];
    $inv_date = $_POST['inv_date'];
    $no_of_pack = $_POST['no_of_pack'];
    $goods_desc = $_POST['goods_desc'];
    $no_of_pieces = $_POST['no_of_pieces'];
    $m_country = $_POST['m_country'];
    $inv_curr = $_POST['inv_curr'];
    $inv_val = $_POST['inv_val'];
    $freight = $_POST['freight'];
    $freight_curr = $_POST['freight_curr'];
    $insurance = $_POST['insurance'];
    $ins_curr = $_POST['ins_curr'];
    $inco = $_POST['inco'];
    $pack_type = $_POST['pack_type'];
    $inv_type = $_POST['inv_type'];
    $echo = '';
    $echo .= '
    <style>
        .ui-autocomplete { z-index:2147483647; }
        .form-group.required .control-label:after
        {
            content:"*";
            color:red;
        }
    </style>
	<div class="row">
        <div class="col-md-8">
	        <fieldset>
	            <legend style=" width: 80px !important;margin-left: 10px;padding-left: 19px !important;">HAWB</legend> 
                <div class="row">
                    <div class="col-md-3">
                        <input type="text" name="m_hawb" id="m_hawb" class="disstyle form-control" value="'.$hawb.'" readonly>  
                        <p class="pull-left control-label" style="font-size:13px;">HAWB</p>  
                    </div> 
                    <div class="col-md-3">
                        <input type="number" step="0.1" name="no_of_pieces" id="no_of_pieces" class="disstyle form-control" value="'.$no_of_pieces.'">    
                        <p class="pull-left" style="font-size:13px;">No. of Pieces  </p>
                    </div>
                    <div class="col-md-3">
                        <select name="pack_type" id="pack_type" class="form-control ">';
                                $sql_stmt = "SELECT * FROM package_master";
                                $result = mysqli_query($con,$sql_stmt); 
                                while($row = mysqli_fetch_array($result))
                                {
                                    $echo .='<option value=';
                                    $echo .=  $row["code"];                                
                                    if ($row["code"] == $pack_type)
                                    {
                                        $echo .= 'selected';
                                    }
                                    $echo .=  '>' .$row["type"] .'</option>';
                                }
                                $echo .='  
                            </select>
                        <p class="pull-left" style="font-size:13px;">Package Type </p>                                                        
                    </div>         
                    <div class="col-md-3">
                        <input type="text" name="dec_ref_no" id="dec_ref_no" class="disstyle form-control" value="'.$ref_no.'">  
                        <p class="pull-left" style="font-size:13px;">Declarants Ref No </p>  
                    </div> 
		        </div> 
	        </fieldset>	
	        <fieldset>
	            <legend style="    width: 122px !important;
                margin-left: 10px;
                padding-left: 19px !important;">Invoice Details</legend> 	
                <div class="row">
                    <div class="col-md-4">
                        <input type="text" name="inv_no" id="inv_no" class="disstyle form-control" value="'.$inv_no.'" onkeyup="removeSpecials(this.value)"> 
                        <p class="pull-left" style="font-size:13px;">INV No. </p>   
                    </div>                
                    <div class="col-md-4">
                        <input type="date" name="inv_date" id="inv_date" class="disstyle form-control datepicker" value="';
                        if($inv_date != "")
                        {
                            $echo .= date('Y-m-d',strtotime($inv_date));
                        }
                        else
                        {
                            $echo .= date('Y-m-d');
                        }
                        $echo .='"> 
                        <p class="pull-left" style="font-size:13px;">INV Date</p>   
                    </div>
                    <div class="col-md-4">
                            <input type="number" step="0.1" name="no_of_pack" id="no_of_pack" class="disstyle form-control" value="'.$no_of_pack.'">   
                            <p class="pull-left" style="font-size:13px;">INV No. of Pages  </p> 
                    </div>          
                    <div class="col-md-4 form-group required">
                        <select name="inv_type" id="inv_type" class="form-control">';
                                $sql_stmt = "SELECT * FROM invoice_type";
                                $result = mysqli_query($con,$sql_stmt); 
                                while($row = mysqli_fetch_array($result))
                                {
                                    $echo .='<option value=';
                                    $echo .=  $row["id"];                                
                                    if ($row["id"] == $inv_type)
                                    {
                                        $echo .= 'selected';
                                    }
                                    $echo .=  '>' .$row["type"] .'</option>';
                                }
                                $echo .=' 
                        </select>
                        <p class="pull-left control-label" style="font-size:13px;">Invoice type </p>                                                        
                    </div>  
                    <div class="col-md-4 form-group required">
                        <input type="text" name="inv_curr" id="inv_curr" class="disstyle form-control currency" value="'.$inv_curr.'"> 
                        <p class="pull-left control-label" style="font-size:13px;">INV Currency</p>   
                    </div>
                <div class="col-md-4 form-group required">
                    <input type="number" name="inv_val" id="inv_val" class="disstyle form-control" value="'.$inv_val.'" step="0.1">   
                    <p class="pull-left control-label" style="font-size:13px;">INV Value  </p> 
                </div>  
            </div>
        </fieldset>
        <fieldset>
            <legend style="width: 122px !important;
            margin-left: 10px;
            padding-left: 19px !important;">Goods Details</legend> 
            <div class="row">                                 
                <div class="col-md-4 form-group required">
                    <input type="text" name="goods_desc" id="goods_desc" class="disstyle form-control" value="'.$goods_desc.'">    
                    <p class="pull-left control-label" style="font-size:13px;">Goods Description </p>
                </div>
                <div class="col-md-4 form-group required">
                    <input type="number" step="0.1" name="total_gross" id="total_gross" class="disstyle form-control" value="'.$gross.'"> 
                    <p class="pull-left control-label" style="font-size:13px;"> Gross weight - kg</p>   
                </div> 
                <div class="col-md-4 form-group required">
                    <input type="number" step="0.1" name="total_net" id="total_net" class="disstyle form-control" value="'.$net.'"> 
                    <p class="pull-left control-label" style="font-size:13px;"> Net weight - kg</p>   
                </div>
                <div class="col-md-4  ui-widget">
                    <input type="text" name="m_country" id="m_country" class="disstyle form-control" value="'.$m_country.'"> 
                    <p class="pull-left" style="font-size:13px;">Country Of Manufacturer </p>   
                </div>   
                <div class="col-md-4 form-group required">
                    <select name="m_goods_cond" id="m_goods_cond" class="form-control">
                        <option value="N">NEW</option>
                        <option value="U">USED</option>
                    </select>
                    <p class="pull-left control-label" style="font-size:13px;">Goods Condition  </p>
                </div> 
            </div>
        </fieldset>
    </div> 
    <div class="col-md-4">
        <fieldset>
	        <legend style="width: 109px !important;
            margin-left: 10px;
            padding-left: 19px !important;">INCO Terms</legend> 
            <div class="row">
                <div class="col-md-6 form-group required">
                    <input type="text" name="inco" id="inco" class="disstyle form-control" value="'.$inco.'">    
                    <p class="pull-left control-label" style="font-size:13px;">INCO Terms  </p>
                </div>
            </div>
            <div class="row">
                <div class="col-md-6 fri form-group required">
                    <input type="number" step="0.1" name="freight" id="freight" class="disstyle form-control" value="'.$freight.'">    
                    <p class="pull-left control-label" style="font-size:13px;">Freight  </p>
                </div>
                <div class="col-md-6 fri form-group required">
                    <input type="text" name="freight_curr" id="freight_curr" class="disstyle form-control currency" value="'.$freight_curr.'"> 
                    <p class="pull-left control-label" style="font-size:13px;">Freight Currency </p>   
                </div>                
                <div class="col-md-6 insu form-group required">
                        <input type="number" step="0.1" name="insurance" id="insurance" class="disstyle form-control" value="'.$insurance.'">   
                        <p class="pull-left control-label" style="font-size:13px;">Insurance  </p> 
                </div>
                <div class="col-md-6 insu form-group required">
                    <input type="text" name="ins_curr" id="ins_curr" class="disstyle form-control currency" value="'.$ins_curr.'">    
                    <p class="pull-left control-label" style="font-size:13px;">Insurance Currency </p>
                </div>
            </div>
        </fieldset>
        <div class="row">
            <div class="col-md-12" style="text-align: center;position: absolute;bottom: 36px;">
                <button id="'.$id.'" class="btn btn-primary btn-lg add">Submit</button>
                <button type="button" class="btn btn-primary btn-lg" id = "cancel" aria-label="Close">Cancel</button>
            </div>
        </div>
    </div>
</div>
<script>
    $(function() {
        $( "#m_country" ).autocomplete({
            source: "destination.php",
            minLength: 0
        }).focus(function(){     
            //Use the below line instead of triggering keydown
            $(this).data("uiAutocomplete").search($(this).val());
        });
    });
</script>
<script>            
    $("#cancel").click(function()
    {
        if(confirm("Are you sure?"))
        {
            $(".modal").modal("hide");
        }
    });

    function removeSpecials(str)
    {
        var res = str.replace(/\W+/g, " ");                
        return res;
    }

    $("#inv_no").on("keyup", function()
    {
        var str = $(this).val();
        var res = str.replace(/\W+/g, " ");                
        $("#inv_no").val(res);
    });

    $("#dec_ref_no").on("keyup", function()
    {   
        var str = $(this).val();
        var res = str.replace(/\W+/g, " ");                
        $("#dec_ref_no").val(res);
    });

    $("#goods_desc").on("keyup", function()
    {
        var str = $(this).val();
        var res = str.replace(/\W+/g, " ");                
        $("#goods_desc").val(res);
    });

    $(".add").click(function()
    {
        var regime_type = $("#regime_type").val();
        var term = $("#inco").val();
        var id = $(".add").attr("id");                
        if($("#total_net").val() == "" || $("#total_gross").val() == "" || $("#inv_curr").val() == "" || $("#inv_val").val() == "" || $("#inco").val() == "" || $("#goods_desc").val() == "")
        {
            alert("Please Fill All Mandatory Fields");
            return false;
        }
        else
        {
            if(regime_type == 1)
            {
                if(term == "FOB" || term == "EXW" || term == "FAS" || term == "FCA" || term == "CFR" || term == "CPT" )
                {
                    if($("#insurance").val() == "" || $("#ins_curr").val() == "")
                    {
                        alert("Please Fill Insurance value & currency");
                        $("tr."+id).css("background-color", "red");
                        return false;
                    }    
                    else
                    {
                        $("tr."+id).css("background-color", "white");
                    }                      
                }  
                if(term == "FOB" || term == "EXW" || term == "FAS" || term == "FCA")
                {
                    if($("#freight").val() == "" || $("#freight_curr").val() == "")
                    {
                        alert("Please Fill Freight value & currency");
                        $("tr."+id).css("background-color", "red");
                        return false;
                    }   
                    else
                    {
                        $("tr."+id).css("background-color", "rgb(0 0 0 / 7%)");
                    }                       
                }
            }
            if($("#inv_curr").val() != "")
            {
                var d;
                var currency = $("#inv_curr").val();
                $.ajax({
                    type : "post",
                    data : { currency : currency},
                    url : "check_currency.php",
                    async: false,
                    success : function(code)
                    {
                        if(code < 1)
                        {
                            d = code;
                        }
                    }
                });
                if( d < 1 )
                {
                    alert("Invalid Currency Code in Invoive Currency!!");
                    $("tr."+id).css("background-color", "red");
                    return false;
                }
                else
                {
                    $("tr."+id).css("background-color", "rgb(0 0 0 / 7%)");
                }
            }
            if($("#m_country").val() != "")
            {
                var s;
                var dest = $("#m_country").val();
                if(dest != "")
                {
                    $.ajax({
                        type : "post",
                        url : "check_country.php",
                        data : {term : dest},
                        async: false,
                        success : function(code)
                        {
                            if(code < 1)
                            {
                                s = code;
                            }
                        }
                    });
                    if( s < 1 )
                    {   
                        alert("Invalid Country Code in Country of Origin!!");
                        $("tr."+id).css("background-color", "red");
                        $("#m_country").val("");
                        return false;
                    }
                    else
                    {
                        $("tr."+id).css("background-color", "rgb(0 0 0 / 7%)");
                    }
                }
                else
                {
                    $("tr."+id).css("background-color", "rgb(0 0 0 / 7%)");
                }

            }
        }
    });
    $(document).ready(function()
    {
        var term  = $("#inco").val();
        var regime_type = $("#regime_type").val();
        if(regime_type == 1)
        {
            if(term == "CIF" || term == "CIP"||term == "DAF" || term == "DDP"||term == "DDU" || term == "DEQ"||term == "DES" || term == "DAP"||term == "DAT")
            {
                $("#insurance").val("");
                $("#ins_curr").val("");
                $("#freight").val("");
                $("#freight_curr").val("");
                $(".insu").hide();
                $(".fri").hide();

            }
            else if(term == "FOB" || term == "EXW" || term == "FAS" || term == "FCA" )
            {
                $(".insu").show(); 
                $(".fri").show();                         
            }
            else if(term == "CFR" || term == "CPT")
            {
                $(".insu").show(); 
                $("#freight").val("");
                $("#freight_curr").val("");
                $(".fri").hide();
            }  
        }  
        else
        {
            $("#insurance").val("");
            $("#ins_curr").val("");
            $("#freight").val("");
            $("#freight_curr").val("");
            $(".insu").hide();
            $(".fri").hide();
        }
    });

    $("#m_country").blur(function()
    {
        var dest = $(this).val();
        if(dest != "")
        {
            $.ajax({
                type : "post",
                url : "check_country.php",
                data : {term : dest},
                success : function(code)
                {
                    if(code < 1)
                    {
                        alert("Invalid Country !!");
                        $("#m_country").val("");
                    }
                    else
                    {
                        convert();
                        getCountry($("#m_country").val());
                    }
                }                    
            })
        }
        
    });
    
    function convert()
    {
        var str = $("#m_country").val();
        str1 = str.toUpperCase();
        $("#m_country").val(str1);
    }

    $("#inv_curr").change(function()
    {
        var str = $(this).val();
        var currency = str.toUpperCase();
        $("#inv_curr").val(currency);
        $.ajax({
            type : "post",
            data : { currency : currency},
            url : "check_currency.php",
            success : function(code)
            {
                if(code < 1)
                {
                    alert("Invalid Currency Code!!");
                    $("#inv_curr").val("");
                }
            }
        });
    });

    function getCountry(string)
    {
        $.ajax
        ({
            type : "post",
            url : "getCountry.php",
            async: false,
            data : {term : string},
            success : function(code)
            {
                $("#m_country").val(code)                      
            }                    
        });
    }
    
    $("#freight_curr").change(function()
    {
        var str = $(this).val();
        var currency = str.toUpperCase();
        $("#freight_curr").val(currency);
        $.ajax({
            type : "post",
            data : { currency : currency},
            url : "check_currency.php",
            success : function(code)
            {
                if(code < 1)
                {
                    alert("Invalid Currency Code!!");
                    $("#freight_curr").val("");
                }
            }
        });
    });

    $("#ins_curr").change(function()
    {
        var str = $(this).val();
        var currency = str.toUpperCase();
        $("#ins_curr").val(currency);
        $.ajax({
            type : "post",
            data : { currency : currency},
            url : "check_currency.php",
            success : function(code)
            {
                if(code < 1)
                {
                    alert("Invalid Currency Code!!");
                    $("#ins_curr").val("");
                }
            }
        });
    });

    $(document).on("change","#inco",function()
    {
        var term  = $(this).val();
        var str = term.toUpperCase();
        $("#inco").val(str);
        var regime_type = $("#regime_type").val();
        if(regime_type == 1)
        {
            if(str == "CIF" || str == "CIP"||str == "DAF" || str == "DDP"||str == "DDU" || str == "DEQ"||str == "DES" || str == "DAP"||str == "DAT")
            {
                $("#insurance").val("");
                $("#ins_curr").val("");
                $("#freight").val("");
                $("#freight_curr").val("");
                $(".insu").hide();
                $(".fri").hide();
            }
            else if(str == "FOB" || str == "EXW" || str == "FAS" || str == "FCA" )
            {
                $(".insu").show(); 
                $(".fri").show();                         
            }
            else if(str == "CFR" || str == "CPT")
            {
                $(".insu").show(); 
                $(".fri").hide();
                $("#freight").val("");
                $("#freight_curr").val("");            }  
            else
            {
                alert("invalid inco term!!");
                $("#inco").val("");
            }
        }     
        else if(regime_type == 2)
        {
            if(str == "CIF" || str == "CIP"||str == "DAF" || str == "DDP"||str == "DDU" || str == "DEQ"||str == "DES" || str == "DAP"||str == "DAT" || str == "FOB" || str == "EXW" || str == "FAS" || str == "FCA" || str == "CFR" || str == "CPT")
            {
                $(".insu").hide();
                $(".fri").hide();
                $("#insurance").val("");
                $("#ins_curr").val("");
                $("#freight").val("");
                $("#freight_curr").val("");
            }
            else
            {
                alert("invalid inco term!!");
                $("#inco").val("");
            }
        }

    });
    </script>';
    echo $echo; 
?>
