<?php

use app\libraries\Tenants;

defined('BASEPATH') or exit('No direct script access allowed');

class Product_category_model extends MY_Model {

    use Tenants;

    public $short = "pc";
    public $table;
    public $tbl;
//    public $database;

    public function __construct() {
        parent::__construct();
		$this->database = $this->session->userdata('tenant_no');
		$this->tbl = 'product_category';
        $this->table = $this->tbl . ' ' . $this->short;
    }

    public function detail($id) {
        $this->tenant();

        return $this->db->from('mas_product_category')->where('id', $id)->where('delete_time IS NULL', null, false)->get()->row_array();
    }

    public function detail_product($productId) {
        $this->tenant();

        return $this->db->from('mas_product_category')
                        ->join('mas_product', 'mas_product_category.id = mas_product.product_category_id')
                        ->where('mas_product', $productId)
                        ->get()
                        ->row_array();
    }

    public function totalCount() {
        $db = $this->load->database($this->database, TRUE);
        $db->from($this->table_prefix . $this->table);
		if ($this->database == 'default') {
			$db->where(['tenant_no' => $this->session->userdata('tenant_no')]);
		}
        return $db->count_all_results();
    }

    public function filteredCount($param) {
        $db = $this->load->database($this->database, TRUE);
        $searchVal = isset($param['search']['value']) ? trim($param['search']['value']) : '';
        $search = [];
        if (trim($searchVal) !== "") {
            foreach ($param['columns'] as $column) {
                if ($column['searchable'] === "true") {
                    $search[$this->short . "." . $column['data']] = $searchVal;
                }
            }
        }
		if ($this->database == 'default') {
			$db->where(['tenant_no' => $this->session->userdata('tenant_no')]);
		}
        $db->or_like($search);
        $db->from($this->table_prefix . $this->table);
        return $db->count_all_results();
    }

    public function listing($param) {
        $fields = [$this->short . '.id', $this->short . '.name',$this->short.'.status', $this->short . '.create_time', $this->short . '.update_time'];
        $orders = [];
        foreach ($param['order'] as $order) {
            $colIdx = $order['column'];
            $colName = $param['columns'][$colIdx]['data'];
            $dir = strtoupper($order['dir']);
            $orders[] = $this->short . "." . $colName . '  ' . $dir;
        }
        $start = $param['start'];
        $length = $param['length'];
        $joins = [];
        $searchVal = trim($param['search']['value']);
        $search = [];
        if (trim($searchVal) !== "") {
            foreach ($param['columns'] as $column) {
                if ($column['searchable'] === "true") {
                    $search[$this->short . "." . $column['data']] = $searchVal;
                }
            }
        }
        $this->load->model('Product_model');
        $productCats = $this->Product_model->all([$this->Product_model->short.'.product_category_id']);
        $productCatIds = [];
        foreach($productCats as $v){
            $productCatIds[]=$v['product_category_id'];
        }
//        log_message('debug','productcatids:'.implode(',',$productCatIds));
        $result = $this->getFromDB($this->database, $this->table, $fields, $start, $length, $orders, $search, $joins);
        $data = [];
        foreach ($result as $row) {
            $disabled=in_array($row['id'],$productCatIds)?' disabled':'';
            $data[] = [
                'id' => trim($row['id']),
                'name' => trim($row['name']),
                 'create_time' => date("Y-m-d H:i:s", trim($row['create_time'])),
                'update_time' => date("Y-m-d H:i:s", trim($row['update_time'])),
                'status' => $this->status[$row['status']],
                'details' => '<button id="details-' . trim($row['id']) . '">詳情</button>',
                'delete' => '<input type="checkbox" class="checkbox-delete" id="' . trim($row['id']) . '"'. $disabled.' />',
            ];
        }
        return $data;
    }

    public function insert($param) {
        return $this->insertDB($this->database, $this->tbl, $param);
    }

    public function all($where=[]) {
        $fields = [$this->short . '.id', $this->short . '.name', $this->short . '.status'];
        return $this->getAll($this->database, $this->table, $fields,$where);
    }

    public function one($product_category_id) {        
        $fields = [$this->short . '.id', $this->short . '.name', $this->short . '.status', $this->short . '.remark',"(Select Count(id) From ".$this->database_prefix.$this->database.'.'.$this->table_prefix."product where product_category_id=$product_category_id)  as inuse"];        
        return $this->getOne($this->database, $this->table, $fields, $product_category_id);
    }

    public function save($param, $id) {
        $this->saveDB($this->database, $this->tbl, $param, $id);
    }

    public function deleteMultiple($ids) {
        $this->deleteDB($this->database, $this->tbl, $ids, true);
    }

    public function deleteAll() {
        $this->deleteAllDB($this->database, $this->tbl);
    }
    
    public function getAllIds(){
        $ids = [];
        $all = $this->all();
        foreach($all as $k=>$v){
            $ids[]=$v['id'];
        }
        return $ids;
    }

}
