<?php
defined('BASEPATH') OR exit('No direct script access allowed');

if (!function_exists('az_barber_ensure_tables')) {
function az_barber_ensure_tables() {
  $ci =& get_instance();
  $ci->db->query("CREATE TABLE IF NOT EXISTS `barber_setting` (
    `idbarber_setting` INT(11) NOT NULL AUTO_INCREMENT,
    `setting_key` VARCHAR(60) NOT NULL,
    `setting_value` VARCHAR(120) NOT NULL DEFAULT '',
    `status` INT(11) NOT NULL DEFAULT 1,
    PRIMARY KEY (`idbarber_setting`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
  $ci->db->query("CREATE TABLE IF NOT EXISTS `barber_libur` (
    `idbarber_libur` INT(11) NOT NULL AUTO_INCREMENT,
    `libur_date` DATE NOT NULL,
    `label` VARCHAR(120) NOT NULL DEFAULT '',
    `status` INT(11) NOT NULL DEFAULT 1,
    PRIMARY KEY (`idbarber_libur`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8");
}
}

if (!function_exists('az_barber_get_setting')) {
function az_barber_get_setting($key, $default = '') {
  $ci =& get_instance();
  $ci->db->where('setting_key', $key);
  $ci->db->where('status', 1);
  $row = $ci->db->get('barber_setting')->row();
  return $row ? $row->setting_value : $default;
}
}

if (!function_exists('az_barber_set_setting')) {
function az_barber_set_setting($key, $value) {
  $ci =& get_instance();
  $ci->db->where('setting_key', $key);
  $ada = $ci->db->get('barber_setting')->row();
  if ($ada) {
    $ci->db->where('setting_key', $key);
    $ci->db->update('barber_setting', array('setting_value' => (string)$value, 'status' => 1));
  } else {
    $ci->db->insert('barber_setting', array('setting_key' => $key, 'setting_value' => (string)$value, 'status' => 1));
  }
}
}

if (!function_exists('az_barber_libur_list')) {
function az_barber_libur_list() {
  $ci =& get_instance();
  az_barber_ensure_tables();
  $ci->db->where('status', 1);
  $ci->db->order_by('libur_date', 'asc');
  return $ci->db->get('barber_libur')->result();
}
}

if (!function_exists('az_barber_libur_map')) {
function az_barber_libur_map() {
  $map = array();
  foreach (az_barber_libur_list() as $r) { $map[$r->libur_date] = $r->label; }
  return $map;
}
}

if (!function_exists('az_barber_hitung_besok')) {
function az_barber_hitung_besok($today = '') {
  if (strlen($today) == 0) $today = date('Y-m-d');
  az_barber_ensure_tables();
  $mode      = az_barber_get_setting('libur_mode', 'auto');
  $minggu    = (int)az_barber_get_setting('minggu_libur', '1');
  $skip_days = max(1, (int)az_barber_get_setting('skip_days', '1'));
  $libur_map = az_barber_libur_map();
  $today_ts  = strtotime($today);

  if ($mode === 'manual') {
    $skip = $skip_days;
  } else {
    $skip = 1;
    for ($d = 1; $d <= 60; $d++) {
      $cand      = date('Y-m-d', strtotime("+$d day", $today_ts));
      $dow       = (int)date('N', strtotime($cand));
      $is_minggu = ($minggu == 1 && $dow == 7);
      $is_libur  = isset($libur_map[$cand]);
      if (!$is_minggu && !$is_libur) { $skip = $d; break; }
      $skip = $d;
    }
  }

  $besok_d = date('Y-m-d', strtotime("+$skip day", $today_ts));

  $rentang = array();
  for ($d = 1; $d < $skip; $d++) { $rentang[] = date('Y-m-d', strtotime("+$d day", $today_ts)); }

  $nama_hari = array('Minggu','Senin','Selasa','Rabu','Kamis','Jumat','Sabtu');
  return array(
    'mode'        => $mode,
    'minggu_libur'=> $minggu,
    'skip_days'   => $skip_days,
    'skip'        => $skip,
    'besok'       => $besok_d,
    'besok_label' => $nama_hari[(int)date('w', strtotime($besok_d))] . ', ' . date('d/m/Y', strtotime($besok_d)),
    'rentang'     => $rentang,
    'libur_map'   => $libur_map,
  );
}
}