<?php include("auth.php");

require("conn.php");
$action = "Add";
$id = null;

if (isset($_GET['action'])) {
    $action = base64_decode($_GET['action']);
    $id = base64_decode($_GET['id']);

    $sql = "select * from tbl_customer where id='$id'";
    $smt = mysqli_query($conn, $sql);
    $row = mysqli_fetch_assoc($smt);
}

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">

    <title>Create Customer </title>
    <meta content="" name="description">
    <meta content="" name="keywords">
    <link href="assets/img/favicon.png" rel="icon">
    <link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
    <?php include("templates/inc.php"); ?>

    <script src="scripts/customer.js" type="text/javascript" language="javascript"></script>
      <script>
  
    //Upload image_url 

            $(document).on('change', '#file_url', function () {
                var name = document.getElementById("file_url").files[0].name;
                var form_data = new FormData();
                var ext = name.split('.').pop().toLowerCase();
                if (jQuery.inArray(ext, ['pdf', 'jpeg', 'jpg', 'png']) == -1) {
                    alert("Invalid Image File");
                }
                var oFReader = new FileReader();
                oFReader.readAsDataURL(document.getElementById("file_url").files[0]);
                var f = document.getElementById("file_url").files[0];
                var fsize = f.size || f.fileSize;
                if (fsize > 2000000) {
                    alert("Image File Size is very big");
                } else {
                    form_data.append("file_url", document.getElementById('file_url').files[
                        0]);

                    $.ajax({

                        url: "upload.php",
                        method: "POST",
                        data: form_data,
                        contentType: false,
                        cache: false,
                        processData: false,
                        beforeSend: function () {
                            $('#uploaded_file_url').html(
                                "<label class='text-success'>Uploading...</label>");
                        },
                        success: function (data) {
                            $('#uploaded_file_url').html(data);
                        }
                    });
                }
            });



  
  
  
  $(function () {
  $("#state_id").change(function(){
	 
   var id=$("#state_id").val();
   
	
		$.ajax({
			 type:'post',
			 url: 'get-districts.php',
			 data: {'id':id},
			 success: function(data)
			 {	 
			    
				 $("#district_id").html(data); 
				
			     
			  }
		 });
       });
       
       
       
  });

  </script>

</head>

