<?php include "layouts/menu.php"; ?> 

<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
 

<div class="right_col" role="main">
  
	<style>
	#partitioned {
  padding-left: 15px;
  letter-spacing: 10px;
  border: 1;
  background-image: linear-gradient(to left, black 70%, rgba(255, 255, 255, 0) 0%);
  background-position: bottom;
  background-size: 20px 1px;
  background-repeat: repeat-x;
  background-position-x: 20px;
  width: 100%;
}
</style>  
            <div class="row">
                <div class="col-lg-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Details
                        </div>
                        <div class="panel-body">
                            <div class="row">
<?php $idd=$_REQUEST['id'];
$sql = mysqli_query($conn, "select * from mcust where id ='$idd'");
$row = mysqli_fetch_array($sql);
 ?>
  <form role="form" name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
  <div class="row">

    <!-- ================= MEMBER DETAILS ================= -->
    <div class="col-lg-12">
      <h3 style="background:#f5f5f5;padding:10px;border-radius:5px;">Member Details</h3>
    </div>

    <div class="col-lg-4">
      <div class="form-group has-success has-feedback">
        <label>Member ID <font color="red">*</font></label>
        <input type="text" class="form-control" name="hpl"
               value="<?php echo isset($row['hpl']) ? htmlspecialchars($row['hpl']) : ''; ?>"
               placeholder="E.g 1008" required>
        <input type="hidden" class="form-control" name="hplt" value="<?php echo isset($row['hplt']) ? htmlspecialchars($row['hplt']) : ''; ?>">
        <input type="hidden" class="form-control" name="mfin" value="<?php echo isset($row['mfin']) ? htmlspecialchars($row['mfin']) : ''; ?>">
        <input type="hidden" class="form-control" name="idd" value="<?php echo $idd; ?>">
        <span class="glyphicon glyphicon-phone-alt form-control-feedback"></span>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Member Name <font color="red">*</font></label>
        <input style="text-transform:uppercase" type="text" class="form-control"
               name="name" id="name"
               value="<?php echo isset($row['name']) ? htmlspecialchars($row['name']) : ''; ?>"
               onkeyup="autocomp()" placeholder="Name">
        <ul id="dname"></ul>
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Mobile Number <font color="red">*</font></label>
        <input type="text" class="form-control" name="pno"
               value="<?php echo isset($row['pno']) ? htmlspecialchars($row['pno']) : ''; ?>"
               placeholder="Eg: 9876543210">
        <span class="glyphicon glyphicon-phone-alt form-control-feedback"></span>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Father's Name</label>
        <input type="text" class="form-control" name="fname"
               value="<?php echo isset($row['fname']) ? htmlspecialchars($row['fname']) : ''; ?>"
               placeholder="Name">
        <span class="glyphicon glyphicon-user form-control-feedback"></span>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Father Phone No</label>
        <input type="text" class="form-control" name="pnoresi1"
               value="<?php echo isset($row['pnoresi1']) ? htmlspecialchars($row['pnoresi1']) : ''; ?>"
               placeholder="Eg: 9876543210">
        <span class="glyphicon glyphicon-phone-alt form-control-feedback"></span>
      </div>
    </div>

    <div class="col-lg-4">
      <div class="form-group has-success has-feedback">
        <label>Address</label>
        <input type="text" class="form-control" name="address"
               value="<?php echo isset($row['address']) ? htmlspecialchars($row['address']) : ''; ?>"
               placeholder="Line 1">
        <input type="text" class="form-control" name="address1"
               value="<?php echo isset($row['address1']) ? htmlspecialchars($row['address1']) : ''; ?>"
               placeholder="Line 2">
        <input type="text" class="form-control" name="address2"
               value="<?php echo isset($row['address2']) ? htmlspecialchars($row['address2']) : ''; ?>"
               placeholder="Line 3 + Pincode">
        <span class="glyphicon glyphicon-home form-control-feedback"></span>
      </div>

      <div class="form-group has-success has-feedback">
        <label>House Own/Rent</label>
        <select class="form-control" name="houserent">
          <option>Select</option>
          <option <?php if(isset($row['houserent']) && $row['houserent']=="Own House") echo "selected"; ?>>Own House</option>
          <option <?php if(isset($row['houserent']) && $row['houserent']=="Own by Family Member") echo "selected"; ?>>Own by Family Member</option>
          <option <?php if(isset($row['houserent']) && $row['houserent']=="Rent House") echo "selected"; ?>>Rent House</option>
          <option <?php if(isset($row['houserent']) && $row['houserent']=="Hostel/Mansion") echo "selected"; ?>>Hostel/Mansion</option>
        </select>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Date</label>
        <input type="date" class="form-control" name="rdate"
               value="<?php echo isset($row['rdate']) ? htmlspecialchars($row['rdate']) : ''; ?>">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Area <font color="red">*</font></label>
        <select class="form-control" name="area">
          <option></option>
          <?php
            $sql11 = mysqli_query($conn, "select * from msarea order by name asc");
            while($row11 = mysqli_fetch_array($sql11)) {
              $selected = (isset($row['area']) && $row['area'] == $row11['name']) ? "selected" : "";
              echo "<option $selected>".$row11['name']."</option>";
            }
          ?>
        </select>
      </div>
    </div>

    <div class="col-lg-4">
      <div class="form-group has-success has-feedback">
        <label>Govt ID <font color="red">*</font></label>
        <input type="text" class="form-control" name="gg1"
               value="<?php echo isset($row['gg1']) ? htmlspecialchars($row['gg1']) : ''; ?>"
               placeholder="Govt ID No">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Attachment 1</label>
        <input type="file" class="form-control" name="upp1">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Govt ID (Alternate)</label>
        <input type="text" class="form-control" name="g2"
               value="<?php echo isset($row['g2']) ? htmlspecialchars($row['g2']) : ''; ?>"
               placeholder="Govt ID No">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Attachment 2</label>
        <input type="file" class="form-control" name="upp2">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Land Mark</label>
        <input type="text" class="form-control" name="lmark"
               value="<?php echo isset($row['lmark']) ? htmlspecialchars($row['lmark']) : ''; ?>">
      </div>
    </div>

    <!-- ================= GUARANTOR DETAILS ================= -->
    <div class="col-lg-12">
      <h3 style="background:#f5f5f5;padding:10px;border-radius:5px;">Guarantor Details</h3>
    </div>

    <div class="col-lg-4">
      <div class="form-group has-success has-feedback">
        <label>Guarantor Name <font color="red">*</font></label>
        <input type="text" class="form-control" name="nname" style="text-transform:uppercase"
               value="<?php echo isset($row['nname']) ? htmlspecialchars($row['nname']) : ''; ?>"
               placeholder="Name">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Guarantor Mobile Number <font color="red">*</font></label>
        <input type="text" class="form-control" name="npno"
               value="<?php echo isset($row['npno']) ? htmlspecialchars($row['npno']) : ''; ?>"
               placeholder="Eg: 9876543210">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Guarantor Address</label>
        <input type="text" class="form-control" name="naddress"
               value="<?php echo isset($row['naddress']) ? htmlspecialchars($row['naddress']) : ''; ?>"
               placeholder="Line 1">
        <input type="text" class="form-control" name="naddress1"
               value="<?php echo isset($row['naddress1']) ? htmlspecialchars($row['naddress1']) : ''; ?>"
               placeholder="Line 2">
        <input type="text" class="form-control" name="naddress2"
               value="<?php echo isset($row['naddress2']) ? htmlspecialchars($row['naddress2']) : ''; ?>"
               placeholder="Line 3 + Pincode">
      </div>
    </div>

    <div class="col-lg-4">
      <div class="form-group has-success has-feedback">
        <label>Guarantor Mobile Number 1 <font color="red">*</font></label>
        <input type="text" class="form-control" name="npno1"
               value="<?php echo isset($row['npno1']) ? htmlspecialchars($row['npno1']) : ''; ?>"
               placeholder="Eg: 9876543210">
      </div>

      <div class="form-group has-success has-feedback">
        <label>Guarantor House Own/Rent</label>
        <select class="form-control" name="nhouserent">
          <option>Select</option>
          <option <?php if(isset($row['nhouserent']) && $row['nhouserent']=="Own House") echo "selected"; ?>>Own House</option>
          <option <?php if(isset($row['nhouserent']) && $row['nhouserent']=="Own by Family Member") echo "selected"; ?>>Own by Family Member</option>
          <option <?php if(isset($row['nhouserent']) && $row['nhouserent']=="Rent House") echo "selected"; ?>>Rent House</option>
          <option <?php if(isset($row['nhouserent']) && $row['nhouserent']=="Hostel/Mansion") echo "selected"; ?>>Hostel/Mansion</option>
        </select>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Guarantor Area <font color="red">*</font></label>
        <select class="form-control" name="narea">
          <option></option>
          <?php
            $sql11 = mysqli_query($conn, "select * from msarea order by name asc");
            while($row11 = mysqli_fetch_array($sql11)) {
              $selected = (isset($row['narea']) && $row['narea'] == $row11['name']) ? "selected" : "";
              echo "<option $selected>".$row11['name']."</option>";
            }
          ?>
        </select>
      </div>

      <div class="form-group has-success has-feedback">
        <label>Guarantor Land Mark</label>
        <input type="text" class="form-control" name="nlmark"
               value="<?php echo isset($row['nlmark']) ? htmlspecialchars($row['nlmark']) : ''; ?>">
      </div>
    </div>

  
    
  
