<?php
$tt_page_info = array(
  'Page_Info' =>   array(
    'Title' => 'ঠিকানা সম্পাদনা',
    'Description' => '',
    'Keyword' => '',
  ),
  'Social_og' => array(
    'Image' => '',
    'Type' => 'website',
    'URL' => 'account/address/',
  ),
);
if ($the_writer['UpdateProfile'] == 'active') {
  $noPermissionHtml = '';

  $error = false;
  // Update Information
  if ($_SERVER['REQUEST_METHOD'] === 'POST') {

      // Check if CSRF token is valid
      $csrf_token = $_POST['csrf_token'] ?? '';

      if (!verifyCSRFToken($csrf_token)) {
        $error = true;
      } else {
        $update_info = UpdateData('tt_accounts', 'UserID', $the_writer_id, array(
          "UserPaddress" => htmlentities($_POST['permanentroad'], ENT_QUOTES, 'utf-8'),
          "UserpUnion" => htmlentities($_POST['permanentunion'], ENT_QUOTES, 'utf-8'),
          "UserpUpazila" => htmlentities($_POST['permanentupazilla'], ENT_QUOTES, 'utf-8'),
          "UserpZilla" => htmlentities($_POST['permanentzila'], ENT_QUOTES, 'utf-8'),
          "UserSocial" =>json_encode($_POST['UserSocial']),

          "UserLastUpdate" => $the_time,
          "UserAddress" => htmlentities($_POST['presentroad'], ENT_QUOTES, 'utf-8'),
          "UserZilla" => htmlentities($_POST['presentzilla'], ENT_QUOTES, 'utf-8'),
          "UserUpazila" => htmlentities($_POST['presentupazilla'], ENT_QUOTES, 'utf-8'),
        ));

        if ($update_info) {
          header("Location:".$site_domain.$tt_page_info['Social_og']['URL']);
          exit;
        }else {
          $error = true;
        }
      }
  }

}else {
    $noPermissionHtml = '
    <div class="col-12 mt-4">
      <div class="border-0 rounded-2 alert alert-danger alert-dismissible fade show" role="alert">
        তথ্য পরিবর্তনের অনুমতি নেই !
        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
      </div>
    </div>
    <script type="text/javascript">
    $(document).ready(function() {
      $("#editInfoMahamudfutureit").find("input, select, textarea, button").attr("disabled", true);
    });
    </script>
    ';

}

// Get All District
$get_all_district = showAllttdata("mahamudfutureit_loc_districts");

require_once('./header.php'); // Include Footer
?>

