<?php include_once "header.php" ?>

<?php
//if (!isset($conn)) $conn = ew_Connect();

$error = ew_RemoveXSS(ew_StripSlashes($_GET['success']));
if ($error=='1'){
    $success = "<div class=\"alert alert-success alert-dismissable\" id=\"success-alert\"><button type=\"button\" class=\"close\" data-dismiss=\"alert\">x</button>Update Success</div>";
}

if(isset($_POST['btn-submit'])) {
    //die($_POST['inputAddress1'       ]);
    $title     = ew_RemoveXSS(ew_StripSlashes($_POST['inputTitle'          ]));
    /*
    $nric      = ew_RemoveXSS(ew_StripSlashes($_POST['inputNRIC'           ]));
    $nric      = str_replace("-", "", $nric); // Remove instances of hyphen symbol
    $address1  = ew_RemoveXSS(ew_StripSlashes($_POST['inputAddress1'       ]));
    $address2  = ew_RemoveXSS(ew_StripSlashes($_POST['inputAddress2'       ]));
    $address3  = ew_RemoveXSS(ew_StripSlashes($_POST['inputAddress3'       ]));
    $postcode  = ew_RemoveXSS(ew_StripSlashes($_POST['inputPostcode'       ]));
    $city      = ew_RemoveXSS(ew_StripSlashes($_POST['inputCity'           ]));
    $state     = ew_RemoveXSS(ew_StripSlashes($_POST['inputState'          ]));
    */
    
    // INSERT INTO WEBUSER TABLE
    /*
    $stmt = $DB_con->prepare("UPDATE WEBUSERPROFILE
                                 SET  TITLE    =:utitle
                                     ,NRIC     =:unric
                                     ,ADDRESS1 =:uaddr1
                                     ,ADDRESS2 =:uaddr2
                                     ,ADDRESS3 =:uaddr3
                                     ,POSTCODE =:upcode
                                     ,CITY     =:ucity
                                     ,STATE    =:ustate
                                     ,DATEMODIFIED = getdate()                                     
                               WHERE USERID=:uid
                             ");
    */
    $stmt = $DB_con->prepare("UPDATE WEBUSERPROFILE
                                 SET  TITLE    =:utitle
                                     ,DATEMODIFIED = getdate()                                     
                               WHERE USERID=:uid
                             ");                             
    $stmt->bindparam(":uid",    $user_id);
    $stmt->bindparam(":utitle", $title);
    $stmt->execute();
    //$lastid = $stmt->fetch(PDO::FETCH_ASSOC);
    
    $success='<div class="alert alert-success alert-dismissable" id="success-alert"><button type="button" class="close" data-dismiss="alert">x</button>Update Success</div>';
    
    $user->log($user_id, 10, "");
    
} else {
    $user->log($user_id, 9, "");
}

$sql = "SELECT *
          FROM [dbo].[WEBUSERPROFILE]
         WHERE USERID=:uid
       ";
$stmt = $DB_con->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL));
$stmt->bindparam(":uid", $user_id);
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
    $title    = $row['TITLE'    ];
    $fullname = $row['FULLNAME' ];
    $email    = $row['EMAIL'    ];
    /*
    $nric     = $row['NRIC'     ];
    $address1 = $row['ADDRESS1' ];
    $address2 = $row['ADDRESS2' ];
    $address3 = $row['ADDRESS3' ];
    $postcode = $row['POSTCODE' ]; 
    $city     = $row['CITY'     ];
    $state    = $row['STATE'    ];
    */
}

?>

