<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class OrdersPockets extends Model
{

    const defaultSelectedPockets = "
    pm.*
	,pck.name as pck_name,pck.fulladdress as pck_fulladdress
	,drp.name as drop_name,drp.fulladdress as drop_fulladdress
    ";

    public static function listPockets($filter = [])
    {
        $select = '';
        $where = '';
        $params = [];

        if (isset($filter['pck_id'])) {
            $where .= ' AND pm.pck_id = ?';
            $params[] = $filter['pck_id'];
        }
        if (isset($filter['drop_id'])) {
            $where .= ' AND pm.drop_id = ?';
            $params[] = $filter['drop_id'];
        }
        if (isset($filter['is_active'])) {
            $where .= ' AND pm.is_active = ?';
            $params[] = $filter['is_active'];
        }
        if (isset($filter['cptid'])) {
            $where .= ' AND pck.client_group_id = ?';
            $params[] = $filter['cptid'];
            $where .= ' AND drp.client_group_id = ?';
            $params[] = $filter['cptid'];
        }

        return DB::select("SELECT "
        . OrdersPockets::defaultSelectedPockets . "
        $select
        FROM t_orders_pockets as pm
        INNER JOIN t_zones as pck ON pm.pck_id = pck.id
        INNER JOIN t_zones as drp ON pm.drop_id = drp.id
        WHERE pm.dlt is null
        $where
        ;", $params);
    }

    public static function showPocketById($pocket_id)
    {
        return DB::select("SELECT "
        . OrdersPockets::defaultSelectedPockets . "
        FROM t_orders_pockets as pm
        INNER JOIN t_zones as pck ON pm.pck_id = pck.id
        INNER JOIN t_zones as drp ON pm.drop_id = drp.id
        WHERE pm.dlt is null AND pm.id = ?
        LIMIT 1;", [$pocket_id]);
    }

    public static function getPockets()
    {
        return DB::select("SELECT * FROM t_orders_pockets WHERE dlt is null;");
    }

    public static function getPocketById($pocket_id)
    {
        return DB::select("SELECT * FROM t_orders_pockets WHERE dlt is null AND id = ? LIMIT 1;", [$pocket_id]);
    }

    public static function getPocketByCode($code)
    {
        return DB::select("SELECT * FROM t_orders_pockets WHERE dlt is null AND code = ? LIMIT 1;", [$code]);
    }

	public static function getPocketByPckDrop($pck_id, $drop_id)
    {
        return DB::select("SELECT * FROM t_orders_pockets WHERE dlt is null AND pck_id = ? AND drop_id = ? LIMIT 1;", [$pck_id, $drop_id]);
    }

    public static function addPocket($data)
    {
        $pocket_id = DB::table("t_orders_pockets")->insertGetId($data);
        return $pocket_id;
    }

    public static function updatePocket($pocket_id, $data)
    {
        return DB::table("t_orders_pockets")->where("id", $pocket_id)->update($data);
    }

    public static function updtByOrdId($ord_id, $data)
    {
        return DB::table("t_orders_pockets")->where("ord_id", $ord_id)->update($data);
    }

    public static function deletePocket($pocket_id)
    {
        return DB::table("t_orders_pockets")->where("id", $pocket_id)->delete();
    }
}
