<script type="text/javascript">
$(document).ready(function(){
	$("#frm-product").submit(function(e){
            if(!validate_product()){
                e.preventDefault();
            }
	});
        
	
});
function validate_product(){

	var plan_product = $("#plan_product");var planproductmsg = $("#msg_plan_product");
    var priceing_modal = $("#priceing_modal");var pricemodalmsg = $("#msg_priceing_modal");
    var plan_price = $("#plan_price");var plan_pricemsg = $("#msg_plan_price");
    var priceing_type = $("#priceing_type");var priceing_typemsg = $("#msg_priceing_type");
	var billing_period =$("#billing_period");var billing_periodmsg = $("#msg_billing_period");
    var plan_name =$("#plan_name");var plan_namemsg = $("#msg_plan_name");


    if(plan_product.val()=='')
    {
        plan_product.addClass('error');
        plan_product.focus();
        planproductmsg.html('Please select product name').show();
        return false;
	}else
    {
        plan_product.removeClass('error');
        planproductmsg.hide();
    }


    if(priceing_modal.val()=='')
    {
        priceing_modal.addClass('error');
        priceing_modal.focus();
        pricemodalmsg.html('Please select price modal').show();
        return false;
    }else
    {
        priceing_modal.removeClass('error');
        pricemodalmsg.hide();
    }

    if(plan_price.val()=='')
    {
        plan_price.addClass('error');
        plan_price.focus();
        plan_pricemsg.html('Please enter price').show();
        return false;
    }else
    {
        plan_price.removeClass('error');
        plan_pricemsg.hide();
    }

    if(priceing_type.val()=='')
    {
        priceing_type.addClass('error');
        priceing_type.focus();
        priceing_typemsg.html('Please enter price').show();
        return false;
    }else
    {
        priceing_type.removeClass('error');
        priceing_typemsg.hide();
    }

    if(billing_period.val()=='')
    {
        billing_period.addClass('error');
        billing_period.focus();
        billing_periodmsg.html('Please select billing period').show();
        return false;
    }else
    {
        billing_period.removeClass('error');
        billing_periodmsg.hide();
    }

    if(plan_name.val()=='')
    {
        plan_name.addClass('error');
        plan_name.focus();
        plan_namemsg.html('Please select plan').show();
        return false;
    }else
    {
        plan_name.removeClass('error');
        plan_namemsg.hide();
    }

    return true;
}

</script>
<div class="breadcrumb">
    <h1>Tax</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Tax</li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></div>
<div class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Tax</div>
                <form action="" id='frm-product' method="post" enctype="multipart/form-data">
                    <?php if(isset($taxDetail->taxrate_id) && $taxDetail->taxrate_id !=0) { ?>
                       <input type="hidden" id="taxrate_id" name="taxrate_id" value="<?php echo $taxDetail->taxrate_id; ?>" />
                       <input type="hidden" id="taxrate_id" name="taxrate_id" value="<?php echo $taxDetail->taxrate_id; ?>" />
                       
                    <?php } ?>
                    <div class="row">
                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="tax_type">Type </label>
                            <select class="form-control" id="tax_type" name="tax_type">
                                <option value="">Select Tax</option>
                                <!-- <option value="sales_tax">Sales tax</option>
                                <option value="vat">VAT</option>
                                <option value="gst">GST</option> -->
                                <option value="custom">Custom</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_tax_type"></div>
                        </div>
                    	
                        <div class="col-md-6 form-group mb-3 facilities" style="display: none;" id="div_tax_display_name">
                            <label for="tax_display_name">Display Name </label>
                            <input type="text" class="form-control" id="tax_display_name" name="tax_display_name" value="<?php echo $taxDetail->taxrate_display_name ?>" placeholder="Enter name hst">
                            <div class="invalid-tooltip" id="msg_tax_display_name"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="country_id">Country name <span class="asterisk">*</span></label>
                            <select class="form-control" id="country_id" name="country_id">
                            <option value="">Select Country</option>
                            <?php
                                  $countryobj = new country;
                                  $fields = array('country_id,country_name,country_shortcode');
                                  $countryArg = array("fields"=>$fields);
                                  $countrydata = $countryobj->getAllRecords($countryArg);
                                  foreach ($countrydata as $ckey => $country_value) 
                                    { 
                                      ?>
                                        <option value="<?php echo $country_value->country_id; ?>" <?php echo (isset($taxDetail) && $taxDetail->taxrate_country == $country_value->country_id) ? "selected='selected'" : ""; ?> ><?php echo $country_value->country_name; ?></option>
                              <?php }
                            ?>

                        </select>
                            <div class="invalid-tooltip" id="msg_country_id"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3">
                            <label for="state_id">State name <span class="asterisk">*</span></label>
                            <select class="form-control" id="state_id" name="state_id">
                            
                            </select>
                            <div class="invalid-tooltip" id="msg_state_id"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="tax_rate">Rate </label>
                            <input type="text" class="form-control" id="tax_rate" name="tax_rate" value="<?php echo $taxDetail->taxrate_percentage ?>" placeholder="Enter Tax Rate">
                            <div class="invalid-tooltip" id="msg_tax_rate"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="tax_mode">Tax Mode </label>
                            <select class="form-control" id="tax_mode" name="tax_mode">
                                <option value="exclusive">Exclusive</option>
                                <option value="inclusive">Inclusive</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_tax_mode"></div>
                        </div>

                        <div class="col-md-6 form-group mb-3 facilities">
                            <label for="tax_status">Status </label>
                            <select class="form-control" id="tax_status" name="tax_status">
                                <option value="active">Active</option>
                                <option value="inactive">Iactive</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_tax_status"></div>
                        </div>

                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="./assets/js/jquery.multiselect.js"></script>