<!-- Full Width Column -->
<div class="content-wrapper">
    <div class="container">
        <!-- Content Header (Page header) -->
        <section class="content-header">
          <h1>
            Profil Pengguna
            <small>Kemas Kini Profil Pengguna</small>
          </h1>
          <ol class="breadcrumb">
            <li><a href="#"><i class="fa fa-dashboard"></i class="active">Laman Utama</a></li>
            <li>Profil</li>
          </ol>
        </section>    
    
        <!-- Main content -->
        <section class="content">

          <div class="row">
            <div class="col-md-3">

              <!-- Profile Image -->
              <div class="box box-primary">
                <div class="box-body box-profile">
                  <img class="profile-user-img img-responsive img-circle" src="dist/img/soldier.png" alt="User profile picture">

                  <h3 class="profile-username text-center"><?php echo $_SESSION['user_name']; ?></h3>

                  <p class="text-muted text-center"><?php echo $user_id; ?></p>

                  <ul class="list-group list-group-unbordered">
                    <li class="list-group-item">
                      <b>Ahli sejak:</b> <a class="pull-right"><?php echo date("d M Y", strtotime($_SESSION['date_created'])); ?></a>
                    </li>
                  </ul>
                  <a href="profile-changepwd.php" class="btn btn-primary btn-block"><b>Tukar Kata Laluan</b></a>
                  <a href="profile-secureword.php" class="btn btn-primary btn-block"><b>Tukar SecureWord</b></a>
                </div>
                <!-- /.box-body -->
              </div>
              <!-- /.box -->
              
            </div>
            <!-- /.col -->
            <div class="col-md-9">
              <div class="nav-tabs-custom">
                <ul class="nav nav-tabs">
                  <li class="active"><a href="#settings" data-toggle="tab">Profil</a></li>
                </ul>
                <div class="tab-content">
                  <div class=" active tab-pane" id="settings">
                    <form class="form-horizontal" id="profile-form" action="<?php echo htmlspecialchars(ew_CurrentPage()) ?>" data-toggle="validator" role="form" method="POST" remote="true">
                      <div id="error">
                            <!-- error will be showen here ! -->  
                        <?php echo $success; ?>                            
                      </div>
                      <div class="form-group">
                        <label for="inputTitle" class="col-sm-3 control-label">Panggilan</label>
                        <div class="col-sm-9">
                          <select name="inputTitle" class="form-control" id="inputTitle">
                            <option value="" selected>Silah Pilih</option>
                            <option value="Tan Sri"    <?php if ($title=="Tan Sri"   ) echo "selected";?>>Tan Sri</option>
                            <option value="Puan Sri"   <?php if ($title=="Puan Sri"  ) echo "selected";?>>Puan Sri</option>
                            <option value="Dato/Datuk" <?php if ($title=="Dato/Datuk") echo "selected";?>>Dato/Datuk</option>
                            <option value="Datin"      <?php if ($title=="Datin"     ) echo "selected";?>>Datin</option>
                            <option value="Tuan"       <?php if ($title=="Tuan"      ) echo "selected";?>>Tuan</option>
                            <option value="Puan"       <?php if ($title=="Puan"      ) echo "selected";?>>Puan</option>
                            <option value="Encik"      <?php if ($title=="Encik"     ) echo "selected";?>>Encik</option>
                            <option value="Cik"        <?php if ($title=="Cik"       ) echo "selected";?>>Cik</option>
                          </select>
                        </div>
                      </div>                    
                      <div class="form-group">
                        <label for="inputName" class="col-sm-3 control-label">Nama</label>

                        <div class="col-sm-9">
                          <input type="text" name="inputName" class="form-control" id="inputName" placeholder="Nama" value="<?php echo $fullname;?>" disabled>
                        </div>
                      </div>
                      <!--
                      <div class="form-group">
                        <label for="inputNRIC" class="col-sm-3 control-label">Kad Pengenalan</label>

                        <div class="col-sm-9">
                          <input type="text`" name="inputNRIC" class="form-control" id="inputNRIC" placeholder="Kad Pengenalan" value="<?php echo $nric;?>" data-mask>
                        </div>
                      </div>
                      -->
                      <div class="form-group">
                        <label for="inputEmail" class="col-sm-3 control-label">Emel</label>

                        <div class="col-sm-9">
                          <input type="email" name="inputEmail" class="form-control" id="inputEmail" placeholder="Emel" value="<?php echo $email;?>" disabled>
                        </div>
                      </div>
                      <!--
                      <div class="form-group">
                        <label for="inputAddress1" class="col-sm-3 control-label">Alamat</label>

                        <div class="col-sm-9">
                          <input type="text" name="inputAddress1" class="form-control" id="inputAddress1" placeholder="Alamat #1" value="<?php echo $address1;?>">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="inputAddress2" class="col-sm-3 control-label"></label>

                        <div class="col-sm-9">
                          <input type="text" name="inputAddress2" class="form-control" id="inputAddress2" placeholder="Alamat #2" value="<?php echo $address2;?>">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="inputAddress3" class="col-sm-3 control-label"></label>

                        <div class="col-sm-9">
                          <input type="text" name="inputAddress3" class="form-control" id="inputAddress3" placeholder="Alamat #3" value="<?php echo $address3;?>">
                        </div>
                      </div>
                      -
                      <div class="form-group">
                        <label for="inputPostcode" class="col-sm-3 control-label">Poskod</label>

                        <div class="col-sm-9">
                          <input type="text" name="inputPostcode" class="form-control" id="inputPostcode" placeholder="Poskod" pattern="[0-9]{5}" data-error="Sila masukkan Postcode yang sah!" value="<?php echo $postcode;?>">
                          <span class="help-block with-errors"></span>
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="inputCity" class="col-sm-3 control-label">Bandar</label>

                        <div class="col-sm-9">
                          <input type="text" name="inputCity" class="form-control" id="inputCity" placeholder="Bandar" value="<?php echo $city;?>">
                        </div>
                      </div>
                      <div class="form-group">
                        <label for="inputState" class="col-sm-3 control-label">Negeri</label>
                        <div class="col-sm-9">
                          <select name="inputState" class="form-control" id="inputState">
                            <option value="" selected>Silah Pilih</option>
                            <option value="Johor"           <?php if ($state=="Johor"          ) echo "selected";?>>Johor</option>
                            <option value="Kedah"           <?php if ($state=="Kedah"          ) echo "selected";?>>Kedah</option>
                            <option value="Kelantan"        <?php if ($state=="Kelantan"       ) echo "selected";?>>Kelantan</option>
                            <option value="Kuala Lumpur"    <?php if ($state=="Kuala Lumpur"   ) echo "selected";?>>Kuala Lumpur</option>
                            <option value="Labuan"          <?php if ($state=="Labuan"         ) echo "selected";?>>Labuan</option>
                            <option value="Melaka"          <?php if ($state=="Melaka"         ) echo "selected";?>>Melaka</option>
                            <option value="Negeri Sembilan" <?php if ($state=="Negeri Sembilan") echo "selected";?>>Negeri Sembilan</option>
                            <option value="Pahang"          <?php if ($state=="Pahang"         ) echo "selected";?>>Pahang</option>
                            <option value="Perak"           <?php if ($state=="Perak"          ) echo "selected";?>>Perak</option>
                            <option value="Perlis"          <?php if ($state=="Perlis"         ) echo "selected";?>>Perlis</option>
                            <option value="Pulau Pinang"    <?php if ($state=="Pulau Pinang"   ) echo "selected";?>>Pulau Pinang</option>
                            <option value="Sabah"           <?php if ($state=="Sabah"          ) echo "selected";?>>Sabah</option>
                            <option value="Sarawak"         <?php if ($state=="Sarawak"        ) echo "selected";?>>Sarawak</option>
                            <option value="Selangor"        <?php if ($state=="Selangor"       ) echo "selected";?>>Selangor</option>
                            <option value="Terengganu"      <?php if ($state=="Terengganu"     ) echo "selected";?>>Terengganu</option>
                          </select>
                        </div>
                      </div>  
                      -->                      
                      <div class="form-group">
                        <div class="col-sm-offset-3 col-sm-9">
                          <button type="submit" name="btn-submit" class="btn btn-danger">Hantar</button>
                          <a href="index.php" class="btn btn-info">Batal</a>
                        </div>
                      </div>
                    </form>
                  </div>
                  <!-- /.tab-pane -->
                </div>
                <!-- /.tab-content -->
              </div>
              <!-- /.nav-tabs-custom -->
            </div>
            <!-- /.col -->
          </div>
          <!-- /.row -->

        </section>
        <!-- /.content -->








    </div>
    <!-- /.container -->
</div>
<!-- /.content-wrapper -->

<?php include_once "footer.php" ?>
<script>
    // close the success message
    $(document).ready (function(){
        $("#success-alert").fadeTo(2000, 1000).slideUp(1000, function(){
            $("#success-alert").alert('close');
        });
        
        // input mask
        $("#inputNRIC").inputmask("999999-99-9999", {removeMaskOnSubmit: true, clearIncomplete: true});  //static mask
        
    });
</script>