<body>

    <!-- ======= Header ======= -->
    <?php
    include("templates/header.php");
    ?>

    <!-- End Header -->

    <!-- ======= Sidebar ======= -->
    <?php
    include("templates/sidebar.php");

    ?>
    <!-- End Sidebar-->

    <main id="main" class="main">

        <div class="pagetitle">
            <h1>Create Customer </h1>
            <nav>
                <ol class="breadcrumb">
                    <li class="breadcrumb-item"><a href="dashboard.php">Dashboard</a></li>
                    <li class="breadcrumb-item">Create Customer </li>

                </ol>
            </nav>
            <hr>
           
        </div><!-- End Page Title -->

        <section class="section">
            <div class="row">
                <div class="col-lg-12">

                    <div class="card">
                        <div class="card-body">
                            <div class="card-body">
                                <h5 class="card-title">Add New Customer</h5>

                                <!-- Custom Styled Validation -->
                                <form id="customer" action="customer-action.php" class="row g-3 needs-validation" novalidate="">
                                    <input value="<?php echo $_SESSION['s_code']; ?>" type="hidden" class="form-control" id="s_code" name="s_code">

                                    <div class="col-md-12">
                                        <label for="validationDefault04" class="form-label"><b>Services</b></label>
                                        <select class="form-select" id="services" name="services" required="">
                                            <option selected="salacted" value="0">Choose Service...</option>
                                                <?php
                                                include("./conn.php");
                                               $sql = "select * from tbl_services where deleted_at is NULL order by id asc";
                                                $smt = mysqli_query($conn, $sql);
                                                while ($rs = mysqli_fetch_assoc($smt)) 
                                                {
                                                    echo '<option value="'.$rs['services'].'">'.$rs['services'].'</option>';
                                                }
                                                ?>
                                        </select>
                                    </div>

                                     <div class="col-md-6">
                                        <label for="validationCustom01" class="form-label"><b> Custome Name</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['name']; ?>" type="text" class="form-control" id="name" name="name" required="">
                                        <div class="invalid-feedback">
                                            Please provide a Name.
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <label for="validationCustom01" class="form-label"><b>Mobile</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['mobile']; ?>" type="number" class="form-control" id="mobile" name="mobile" required="">
                                        <div class="invalid-feedback">
                                            Please provide a mobile.
                                        </div>
                                    </div>
                                    
                                    <div class="col-md-6">
                                        <label for="validationCustom01" class="form-label"><b>Email</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['email']; ?>" type="email" class="form-control" id="email" name="email" required="">
                                        <div class="invalid-feedback">
                                            Please provide a email.
                                        </div>
                                    </div>
                                    
                                     <div class="col-md-6">
                                        <label for="validationCustom01" class="form-label"><b>Whatsapp No</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['whatsapp']; ?>" type="number" class="form-control" id="whatsapp" name="whatsapp" required="">
                                        <div class="invalid-feedback">
                                            Please provide a Whatsapp No.
                                        </div>
                                    </div>

                                  <div class="col-md-12">
                                        <label for="validationCustom01" class="form-label"><b>Website Url</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['web_url']; ?>" type="text" class="form-control" id="web_url" name="web_url" required="">
                                        <div class="invalid-feedback">
                                            Please provide a web url.
                                        </div>
                                  </div>

                                 

                                   <div class="col-md-6">
                                        <label for="validationDefault04" class="form-label"><b>State</b></label>
                                        <select class="form-select" id="state_id" name="state_id" required="">
                                            <option selected="salacted" value="0">Choose...</option>
                                                <?php
                                                include("./conn.php");
                                                $sql = "select * from state order by state_title asc";
                                                $smt = mysqli_query($conn, $sql);
                                                while ($rs = mysqli_fetch_assoc($smt)) 
                                                {
                                                    echo '<option value="'.$rs['id'].'">'.$rs['state_title'].'</option>';
                                                }
                                                ?>
                                        </select>
                                    </div>


                                     <div class="col-md-6">
                                        <label for="validationDefault04" class="form-label"><b>District</b></label>
                                        <select class="form-select" id="district_id" name="district_id" required="">
                                            
                                        </select>
                                    </div>


                                    <div class="col-md-12">
                                        <label for="validationCustom01" class="form-label"><b>Address</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['address']; ?>" type="text" class="form-control" id="address" name="address" required="">
                                        <div class="invalid-feedback">
                                            Please provide a address.
                                        </div>
                                  </div>
                               

                                 <div class="col-md-12">
                                        <label for="validationCustom01" class="form-label"><b>Requirment</b></label>
                                        <input value="<?php if (isset($_GET['id'])) echo $row['req']; ?>" type="text" class="form-control" id="req" name="req" required="">
                                        <div class="invalid-feedback">
                                            Please provide require.
                                        </div>
                                  </div>
                               
                                  <div class="col-md-12">
                                        <div class="form-group">
                                            <label for="formFileMultiple" class="form-label"><b>Upload File( Service Enrolment Form )</b></label>
                                            <input class="form-control" value="<?php if(isset($_GET['id'])) echo $row['file_url']?>" type="file" id="file_url" name="file_url"
                                                multiple />
                                            <span id="uploaded_file_url"></span>
                                        </div>
                                    </div>


                                    <div class="col-12">
                                        <input type="hidden" name="id" id="id" value="<?php echo $id; ?>" />
                                        <input type="hidden" name="action" id="action" value="<?php echo $action; ?>" />
                                        <?php if (isset($_GET['id'])) {
                                        ?>
                                            <button class="btn btn-lg btn-primary btn-sm" type="submit">Update</button>
                                            <a href="customer-details-list.php"><button class="btn btn-sm btn-danger btn-lg" type="button">Cancel</button></a>
                                        <?php
                                        } else {
                                        ?>

                                    <button class="btn btn-primary  btn-sm" id="btnAdd" type="submit">Add New</button>
                                        <?php
                                        }
                                        ?>
                                        <br>
                                        <h4 id="msg"></h4>
                                    </div>
                                </form><!-- End Custom Styled Validation -->

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

                </div>
            </div>

        </section>







    </main><!-- End #main -->




    <!-- ======= Footer ======= -->
    <?php
    include("templates/footer.php");
    ?>
    <!-- End Footer -->


    <?php include("templates/vendors.php"); ?>

</body>

</html>