<link rel="stylesheet" href="./assets/styles/css/jquery.multiselect.css">

<script type="text/javascript">
   
    $(document).ready(function() {

        $("#country_id").on('change',function(){

        var country_id = $("#country_id").val();
        $.ajax({
                  url: "taxadd.php?act=getstatedata&country_id="+country_id, 
                  type: "POST",
                  success: function(data)
                  {  
                    $("#state_id").html(data);  
                  }
             });
        });

        $("#tax_type").on('change',function(){
            var tax_type = $("#tax_type").val();
            
            if(tax_type == 'custom') 
            {
                $("#div_tax_display_name").show();
            }else{
                $("#div_tax_display_name").hide();
            } 
        });


        $( "form" ).on( "submit", function(e) {
            e.preventDefault();
            
            var tax_type = $("#tax_type").val();
            var tax_display_name = $("#tax_display_name").val();
            var country_id = $("#country_id").val();
            var state_id = $("#state_id").val();
            var tax_rate = $("#tax_rate").val();
            var tax_mode = $("#tax_mode").val();
            var tax_status = $("#tax_status").val();
            
            if(tax_type=='')
            {
              $("#tax_type").addClass('error');
              $("#tax_type").focus();
              $("#msg_tax_type").html('Please select tax type').show();
              return false;
            }
            else if(country_id =='')
            {
              if(tax_type !='')
              {
                $("#tax_type").removeClass('error');
                $("#msg_tax_type").hide();
              }
                
              $("#country_id").addClass('error');
              $("#country_id").focus();
              $("#msg_country_id").html('Please select country').show();
              return false;
            }
            else if(state_id =='')
            {
              if(country_id !='')
              {
                $("#country_id").removeClass('error');
                $("#msg_country_id").hide();
              }

              $("#state_id").addClass('error');
              $("#state_id").focus();
              $("#msg_state_id").html('Please select state').show();
              return false;
            }
            else if(tax_rate =='')
            {
              if(state_id !='')
              {
                $("#state_id").removeClass('error');
                $("#msg_state_id").hide();
              }

              $("#tax_rate").addClass('error');
              $("#tax_rate").focus();
              $("#msg_tax_rate").html('Please input tax rate').show();
              return false;
            }
            else
            {
                if(tax_rate !='')
                {
                  $("#tax_rate").removeClass('error');
                  $("#msg_tax_rate").hide();
                }  

                 $(".loader").show();
              
                 $.ajax
                 ({
                
                    url: "taxadd.php",
                    data: { act:"load",
                          tax_type:tax_type,tax_display_name:tax_display_name,country_id:country_id,state_id:state_id,tax_rate:tax_rate,tax_mode:tax_mode,tax_status:tax_status },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            $(".loader").hide();
                            window.location='taxlist.php';
                        }
                    }
                });
            }
        });

    });
</script>