<section id="account_mahamudfutureit" class="sectionbg pt-4 pb-5">
      <div class="container">
          <div class="row mt-4 justify-content-center">
            <div class="col-md-8">
              <div class="acc-info-head round-15 mahamudfutureit-shadow border-0 card p-3 p-md-4">
                  <h1 class="m-0 fs-4"><img height="35px" src="<?php echo $site_domain ?>assets/image/icons/user-application.png"> <?php echo $tt_page_info['Page_Info']['Title'] ?></h1>
                  <hr>
                  <nav style="--s-breadcrumb-divider: '/';" aria-label="breadcrumb">
                      <ol class="breadcrumb m-0">
                        <li class="breadcrumb-item"><a href="<?php echo $site_domain ?>account/">অ্যাকাউন্ট</a></li>
                        <li class="breadcrumb-item"><a href="#"> <?php echo $tt_page_info['Page_Info']['Title'] ?></a></li>
                      </ol>
                  </nav>
              </div>

              <?php
              echo $noPermissionHtml;
              if ($error == true) {
                ?>
                <div class="col-12 mt-4">
                  <div class="border-0 rounded-4 alert alert-danger alert-dismissible fade show" role="alert">
                    সংরক্ষন করা সম্ভব হয়নি ! আবার চেষ্টা করুন ।
                    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
                  </div>
                </div>
                <?php
              }
              ?>

              <div class="personal_info  round-15 mahamudfutureit-shadow border-0 card mt-3 p-3 p-md-4">
                <form id="editInfoMahamudfutureit" action="" method="post" class="row g-3 needs-validation" novalidate>
                  <h5>স্থায়ী ঠিকানা</h5>
                  <div class="row">
                    <div class="col-md-4 mt-3">
                      <div class="form-group">
                        <label for="exampleFormControlInput1" class="form-label">স্থায়ী জেলা </label>
                        <select name="permanentzila" id="permanentZillaID" onchange="changePermanentUpazila()" class="form-select " required>
                            <option value="" selected disabled>জেলা নির্বাচন করুণ</option>
                            <?php
                            foreach ($get_all_district as $this_district) {
                              ?>
                                <option value="<?php echo $this_district['id'] ?>"   <?php echo ($the_writer['UserpZilla'] == $this_district['id']) ? 'selected' : '' ?>><?php echo $this_district['name'].' - '.$this_district['bn_name'] ?></option>
                              <?php
                            }
                            ?>
                        </select>
                      </div>
                    </div>
                    <div class="col-md-4 mt-3">
                      <div id="permanentUpazila" class="form-group">
                        <label for="exampleFormControlInput1" class="form-label">স্থায়ী উপজেলা  </label>
                        <select name="permanentupazilla" id="permanentUpaZillaID" onchange="changePermanentUnion()" class="form-select" required>
                          <?php
                          $user_parmanent_zilla = $the_writer['UserpZilla'];
                          $user_parmamnert_zillas_upazil = showAllttdata("mahamudfutureit_loc_upazilas", "WHERE district_id = '$user_parmanent_zilla'");
                          if (!empty($user_parmamnert_zillas_upazil)) {
                            foreach ($user_parmamnert_zillas_upazil as $user_parmanent_zillas_upazilla) {
                              ?>
                              <option value="<?php echo $user_parmanent_zillas_upazilla['id'] ?>"   <?php echo ($the_writer['UserpUpazila'] == $user_parmanent_zillas_upazilla['id']) ? 'selected' : '' ?>><?php echo $user_parmanent_zillas_upazilla['name'].' - '.$user_parmanent_zillas_upazilla['bn_name'] ?></option>
                              <?php
                            }
                          }else {
                            ?>
                            <option value="" selected disabled>উপজেলা নির্বাচন করুণ</option>
                            <?php
                          }
                          ?>
                        </select>
                      </div>
                    </div>
                    <div class="col-md-4 mt-3">
                      <div id="permanentUnion" class="form-group">
                        <label for="exampleFormControlInput1" class="form-label">স্থায়ী ইউনিয়ন  </label>
                        <select class="form-select " id="parmanentUnionOption" required>
                          <?php
                          $user_parmanent_upazilla = $the_writer['UserpUpazila'];
                          $user_parmamnert_upazilas_union = showAllttdata("mahamudfutureit_loc_unions", "WHERE upazilla_id = '$user_parmanent_upazilla'");
                          if (!empty($user_parmamnert_upazilas_union)) {
                            foreach ($user_parmamnert_upazilas_union as $user_parmanent_upazillas_union) {
                              ?>
                              <option value="<?php echo $user_parmanent_upazillas_union['id'] ?>"   <?php echo ($the_writer['UserpUnion'] == $user_parmanent_upazillas_union['id']) ? 'selected' : '' ?>><?php echo $user_parmanent_upazillas_union['name'].' - '.$user_parmanent_upazillas_union['bn_name'] ?></option>
                              <?php
                            }
                          }else {
                            ?>
                            <option value="" selected disabled>ইউনিয়ন নির্বাচন করুণ</option>
                            <?php
                          }
                          ?>
                        </select>
                      </div>
                    </div>
                    <div class="col-12 mt-3">
                      <label class="form-label" for="exampleFormControlTextarea1">রাস্তা/পাড়া/মহল্লা</label>
                      <textarea name="permanentroad" class="form-control" id="exampleFormControlTextarea1" rows="3"><?php echo $the_writer['UserPaddress']; ?></textarea>
                    </div>
                  </div>
                  <h5>বর্তমান ঠিকানা</h5>
                  <div class="row">
                    <div class="col-md-6 mt-3">
                      <div class="form-group">
                        <label for="exampleFormControlInput1" class="form-label">বর্তমান জেলা </label>
                        <select name="presentzilla" onchange="changePresentUpazilla()" id="presentZilaID" class="form-select " required>
                            <option value="" selected disabled>জেলা নির্বাচন করুণ</option>
                            <?php
                            foreach ($get_all_district as $this_district) {
                              ?>
                                <option value="<?php echo $this_district['id'] ?>" <?php echo ($the_writer['UserZilla'] == $this_district['id']) ? 'selected' : '' ?>><?php echo $this_district['name'].' - '.$this_district['bn_name'] ?></option>
                              <?php
                            }
                            ?>
                        </select>
                      </div>
                    </div>
                    <div class="col-md-6 mt-3">
                      <div id="presentUpazilla" class="form-group">
                        <label for="exampleFormControlInput1" class="form-label">বর্তমান উপজেলা  </label>
                        <select class="form-select " required>
                          <?php
                          $user_present_zilla = $the_writer['UserZilla'];
                          $user_present_zillas_upazila = showAllttdata("mahamudfutureit_loc_upazilas", "WHERE district_id = '$user_present_zilla'");
                          if (!empty($user_present_zillas_upazila)) {
                            foreach ($user_present_zillas_upazila as $user_present_zillas_upazilla) {
                              ?>
                              <option value="<?php echo $user_present_zillas_upazilla['id'] ?>"   <?php echo ($the_writer['UserUpazila'] == $user_present_zillas_upazilla['id']) ? 'selected' : '' ?>><?php echo $user_present_zillas_upazilla['name'].' - '.$user_present_zillas_upazilla['bn_name'] ?></option>
                              <?php
                            }
                          }else {
                            ?>
                            <option value="" selected disabled>উপজেলা নির্বাচন করুণ</option>
                            <?php
                          }
                          ?>
                        </select>
                      </div>
                    </div>
                    <div class="col-12 mt-3">
                      <label class="form-label" for="exampleFormControlTextarea1">রাস্তা/পাড়া/মহল্লা</label>
                      <textarea name="presentroad" class="form-control" id="exampleFormControlTextarea1" rows="3"><?php echo $the_writer['UserAddress']; ?></textarea>
                    </div>
                  </div>
                  <div class="col-12">
                    <hr>
                    <h5>সোশ্যাল মিডিয়া</h5>
                  </div>

                    <?php
                    $user_sms = json_decode($the_writer['UserSocial'], TRUE);
                    foreach ($user_social_medias as $sm_name => $sm_icon) {
                        $link_value = isset($user_sms[$sm_name]) ? $user_sms[$sm_name] : '';
                        ?>
                        <div class="col-6">
                            <div class="form-floating mb-3">
                                <input
                                    name="UserSocial[<?php echo $sm_name ?>]"
                                    type="url"
                                    class="form-control"
                                    id="sm<?php echo $sm_name ?>"
                                    placeholder=""
                                    value="<?php echo htmlspecialchars($link_value); ?>"
                                >
                                <label for="sm<?php echo $sm_name ?>">
                                    <?php echo $sm_name ?>
                                </label>
                            </div>
                        </div>
                        <?php
                    }
                    ?>
                  <div class="col-12 text-center">
                    <?php echo csrfField() ?>
                    <button class="btn btn-primary" type="submit"> সংরক্ষন করুন </button>
                  </div>
                </form>
              </div>
            </div>
          </div>
      </div>
  </section>
<script>
function changePermanentUpazila(){jQuery.ajax({url:"<?php echo $site_domain;?>ajax_master/location/",data:"pZilla="+$("#permanentZillaID").val(),type:"POST",success:function(a){$("#permanentUpazila").html(a)},error:function(){}}),$("#permanentUnionID").empty().append('<option value="" selected disabled>ইউনিয়ন নির্বাচন করুণ</option>')}function changePermanentUnion(){jQuery.ajax({url:"<?php echo $site_domain;?>ajax_master/location/",data:"pUpazilla="+$("#permanentUpaZillaID").val(),type:"POST",success:function(a){$("#permanentUnion").html(a)},error:function(){}})}function changePresentUpazilla(){jQuery.ajax({url:"<?php echo $site_domain;?>ajax_master/location/",data:"presentUpazilla="+$("#presentZilaID").val(),type:"POST",success:function(a){$("#presentUpazilla").html(a)},error:function(){}})}
</script>
<?php
require_once('./footer.php'); // Include Footer
?>
