<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<?php if ( !isset($_GET['user_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_GET['user_nonce'])), 'Copuloo') ) { echo 'Sorry, we couldn’t verify your request due to a security check failure.'; return; } ?>
<div class="content d-flex flex-column flex-column-fluid" id="content">
  <div class="container-xxl" id="content_container">

    <div class="card mb-8">
      <div class="card-body flex-column p-5">
        <div class="d-flex align-items-center h-lg-300px p-5 p-lg-15">
          <div class="d-flex flex-column align-items-start justift-content-center flex-equal me-5">
            <div class="d-flex flex-wrap flex-sm-nowrap">
              <div class="d-flex flex-center flex-shrink-0 bg-light rounded w-100px h-100px w-lg-150px h-lg-150px me-7 mb-4">
                <div class="d-flex justify-content-center align-items-center p-10">
                    <span class="menu-bullet">
                        <span class="fa fa-calendar text-dark display-1"></span>
                    </span>
                </div>
              </div>
              <div class="flex-grow-1">
                <div class="d-flex justify-content-between align-items-start flex-wrap mb-2">
                  <div class="d-flex flex-column">
                    <div class="d-flex align-items-center mb-1">
                      <a class="text-gray-800 fs-2 fw-bold me-3">Timetable</a>
                    </div>
                    <div class="d-flex flex-wrap fw-semibold mb-4 fs-5 text-gray-400">Stay organized and set your own hours to avoid getting called outside of work hours</div>
                  </div>
                </div>
                <div class="d-flex flex-wrap justify-content-start">
                  <div class="d-flex flex-wrap">
                    <ul class="nav nav-custom nav-tabs nav-line-tabs nav-line-tabs-2x fs-6 fw-semibold mt-6 mb-8 gap-2" role="tablist">
                      <li class="nav-item" role="presentation">
                        <a class="nav-link text-active-primary d-flex align-items-center pb-4 active" data-bs-toggle="pill" href="#timeline_widget_2_tab_hours" aria-selected="false" role="tab" tabindex="-1">
                          <i class="fs-5 me-2"><span class="fas fa-calendar"></span></i>Timetable</a>
                        </a>
                      </li>
                      <li class="nav-item" role="presentation">
                        <a class="nav-link text-active-primary d-flex align-items-center pb-4" data-bs-toggle="pill" href="#timeline_widget_2_tab_timezone" aria-selected="false" role="tab" tabindex="-1">
                          <i class="fs-5 me-2"><span class="fas fa-clock"></span></i>Timezone</a>
                        </a>
                      </li>
										</ul>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="card-rounded bg-light d-flex flex-stack flex-wrap p-5">
          <ul class="nav flex-wrap border-transparent fw-bold">
            <li class="nav-item my-1">
              <a class="btn btn-color-gray-600 btn-active-secondary btn-active-color-primary fw-bolder fs-8 fs-lg-base nav-link px-3 px-lg-8 mx-1 text-uppercase" href="<?php echo esc_url(wp_nonce_url($copulooDashboardUrl.'&dashboardpage=contacts', 'Copuloo', 'user_nonce')); ?>">Contacts</a>
            </li>
            <li class="nav-item my-1">
              <a class="btn btn-color-gray-600 btn-active-secondary btn-active-color-primary fw-bolder fs-8 fs-lg-base nav-link px-3 px-lg-8 mx-1 text-uppercase" href="<?php echo esc_url(wp_nonce_url($copulooDashboardUrl.'&dashboardpage=messages', 'Copuloo', 'user_nonce')); ?>">Messages</a>
            </li>
            <li class="nav-item my-1">
              <a class="btn btn-color-gray-600 btn-active-secondary btn-active-color-primary fw-bolder fs-8 fs-lg-base nav-link px-3 px-lg-8 mx-1 text-uppercase active" href="<?php echo esc_url(wp_nonce_url($copulooDashboardUrl.'&dashboardpage=calendar', 'Copuloo', 'user_nonce')); ?>">Timetable</a>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <?php
    $copuloo_table = $wpdb->prefix . 'copuloo_phone_details';
    $copuloo_user_id = get_current_user_id();
    // Direct database query used below – $wpdb->prepare() is always used to prevent SQL injection.
    $copuloo_saveData = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->prefix}copuloo_phone_details WHERE user = %d LIMIT 1", $copuloo_user_id)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
    $copuloo_json_data;
    if (isset($copuloo_saveData[0]->business_hours)) { $copuloo_json_data = json_decode($copuloo_saveData[0]->business_hours); }
    ?>
    <?php if (isset($_GET['updated'])) { echo wp_kses_post(copuloo_main_success("Saved Successfuly", "You have successfully updated your timetable settings.")); }  ?>
    <div class="col-12">
      <div class="card h-xl-100" id="timeline_widget_2_card">
        <div class="card-body">
          <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post" class="form">
            <?php wp_nonce_field( 'Copuloo', 'user_nonce' ); ?>
            <input type="hidden" name="action" value="copuloo_update_router_table"></input>
            <input type="hidden" name="dashboardpage" value="calendar"></input>
            <div class="tab-content">
              <div class="tab-pane fade active show" id="timeline_widget_2_tab_hours" role="tabpanel">
                <div class="card border border-gray-300 border-dashed rounded p-6 mb-6">
                  <div class="card-header border-0 pt-5">
                    <h3 class="card-title align-items-start flex-column">
                      <span class="card-label fw-bold text-gray-900">Set Your Own Hours</span>
                      <span class="text-muted mt-1 fw-semibold fs-7">When a customer calls outside of business hours, a custom message will play, and then the call will automatically hang up.</span>
                    </h3>
                    <div class="card-toolbar"></div>
                  </div>
                  <div class="card-body pt-5">
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Sunday </label> <?php
                      if (isset($copuloo_json_data->sunday_status) && $copuloo_json_data->sunday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="sunday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="sunday_status" type="hidden" name="sunday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->sunday_status) && $copuloo_json_data->sunday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="sunday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="sunday_status" type="hidden" name="sunday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="sunday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="sunday_status" type="hidden" name="sunday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="sunday1" <?php if (isset($copuloo_json_data->sunday_status) && $copuloo_json_data->sunday_status == 0) { echo 'readonly'; } ?> type="time" name="sunday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->sunday1)) { echo esc_html($copuloo_json_data->sunday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="sunday2" <?php if (isset($copuloo_json_data->sunday_status) && $copuloo_json_data->sunday_status == 0) { echo 'readonly'; } ?> type="time" name="sunday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->sunday2)) { echo esc_html($copuloo_json_data->sunday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="separator separator-dashed mt-9 mb-6"></div>
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Monday</label> <?php
                      if (isset($copuloo_json_data->monday_status) && $copuloo_json_data->monday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="monday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="monday_status" type="hidden" name="monday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->monday_status) && $copuloo_json_data->monday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="monday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="monday_status" type="hidden" name="monday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="monday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="monday_status" type="hidden" name="monday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="monday1" <?php if (isset($copuloo_json_data->monday_status) && $copuloo_json_data->monday_status == 0) { echo 'readonly'; } ?> type="time" name="monday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->monday1)) { echo esc_html($copuloo_json_data->monday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="monday2" <?php if (isset($copuloo_json_data->monday_status) && $copuloo_json_data->monday_status == 0) { echo 'readonly'; } ?> type="time" name="monday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->monday2)) { echo esc_html($copuloo_json_data->monday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Tuesday</label> <?php
                      if (isset($copuloo_json_data->tuesday_status) && $copuloo_json_data->tuesday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="tuesday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="tuesday_status" type="hidden" name="tuesday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->tuesday_status) && $copuloo_json_data->tuesday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="tuesday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="tuesday_status" type="hidden" name="tuesday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="tuesday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="tuesday_status" type="hidden" name="tuesday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="tuesday1" <?php if (isset($copuloo_json_data->tuesday_status) && $copuloo_json_data->tuesday_status == 0) { echo 'readonly'; } ?> type="time" name="tuesday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->tuesday1)) { echo esc_html($copuloo_json_data->tuesday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="tuesday2" <?php if (isset($copuloo_json_data->tuesday_status) && $copuloo_json_data->tuesday_status == 0) { echo 'readonly'; } ?> type="time" name="tuesday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->tuesday2)) { echo esc_html($copuloo_json_data->tuesday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Wednesday</label> <?php
                      if (isset($copuloo_json_data->wednesday_status) && $copuloo_json_data->wednesday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="wednesday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="wednesday_status" type="hidden" name="wednesday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->wednesday_status) && $copuloo_json_data->wednesday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="wednesday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="wednesday_status" type="hidden" name="wednesday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="wednesday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="wednesday_status" type="hidden" name="wednesday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="wednesday1" <?php if (isset($copuloo_json_data->wednesday_status) && $copuloo_json_data->wednesday_status == 0) { echo 'readonly'; } ?> type="time" name="wednesday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->wednesday1)) { echo esc_html($copuloo_json_data->wednesday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="wednesday2" <?php if (isset($copuloo_json_data->wednesday_status) && $copuloo_json_data->wednesday_status == 0) { echo 'readonly'; } ?> type="time" name="wednesday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->wednesday2)) { echo esc_html($copuloo_json_data->wednesday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Thursday</label> <?php
                      if (isset($copuloo_json_data->thursday_status) && $copuloo_json_data->thursday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="thursday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="thursday_status" type="hidden" name="thursday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->thursday_status) && $copuloo_json_data->thursday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="thursday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="thursday_status" type="hidden" name="thursday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="thursday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="thursday_status" type="hidden" name="thursday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="thursday1" <?php if (isset($copuloo_json_data->thursday_status) && $copuloo_json_data->thursday_status == 0) { echo 'readonly'; } ?> type="time" name="thursday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->thursday1)) { echo esc_html($copuloo_json_data->thursday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="thursday2" <?php if (isset($copuloo_json_data->thursday_status) && $copuloo_json_data->thursday_status == 0) { echo 'readonly'; } ?> type="time" name="thursday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->thursday2)) { echo esc_html($copuloo_json_data->thursday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="row mb-6">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Friday</label> <?php
                      if (isset($copuloo_json_data->friday_status) && $copuloo_json_data->friday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="friday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="friday_status" type="hidden" name="friday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->friday_status) && $copuloo_json_data->friday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="friday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="friday_status" type="hidden" name="friday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="friday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="friday_status" type="hidden" name="friday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="friday1" <?php if (isset($copuloo_json_data->friday_status) && $copuloo_json_data->friday_status == 0) { echo 'readonly'; } ?> type="time" name="friday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->friday1)) { echo esc_html($copuloo_json_data->friday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="friday2" <?php if (isset($copuloo_json_data->friday_status) && $copuloo_json_data->friday_status == 0) { echo 'readonly'; } ?> type="time" name="friday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->friday2)) { echo esc_html($copuloo_json_data->friday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                    <div class="separator separator-dashed mt-9 mb-6"></div>
                    <div class="row">
                      <label class="col-lg-3 col-form-label fw-semibold fs-6">Saturday</label> <?php
                      if (isset($copuloo_json_data->saturday_status) && $copuloo_json_data->saturday_status == 1)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="saturday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="saturday_status" type="hidden" name="saturday_status" value="1"></input>';
                      }
                      else if (isset($copuloo_json_data->saturday_status) && $copuloo_json_data->saturday_status == 0)
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="saturday" type="button" onclick="changeStatus(this.id);" class="badge badge-danger border-0">Closed</button></div>';
                        echo '<input id="saturday_status" type="hidden" name="saturday_status" value="0"></input>';
                      }
                      else
                      {
                        echo '<div class="col-lg-1 col-form-label"><button id="saturday" type="button" onclick="changeStatus(this.id);" class="badge badge-success border-0">Open</button></div>';
                        echo '<input id="saturday_status" type="hidden" name="saturday_status" value="1"></input>';
                      }
                      ?> <div class="col-lg-8">
                        <div class="row">
                          <div class="col-lg-6 fv-row">
                            <input id="saturday1" <?php if (isset($copuloo_json_data->saturday_status) && $copuloo_json_data->saturday_status == 0) { echo 'readonly'; } ?> type="time" name="saturday1" class="form-control form-control-lg form-control-solid mb-3 mb-lg-0" placeholder="Start Time" value="<?php if (isset($copuloo_json_data->saturday1)) { echo esc_html($copuloo_json_data->saturday1); } ?>" />
                          </div>
                          <div class="col-lg-6 fv-row">
                            <input id="saturday2" <?php if (isset($copuloo_json_data->saturday_status) && $copuloo_json_data->saturday_status == 0) { echo 'readonly'; } ?> type="time" name="saturday2" class="form-control form-control-lg form-control-solid" placeholder="End Time" value="<?php if (isset($copuloo_json_data->saturday2)) { echo esc_html($copuloo_json_data->saturday2); } ?>" />
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="card-footer d-flex justify-content-end py-6 px-9">
                    <button type="submit" class="btn btn-primary" id="account_profile_details_submit">Save Changes</button>
                  </div>
                </div>
              </div>
              <div class="tab-pane fade" id="timeline_widget_2_tab_timezone" role="tabpanel">
                <div class="card border border-gray-300 border-dashed rounded p-6 mb-6">
                  <div class="card-header border-0 pt-5">
                    <h3 class="card-title align-items-start flex-column">
                      <span class="card-label fw-bold text-gray-900">Your Timezone</span>
                      <span class="text-muted mt-1 fw-semibold fs-7">It is important that the below timezone is correct otherwise people may still be able to call you outside of your specified work hours.</span>
                    </h3>
                    <div class="card-toolbar"></div>
                  </div>
                  <div class="card-body pt-5">
                    <div class="row">
                      <label class="col-lg-4 col-form-label fw-semibold fs-6">Timezone</label>
                      <div class="col-lg-8">
                        <div class="row mb-6">
                          <div class="col-lg-12 fv-row">
                            <?php
                            // Escaped inside the copuloo_timezone() function
                            if (isset($copuloo_saveData[0]->timezone)) { echo copuloo_timezone($copuloo_saveData[0]->timezone, 'timezone'); } else { echo copuloo_timezone('none', 'timezone'); } // phpcs:ignore WordPress.Security.EscapeOutput ?>
                          </div>
                        </div>
                      </div>
                    </div>
                  </div>
                  <div class="card-footer d-flex justify-content-end py-6 px-9">
                    <button type="submit" class="btn btn-primary" id="account_profile_details_submit">Save Changes</button>
                  </div>
                </div>
              </div>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
