<?php

class Cocina extends CI_Controller
{

  public function __construct()
  {
    parent::__construct();
    $this->load->model(modelo(), 'Controlador_model');
    $this->controlador = controlador();
    $this->titulo_controlador = humanize($this->controlador);
    $this->url = base_url() . $this->controlador;
    $this->vista = $this->controlador;
    $this->perfil = $this->session->userdata('perfil') ? $this->session->userdata('perfil') : FALSE;
    $this->usuario = $this->session->userdata('usuario') ? $this->session->userdata('usuario') : FALSE;
    $this->empresa = $this->session->userdata('empresa') ? $this->session->userdata('empresa') : FALSE;
  }

  public function index()
  {
    $data = array(
      'titulo' => $this->titulo_controlador,
      'contenido' => $this->vista,
      'breads' => array(array('ruta' => 'javascript:;', 'titulo' => $this->titulo_controlador))
    );
    $this->load->view(THEME . TEMPLATE, $data);
  }

  public function load_pedido()
  {
    $ventas = $this->db->where('estado', '0')->where('atender', '0')->order_by('hora', 'asc')->where("empresa", $this->empresa)->get('venta');
    $row = '';
    if ($ventas->num_rows() > 0) {

      foreach ($ventas->result() as $hold) {

        $ventasound = $this->db->where('sound', '0')->where('totalitems >', '0')->where('id', $hold->id)->get('venta')->row();

        if ($ventasound) {
          $row .= '<embed src="' . base_url() . 'assets/sonidos/beep-02.wav" type="audio/wav" controller="true" height="0px" loop="true" autostart="true"></embed>';
          $dataventa = ['sound' => '1'];
          $this->Controlador_model->update(['id' => $ventasound->id], $dataventa, 'venta');
        }
        /* ->where('estado', '0') */
        $posales = $this->db->where('venta', $hold->id)->get('ventadetalle');


        if ($hold->atender == '0' && $posales->num_rows() > 0) {
          $empresa = $this->Controlador_model->get($hold->empresa, 'empresa');
          $table = $this->Controlador_model->get($hold->mesa, 'mesa');
          $usuario = $this->Controlador_model->get($hold->usuario, 'usuario');
          $row .=
            '<div class="col-lg-3 col-md-4 col-sm-6 mb-4">
              <div class="card card-warning h-100 shadow-sm">
                <div class="card-header d-flex align-items-center justify-content-between p-2">
                  <div class="flex-grow-1">
                    <h5 class="m-0 font-weight-bold"><i class="fas fa-chair mr-1"></i> ' . ($table ? $table->nombre : "N/A") . '</h5>
                    <small class="text-dark"><i class="fas fa-clock mr-1"></i> ' . ($table ? date('h:i A', strtotime($table->time)) : "N/A") . '</small>
                  </div>
                  <div>
                    <button onclick="preparados(' . $hold->id . ')" type="button" class="btn btn-success btn-sm shadow-sm" title="Despachar Mesa">
                      <i class="fas fa-check-double fa-lg"></i>
                    </button>
                  </div>
                </div>
                <div class="card-body p-0">
                  <div class="px-2 py-1 bg-light border-bottom">
                    <small class="text-muted text-uppercase font-weight-bold"><i class="fas fa-user-tag mr-1"></i> ' . $usuario->nombre . '</small>
                  </div>
                  <div class="table-responsive" style="max-height: 250px; overflow-y: auto;">
                    <table class="table table-sm table-striped m-0">
                      <thead class="bg-secondary text-white small">
                        <tr>
                          <th width="30" class="text-center">#</th>
                          <th>Producto</th>
                          <th width="40" class="text-center">Cant</th>
                          <th width="30"></th>
                        </tr>
                      </thead>
                      <tbody>';
          $i = 1;

          foreach ($posales->result() as $posale) {
            if ($posale->variante != NULL) {
              $textopedido = '<span class="font-weight-bold">' . $posale->nombre . '</span><br><small class="text-orange font-italic">' . ($posale->nombrevariante != "false" ? $posale->nombrevariante : "") . '</small>';
            } else {
              $textopedido = '<span class="font-weight-bold">' . $posale->nombre . '</span>';
            }

            $producto = $this->Controlador_model->get($posale->producto, 'producto');
            $productocategoria = $this->Controlador_model->get($producto->categoria, 'productocategoria');

            $eomsn = trim($posale->opcion . $posale->opciones);
            $opcionx = "";
            if (strlen($eomsn) > 0) {
              $opcionx = '<br><small class="text-primary font-weight-bold">[' . ($posale->opcion != '' ? $posale->opcion . ',' : '') . ($posale->opciones != '' ? $posale->opciones : '') . ']</small>';
            }

            if ($productocategoria->estadococina == '1') {
              $active = $posale->estado ? 'checked disabled' : '';
              $row .= '
              <tr class="' . ($posale->estado ? 'bg-success-light' : '') . '">
                <td class="text-center small align-middle">' . $i . '</td>
                <td class="align-middle small">' . $textopedido . $opcionx . '</td>
                <td class="text-center font-weight-bold align-middle">' . $posale->cantidad . '</td>
                <td class="align-middle text-center">
                  <div class="custom-control custom-checkbox">
                    <input type="checkbox" class="custom-control-input" onclick="preparado(' . $posale->id . ')" id="checkbox' . $posale->id . '" ' . $active . '>
                    <label class="custom-control-label" for="checkbox' . $posale->id . '"></label>
                  </div>
                </td>
              </tr>';
              $i++;
            }
          }
          $row .= '
                      </tbody>
                    </table>
                  </div>
                </div>
              </div>
            </div>';
        }
      }
    }

    echo json_encode($row);
  }

  function preparados($venta)
  {
    $ventaConsult = $this->Controlador_model->get($venta, 'venta');

    $ventadetalle = $this->db->where('estado', '0')->where('venta', $venta)->get('ventadetalle');
    foreach ($ventadetalle->result() as $detalle) {
      $ventadetalleData['estado'] = '1';
      $this->Controlador_model->update(array('id' => $detalle->id), $ventadetalleData, 'ventadetalle');
    }

    $mesaData['estado'] = '3';
    $mesaData['checked'] = date('Y-m-d H:i:s');
    $this->Controlador_model->update(array('id' => $ventaConsult->mesa), $mesaData, 'mesa');

    $ventaData['atender'] = '1';
    $this->Controlador_model->update(array('id' => $venta), $ventaData, 'venta');
    echo json_encode(['status' => TRUE]);
  }

  public function preparado($detalle)
  {
    //$detalles = $this->Controlador_model->get($detalle, 'ventadetalle');
    //$ventas = $this->Controlador_model->get($detalles->venta, 'venta');
    //$ventadetalle = $this->db->where('estado', '0')->where('venta', $detalles->venta)->from('ventadetalle')->count_all_results();
    /* if ($ventadetalle == 1) {
      $mesa['estado'] = '3';
      $mesa['checked'] = date('Y-m-d H:i:s');
      $this->Controlador_model->update(array('id' => $ventas->mesa), $mesa, 'mesa');
      $venta['atender'] = '1';
      $this->Controlador_model->update(array('id' => $detalles->venta), $venta, 'venta');
    } */
    $data['estado'] = '1';
    $this->Controlador_model->update(array('id' => $detalle), $data, 'ventadetalle');
  }
}
