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

class Data_surya extends CI_Controller {
	public function __construct() {
        parent::__construct();
    }

	public function get_data_product_type($b_l = ''){
    // kategori bisa dari segmen URL atau parameter GET (fallback)
    if (strlen($b_l) == 0) { $b_l = $this->input->get('b_l'); }
    $b_l = strtoupper(trim((string)$b_l));
    if (!in_array($b_l, array('B', 'L'))) { $b_l = ''; }

    $limit = 20;
    $q = $this->input->get("term");
    $page = $this->input->get("page");
    $parent = $this->input->get('parent');
    $offset = ($page - 1) * $limit;
    $this->db->where('idoutlet', $parent);
    if (strlen($b_l) > 0) {
        $this->db->where('b_l', $b_l);
    }
    $this->db->order_by("product_name");
    if (strlen($q) > 0) {
        $this->db->like("product_name", $q);
    }
    $this->db->select("idproduct as id, product_name as text");
    $this->db->where('status', '1');
    $data = $this->db->get("product", $limit, $offset);
    $this->db->where('idoutlet', $parent);
    if (strlen($b_l) > 0) {
        $this->db->where('b_l', $b_l);
    }
    if (strlen($q) > 0) {
        $this->db->like("product_name", $q);
    }
    $this->db->where('status', '1');
    $cdata = $this->db->get("product");
    $count = $cdata->num_rows();
    $endCount = $offset + $limit;
    $morePages = $endCount < $count;
    $results = array(
        "results" => $data->result_array(),
        "pagination" => array(
            "more" => $morePages
        )
    );
    echo json_encode($results);
}

	public function get_data_hue(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;
		
		$this->db->order_by("hue_name");
		if (strlen($q) > 0) {
			$this->db->like("hue_name", $q);
		}
		$this->db->select("idhue as id, hue_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("hue", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("hue_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("hue");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_colour(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;
		
		$this->db->order_by("colour_name");
		if (strlen($q) > 0) {
			$this->db->like("colour_name", $q);
		}
		$this->db->select("idcolour as id, colour_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("colour", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("colour_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("colour");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_payment(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;
		
		$this->db->order_by("payment_type_name");
		if (strlen($q) > 0) {
			$this->db->like("payment_type_name", $q);
		}
		$this->db->select("idpayment_type as id, payment_type_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("payment_type", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("payment_type_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("payment_type");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

public function get_data_driver(){
	$limit  = 20;
	$q      = $this->input->get("term");
	$page   = $this->input->get("page");
	$offset = ($page - 1) * $limit;

	// =========================================================
	//  FILTER OUTLET  (inti perbaikan)
	//  - Kasir (punya outlet)  -> driver dibatasi ke outlet-nya saja
	//  - Administrator (tanpa outlet) -> tampilkan SEMUA driver
	// =========================================================
	$sess_idoutlet = $this->session->userdata('idoutlet');

	// ---- query 1: hasil yang ditampilkan di dropdown ----
	$this->db->order_by("name");
	if (strlen($q) > 0) {
		$this->db->like("name", $q);
	}
	$this->db->select("iduser as id, name as text");
	$this->db->where('status', '1');
	$this->db->where('idrole', 3);                 // hanya user bertipe driver
	if (strlen($sess_idoutlet) > 0) {
		$this->db->where('idoutlet', $sess_idoutlet);   // <-- KUNCI: samakan outlet
	}
	$data = $this->db->get("user", $limit, $offset);

	// ---- query 2: hitung total (untuk scroll/pagination dropdown) ----
	if (strlen($q) > 0) {
		$this->db->like("name", $q);
	}
	$this->db->where('status', '1');
	$this->db->where('idrole', 3);
	if (strlen($sess_idoutlet) > 0) {
		$this->db->where('idoutlet', $sess_idoutlet);   // <-- samakan juga di sini
	}
	$cdata = $this->db->get("user");

	$count     = $cdata->num_rows();
	$endCount  = $offset + $limit;
	$morePages = $endCount < $count;

	$results = array(
		"results" => $data->result_array(),
		"pagination" => array(
			"more" => $morePages
		)
	);
	echo json_encode($results);
}

	public function get_data_production(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;
		
		$this->db->order_by("name");
		$this->db->where('idrole', 4);
		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->select("iduser as id, name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("user", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->where('status', '1');
		$this->db->where('idrole', 4);
		$cdata = $this->db->get("user");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_cashier(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;
		
		$this->db->order_by("name");
		$this->db->where('idrole', 2);
		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->select("iduser as id, name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("user", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->where('status', '1');
		$this->db->where('idrole', 2);
		$cdata = $this->db->get("user");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

		
}