<?php

use Bookingor\Backend\Controller\Service\ServiceControl;
use Bookingor\Helper\HelperControl;

// Exit if accessed directly
if (! defined('ABSPATH')) exit; ?>
<div class="wrap bookingor">
  <header>
    <div class="bookingor-header">
      <div class="bookingor-flex bookingor-justify-b bookingor-width bookingor-align-items-c p-0-1">
        <div class="bookingor-flex bookingor-align-items-c">
          <div class="bookingor-m-0 p-r up-heading"><?php esc_html_e("Services", "bookingor"); ?></div>
          <?php
          $services = ServiceControl::bp_view_service();
          $service_count = is_array($services) ? count($services) : (is_object($services) ? count((array)$services) : 0);
          $is_pro = class_exists('Bookingor\Backend\Controller\LocationTimeSheet\LocationTimeSheetControl');
          $add_service_url = sanitize_text_field(isset($_SERVER['REQUEST_URI'])) ? esc_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'] . '&type=add-service#basic'))) : '';
          ?>
          <a href="<?php echo esc_url($add_service_url); ?>" class="bookingor-btn bookingor-btn-primary bookingor-flex" id="bk_add_service_btn"
            data-count="<?php echo esc_attr(intval($service_count)); ?>"
            data-is-pro="<?php echo esc_attr($is_pro ? 'true' : 'false'); ?>">
            <span><?php esc_html_e("Add Services", "bookingor"); ?></span>
          </a>
        </div>
      </div>
    </div>
  </header>
  <div class="bookingor">
    <div id="myTable" class="bookingor-admin-bg t-p-all bookingor-d-none">
      <table id="example" class="table table-hover mb-0 table table-bordered">
        <thead class="bookingor-t-head bookingor-t-h-r">
          <tr>
            <th class="bookingor-t-h-f"><?php esc_html_e("ID", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("Services Name", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("category Name", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("Price", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("Duration", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("Status", "bookingor"); ?></th>
            <th class="bookingor-t-h-f"><?php esc_html_e("Action", "bookingor"); ?></th>
            <th class="bookingor-t-h-f none"><?php esc_html_e("Single Service Short Code", "bookingor"); ?></th>

          </tr>
        </thead>
        <tbody>
          <?php global $wpdb;
          foreach (
            ServiceControl::bp_view_service() as $key =>
            $row
          ) { ?>
            <tr class="bookingor-t-row">
              <td class="bookingor-t-f">
                <div><?php echo esc_html($row->service_id); ?></div>
              </td>
              <td class="bookingor-t-f">
                <div class="bookingor-flex align-items-center">
                  <div class="bookingor-table-icon-view">
                    <img
                      class="bookingor-cate-icon"
                      src="<?php echo esc_attr(!empty($row->service_icon) ?   wp_get_attachment_url($row->service_icon) :  ServiceControl::$default_img);  ?> "
                      data-img-id="<?php echo esc_attr(($row->service_icon)); ?>"
                      alt="" />
                  </div>
                  <div><?php echo esc_html($row->service_name); ?></div>
                </div>
              </td>
              <td class="bookingor-t-f"><?php echo esc_html($row->category_name); ?></td>
              <td class="bookingor-t-f">
                <div class="<?php echo esc_attr(get_option('bookingor_currency_position_change')); ?> bookingor-flex">
                  <div class="<?php echo esc_attr(get_option('bookingor_currency_position_change')); ?>"><?php echo esc_html(get_option("bookingor_settings_currency_symbol")); ?></div>
                  <div><?php echo esc_html(HelperControl::priceDecimal($row->service_cost)); ?></div>
                </div>
              </td>
              <td class="bookingor-t-f"><?php echo esc_html(HelperControl::convert_duration($row->service_duration)); ?></td>
              <td class="bookingor-t-f"><?php echo esc_html(($row->status === "1") ? 'Active' : 'Draft'); ?></td>
              <td class="bookingor-t-f">
                <div class="dropdown c-drop">
                  <a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
                    <div class="bkingor-svg">
                      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
                        <path d="M12,7a2,2,0,1,0-2-2A2,2,0,0,0,12,7Zm0,10a2,2,0,1,0,2,2A2,2,0,0,0,12,17Zm0-7a2,2,0,1,0,2,2A2,2,0,0,0,12,10Z"></path>
                      </svg>
                    </div>
                  </a>
                  <div class="dropdown-menu bookingor-dropdown">
                    <ul class="bookingor-list-link">
                      <li>
                        <a
                          href="<?php echo sanitize_text_field(isset($_SERVER['REQUEST_URI'])) ? esc_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'] . '&type=edit-service&id=' . (isset($row->service_id) ? $row->service_id : '') . '#basic'))) : ''; ?>"
                          name="bookingor_service_edit"
                          data-id="<?php echo esc_attr($row->service_id); ?>"
                          class="bookingor-service-view-update"><?php esc_html_e("Edit", "bookingor"); ?></a>
                      </li>
                      <li>
                        <a name="bookingor_service_delete" class="bookingor-delete-service" data-id="<?php echo esc_attr($row->service_id); ?>"><?php esc_html_e("Remove", "bookingor"); ?></a>
                      </li>
                    </ul>
                  </div>
                </div>
              </td>
              <td>
                <?php
                echo esc_html('[BOOKINGOR_DESIGN_6 service=' . $row->service_id . ']');
                // Query assigned staff for this service
                $assigned_staff = $wpdb->get_results($wpdb->prepare(
                  "SELECT sa.staff_get_id, st.staff_first_name, st.staff_last_name
                     FROM {$wpdb->prefix}bookingor_staff_assign_service sa
                     INNER JOIN {$wpdb->prefix}bookingor_staff_add st ON sa.staff_get_id = st.staff_id
                     WHERE sa.service_get_id = %d",
                  $row->service_id
                ));
                if (!empty($assigned_staff)) {
                  foreach ($assigned_staff as $staff) {
                    echo '<br>';
                    echo '<small>' . esc_html($staff->staff_first_name . ' ' . $staff->staff_last_name) . " shortcoode" . ': </small>';
                    echo esc_html('[BOOKINGOR_DESIGN_6 service=' . $row->service_id . ' staff=' . $staff->staff_get_id . ']');
                  }
                }
                ?>
              </td>
            </tr>
          <?php } ?>
        </tbody>
      </table>
    </div>
  </div>
  <div class="mt-2 bookingor-d-flex bookingor-g-5 bookingor-doc-help">
    <a href="https://bookingor.com/documentation/add-services" target="_blank" class=" bookingor-f-13">Documentation</a>
    <div> | </div>
    <a href="https://youtu.be/YpC3ascDukg" target="_blank" class="bookingor-f-13">Video Tutorial</a>
    <div> | </div>
    <a href="https://bookingor.com/contact-support" target="_blank" class="bookingor-f-13">Need Help?</a>
  </div>
  <!-- Pro Plan Modal -->
  <div class="modal fade" id="proServiceModal" tabindex="-1" aria-labelledby="proServiceModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content bkings_pro-model">
        <div class="modal-header flex-column">
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
          <div class="boookingor-popup-text text-center w-100">
            <box-icon name="crown" type="solid" color="#ffb800" size="lg"></box-icon>
            <h3 class="modal-title mt-2" id="proServiceModalLabel"><?php esc_html_e("Upgrade to Pro", "bookingor"); ?></h3>
            <p class="mb-0"><?php esc_html_e("The free version allows only 4 services.", "bookingor"); ?></p>
          </div>
        </div>
        <div class="modal-body px-4 py-4">
          <p class="text-center mb-4"><?php esc_html_e("Upgrade to our Pro plan to add unlimited services and unlock powerful features.", "bookingor"); ?></p>
          <ul class="list-group list-group-flush mb-4 bookingor-pro-list">
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Unlimited Services", "bookingor"); ?></li>
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Advance Diposit (Parital Payment upFront)", "bookingor"); ?></li>
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Service Specific Hours", "bookingor"); ?></li>
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Service Tax", "bookingor"); ?></li>
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Recurring Bookings", "bookingor"); ?></li>
            <li class="list-group-item d-flex align-items-center bookingor-pro-list-item"><?php esc_html_e("Premium Support", "bookingor"); ?></li>
          </ul>
        </div>
        <div class="modal-footer justify-content-center">
          <a href="https://bookingor.com/pricing" target="_blank" class="bookingor-btn bookingor-btn-primary w-100 text-center bookingor-pro-btn"><?php esc_html_e("Buy Pro Plan", "bookingor"); ?></a>
        </div>
      </div>
    </div>
  </div>

</div>
<div class="clear"></div>