<?php
session_start();
$key = md5(date("Y-m-d").":".$_SERVER['REMOTE_ADDR']);
if (isset($_SESSION['aW1iZXN0cmEuY29tLm15']) <> $key) {
    header("location:".$_SERVER['REQUEST_SCHEME']."://".$_SERVER['SERVER_NAME']); // cara baru !
} else {
    include "_header.php";
    include "_navtop.php";
    include "_navside.php";
    include "_functions.php";
    require "conn.php";

    if (isset($_POST['update'])) {
          $user         = mysqli_real_escape_string($con, $_POST['ww']);
          $password     = mysqli_real_escape_string($con, $_POST['xx']);
          $nama         = mysqli_real_escape_string($con, $_POST['aa']);
          $roc          = mysqli_real_escape_string($con, $_POST['bb']);
          $telefon      = mysqli_real_escape_string($con, $_POST['cc']);
          $email        = mysqli_real_escape_string($con, $_POST['dd']);
          $alamat       = mysqli_real_escape_string($con, $_POST['ee']);
          $bandar       = mysqli_real_escape_string($con, $_POST['ff']);
          $poskod       = mysqli_real_escape_string($con, $_POST['gg']);
          $negeri       = mysqli_real_escape_string($con, $_POST['hh']);
          $bank         = mysqli_real_escape_string($con, $_POST['ii']);
          $noakaun      = mysqli_real_escape_string($con, $_POST['jj']);
          $naakaun      = mysqli_real_escape_string($con, $_POST['kk']);
          $bnama        = mysqli_real_escape_string($con, $_POST['ll']);
          $bkp          = mysqli_real_escape_string($con, $_POST['mm']);
          $brelation    = mysqli_real_escape_string($con, $_POST['nn']);
          $id           = mysqli_real_escape_string($con, $_POST['oo']);
          $BPacc_no     = mysqli_real_escape_string($con, $_POST['bp_acc_no']);
          $BPacc_name   = mysqli_real_escape_string($con, $_POST['bp_acc_name']);
          if (isset($_POST['CBcarwash']       )) { $carwash      = 1; }     else    { $carwash      = 0; }
          if (isset($_POST['CBpolish']        )) { $polish       = 1; }     else    { $polish       = 0; }    
          if (isset($_POST['CBrepair']        )) { $repair       = 1; }     else    { $repair       = 0; }    
          if (isset($_POST['CBaccessories']   )) { $accessories  = 1; }     else    { $accessories  = 0; }
          if (isset($_POST['CBinsurance']     )) { $insurance    = 1; }     else    { $insurance    = 0; }
    
    
          if (empty($password)) {
                                    mysqli_query($con, "UPDATE merchant SET nama='$nama', roc='$roc', alamat='$alamat', bandar='$bandar', poskod='$poskod', negeri='$negeri', telefon='$telefon', email='$email', username='$user', bank='$bank', naakaun='$naakaun', noakaun='$noakaun', bnama='$bnama', bkp='$bkp', brelation='$brelation', bp_acc_no ='$BPacc_no', bp_acc_name ='$BPacc_name' , carwash = '$carwash' , polish = '$polish' , repair = '$repair' , accessories = '$accessories' , insurance = '$insurance' WHERE id='".$id."'");
          } else {
                                    mysqli_query($con, "UPDATE merchant SET nama='$nama', roc='$roc', alamat='$alamat', bandar='$bandar', poskod='$poskod', negeri='$negeri', telefon='$telefon', email='$email', username='$user', password='$password', bank='$bank', naakaun='$naakaun', noakaun='$noakaun', bnama='$bnama', bkp='$bkp', brelation='$brelation', bp_acc_no ='$BPacc_no', bp_acc_name ='$BPacc_name' , carwash = '$carwash' , polish = '$polish' , repair = '$repair' , accessories = '$accessories' , insurance = '$insurance' WHERE id='".$id."'");
          }
    }
        

        
        $id = mysqli_real_escape_string($con, $_GET['id']);

        $result = mysqli_query($con, "SELECT * FROM merchant WHERE id='".$id."'");

        $rows = mysqli_fetch_array($result);  
        
        
        if (($rows['carwash'])      == 1)   { $s1 = "text-primary"; $cb1 = "checked";} else { $s1 = "text-secondary"; $cb1 = "";} 
        if (($rows['polish'])       == 1)   { $s2 = "text-primary"; $cb2 = "checked";} else { $s2 = "text-secondary"; $cb2 = "";} 
        if (($rows['repair'])       == 1)   { $s3 = "text-primary"; $cb3 = "checked";} else { $s3 = "text-secondary"; $cb3 = "";} 
        if (($rows['accessories'])  == 1)   { $s4 = "text-primary"; $cb4 = "checked";} else { $s4 = "text-secondary"; $cb4 = "";} 
        if (($rows['insurance'])    == 1)   { $s5 = "text-primary"; $cb5 = "checked";} else { $s5 = "text-secondary"; $cb5 = "";} 
    
?>
        <div id="page-wrapper" >
            <div id="page-inner">
                <div class="row">
                    <div class="col-md-12">
                     <h2>Profile</h2>                          
                    </div>
                </div>
            <hr />

			<div class="row">
                <div class="col-md-12">
                    <!-- Form Elements -->
                    <div class="panel panel-default">
                        <div class="panel-heading">
                            Profile 
                        </div>
                        <div class="panel-body">
                            <div class="row">
                                <form role="form" method="post">
                                <div class="col-md-6">
                                        <div class="text-center align-middle">
                                                <?php
                                                   $PNG_TEMP_DIR = dirname(__FILE__).DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR;
                                                    
                                                    //html PNG location prefix
                                                    $PNG_WEB_DIR = 'temp/';
                                                
                                                    include "qrcodes/qrlib.php";    
                                                    
                                                    //ofcourse we need rights to create temp dir
                                                    if (!file_exists($PNG_TEMP_DIR))
                                                        mkdir($PNG_TEMP_DIR);
                                                    
                                                    
                                                    $filename = $PNG_TEMP_DIR.'test.png';
                                                
                                                    $errorCorrectionLevel = 'H';
                                                
                                                    $matrixPointSize = 10;
                                                         $dat   = "https://vip.wepayyourcost.com/?m=".$rows['id']."";   
                                                        // user data
                                                        $filename = $PNG_TEMP_DIR.$rows['id'].'.png';
                                                        QRcode::png($dat, $filename, $errorCorrectionLevel, $matrixPointSize, 10);   
                                                        
                                                ?>
                                                    
                                                    <img style=" border: 1px solid #555;" width="180px" height="auto" src="<?= $PNG_WEB_DIR.basename($filename); ?>" >
                                                                            
                                        <p>
                                            <a href="paysticker.php?id=<?= $id ?>" class="btn btn-info" role="button" target="_blank">
                                                <i class="fa-solid fa-printer fa-1x"> </i>  Print QRcode Sticker 
                                            </a>  
                                        </p>
                                        </div>
                                        <hr>
                                
                                        <div class="form-group">
                                            <label>Username</label>
                                            <input class="form-control" name="ww" value="<?= $rows['username'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>Password</label>
                                            <input class="form-control" type="password" name="xx" />
                                            <p class="help-block">leave it blank if you want to keep the current password</p>
                                        </div>                                    
                                        <hr>
                                        <h3>Biodata</h3>
                                        <div class="form-group">
                                            <label>Name</label>
                                            <input class="form-control" placeholder="Full Name" name="aa" value="<?= $rows['nama'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>R.O.C</label>
                                            <input class="form-control" name="bb"   value="<?= $rows['roc'] ?>" readonly />
                                        </div>
                                        <div class="form-group">
                                            <label>Phone</label>
                                            <input class="form-control" placeholder="your mobile phone no." name="cc" value="<?= $rows['telefon'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>Email</label>
                                            <input class="form-control" type="email" placeholder="valid email only" name="dd" value="<?= $rows['email'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>Address</label>
                                            <input class="form-control" placeholder="without city, postcode, state" name="ee" value="<?= $rows['alamat'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>City</label>
                                            <input class="form-control" name="ff" value="<?= $rows['bandar'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>Postcode</label>
                                            <input class="form-control" name="gg" value="<?= $rows['poskod'] ?>" />
                                        </div>                                                                                
                                        <div class="form-group">
                                            <label>State</label>
                                            <input class="form-control" name="hh" value="<?= $rows['negeri'] ?>" />
                                        </div>

                                </div>
                                <div class="col-md-6">
                                    <div class="form-group">
                                    <h3>Services</h3>
                                       
                                            <div class="form-group" style="padding:15px; text-align: center;">
                                                <div style="display:inline-block; padding-right:30px;"><input type="checkbox" id="CBcarwash"     name="CBcarwash"      <?= $cb1." ".$dis ?> >       <label for="carwash">       <i class="<?= $s1 ?> fa fa-tint fa-2x"                  title="Carwash"></i></label>    </div>
                                                <div style="display:inline-block; padding-right:30px;"><input type="checkbox" id="CBpolish"      name="CBpolish"       <?= $cb2." ".$dis ?> >       <label for="polish">        <i class="<?= $s2 ?> fa fa-star-half-stroke fa-2x"      title="Polish"></i></label>     </div>
                                                <div style="display:inline-block; padding-right:30px;"><input type="checkbox" id="CBrepair"      name="CBrepair"       <?= $cb3." ".$dis ?> >       <label for="repair">        <i class="<?= $s3 ?> fa fa-wrench fa-2x"                title="Repair"></i></label>     </div>
                                                <div style="display:inline-block; padding-right:30px;"><input type="checkbox" id="CBaccessories" name="CBaccessories"  <?= $cb4." ".$dis ?> >       <label for="accessories">   <i class="<?= $s4 ?> fa fa-magic fa-2x"                 title="Accessories"></i></label></div>
                                                <div style="display:inline-block; padding-right:30px;"><input type="checkbox" id="CBinsurance"   name="CBinsurance"    <?= $cb5." ".$dis ?> >       <label for="insurance">     <i class="<?= $s5 ?> fa fa-shield fa-2x"                title="Insurance"></i></label>  </div>
                                            </div>
                             
                                    </div>
                                    <hr>
                                    <h3><img src="bigpay.png" style="width:100px; height:auto;" /> Details</h3>
                                        <div class="form-group">
                                            
                                        </div>
                                        <div class="form-group">
                                            <label>Account No.</label>
                                            <input class="form-control" placeholder="BigPAY account no." name="bp_acc_no" value="<?= $rows['bp_acc_no'] ?>" />
                                        </div>                                        
                                        <div class="form-group">
                                            <label>Account Name</label>
                                            <input class="form-control" placeholder="BigPAY account name" name="bp_acc_name" value="<?= $rows['bp_acc_name'] ?>" />
                                        </div>
                                    <hr>
                                    <h3>Bank Details</h3>
                                        <div class="form-group">
                                            <label>Bank Name</label>
                                            <input class="form-control" name="ii" value="<?= $rows['bank'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>Account No.</label>
                                            <input class="form-control" placeholder="bank account no." name="jj" value="<?= $rows['noakaun'] ?>" />
                                        </div>                                        
                                        <div class="form-group">
                                            <label>Account Name</label>
                                            <input class="form-control" placeholder="your mobile phone no." name="kk" value="<?= $rows['naakaun'] ?>" />
                                        </div>
                                    <hr>
                                    <h3>Beneficiary</h3>
                                        <div class="form-group">
                                            <label>Full Name</label>
                                            <input class="form-control" placeholder="" name="ll" value="<?= $rows['bnama'] ?>" />
                                        </div>
                                        <div class="form-group">
                                            <label>MyKad</label>
                                            <input class="form-control" placeholder="your beneficiary MyKad no." name="mm" value="<?= $rows['bkp'] ?>" />
                                        </div>                                        
                                        <div class="form-group">
                                            <label>Relationship</label>
<?
$a = $b = $c = $d = "";
$s = " selected";
switch ($rows['brelation']) {

    case "p":
    $a = $s;
    break;

    case "s":
    $b = $s;
    break;

    case "c":
    $c = $s;
    break;

    case "o":
    $d = $s;
    break;

    default:
    $d = $s;
}
?>                                            
                                            <select class="form-control" name="nn">
                                                <option<?= $a ?>>Parent</option>
                                                <option<?= $b ?>>Spouse</option>
                                                <option<?= $c ?>>Children</option>
                                                <option<?= $d ?>>Others</option>
                                            </select>
                                        </div>
                                        <input type="hidden" name="oo" value="<?= $id ?>" />
                                        <button type="submit" name="update" class="btn btn-primary">Update</button>
                                </div>
                                </form>

                                                                
                            </div>
                        </div>
                    </div>
                     <!-- End Form Elements -->
                </div>

			</div>


             <!-- /. PAGE INNER  -->
<? kaki: ?>			 
            </div>
         <!-- /. PAGE WRAPPER  -->
        </div>
<?
    include "_footer.php";
}
?>