</div>


 <div class="col-lg-12">
    <div class="form-group has-success has-feedback">

      <button type="submit" class="btn btn-success btn-block" name="s1">Save This</button>
    </div></div>
</div>
      <div class="alert alert-success">
	 <?php
							if (isset($_POST['s1'])) {
    extract($_POST);

    // File upload names
    $upp1 = $_FILES['upp1']['name'];
    $upp2 = $_FILES['upp2']['name'];
    $nupp1 = $_FILES['nupp1']['name'];
    $nupp2 = $_FILES['nupp2']['name'];

    // If files are not uploaded, keep old values (when updating)
    if (empty($upp1) && isset($row['upp1'])) $upp1 = $row['upp1'];
    if (empty($upp2) && isset($row['upp2'])) $upp2 = $row['upp2'];
    if (empty($nupp1) && isset($row['nupp1'])) $nupp1 = $row['nupp1'];
    if (empty($nupp2) && isset($row['nupp2'])) $nupp2 = $row['nupp2'];

    // Check if record exists (by member id hpl)
    $check = mysqli_query($conn, "SELECT * FROM mcust WHERE hpl='$hpl'");
    
    if (mysqli_num_rows($check) > 0) {
        // --- UPDATE ---
        $update = mysqli_query($conn, "UPDATE mcust SET 
            mfin='$mfin',
            scode='$scode',
            hplt='$hplt',
            name='$name',
            pno='$pno',
            fname='$fname',
            pnoresi1='$pnoresi1',
            address='$address',
            address1='$address1',
            address2='$address2',
            houserent='$houserent',
            rdate='$rdate',
            area='$area',
            gg1='$gg1',
            upp1='$upp1',
            g2='$g2',
            upp2='$upp2',
            lmark='$lmark',
            nname='$nname',
            npno='$npno',
            naddress='$naddress',
            naddress1='$naddress1',
            naddress2='$naddress2',
            npno1='$npno1',
            nhouserent='$nhouserent',
            narea='$narea',
            nlmark='$nlmark',
            ngg1='$ngg1',
            nupp1='$nupp1',
            ng2='$ng2',
            nupp2='$nupp2'
            WHERE id='$idd'
        ");

        if ($update) {
            // Upload new files if provided
            if (!empty($_FILES['upp1']['tmp_name'])) move_uploaded_file($_FILES['upp1']['tmp_name'], "user/".$upp1);
            if (!empty($_FILES['upp2']['tmp_name'])) move_uploaded_file($_FILES['upp2']['tmp_name'], "user/".$upp2);
            if (!empty($_FILES['nupp1']['tmp_name'])) move_uploaded_file($_FILES['nupp1']['tmp_name'], "user/".$nupp1);
            if (!empty($_FILES['nupp2']['tmp_name'])) move_uploaded_file($_FILES['nupp2']['tmp_name'], "user/".$nupp2);

            echo "<script>alert('Updated successfully');window.location.href='mcust.php'</script>";
        } else {
            echo "Update Failed: " . mysqli_error($conn);
        }

    } else {
        // --- INSERT ---
        $insert = mysqli_query($conn, "INSERT INTO mcust(
            mfin, scode, hpl, hplt, name, pno, fname, pnoresi1, address, address1, address2, 
            houserent, rdate, area, gg1, upp1, g2, upp2, lmark, nname, npno, naddress, naddress1, 
            naddress2, npno1, nhouserent, narea, nlmark, ngg1, nupp1, ng2, nupp2
        ) VALUES (
            '$mfin','$scode','$hpl', '$hplt', '$name', '$pno', '$fname', '$pnoresi1', '$address', 
            '$address1', '$address2','$houserent', '$rdate', '$area', '$gg1', '$upp1', '$g2', '$upp2', 
            '$lmark','$nname', '$npno', '$naddress', '$naddress1', '$naddress2', '$npno1', '$nhouserent',
            '$narea', '$nlmark', '$ngg1', '$nupp1', '$ng2', '$nupp2'
        )");

        if ($insert) {
            move_uploaded_file($_FILES['upp1']['tmp_name'], "user/".$upp1);
            move_uploaded_file($_FILES['upp2']['tmp_name'], "user/".$upp2);
            move_uploaded_file($_FILES['nupp1']['tmp_name'], "user/".$nupp1);
            move_uploaded_file($_FILES['nupp2']['tmp_name'], "user/".$nupp2);

            echo "<script>alert('Register successfully');window.location.href='mcust.php'</script>";
        } else {
            echo "Insert Failed: " . mysqli_error($conn);
        }
    }
}

?>
   </div>
</form>
    </div>
  
  
  
    
</div> 
</div>
</div></div>
</div></div></div><?php include "layouts/footer2.php"; ?>
 <script src="vendors/jquery/dist/jquery.min.js"></script>
    <!-- Bootstrap -->
    <script src="vendors/bootstrap/dist/js/bootstrap.min.js"></script>
    <!-- FastClick -->

    <!-- NProgress -->

    <script src="vendors/jQuery-Smart-Wizard/js/jquery.smartWizard.js"></script>
    <!-- Custom Theme Scripts -->
    <script src="build/js/custom.min.js"></script>
</body></html>