<?php

namespace App\Models;

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

class PocketMoney extends Model
{

    const IS_INACTIVE = 0;
    const IS_ACTIVE = 1;

	const TYPE_BUNDLE = 1;
	const TYPE_BUNDLE_TEXT = 'Bundle';
    const TYPE_CHECKPOINTS = 2;
	const TYPE_CHECKPOINTS_TEXT = 'Checkpoints';

	const FLOW_DEPARTURE = 1;
	const FLOW_DEPARTURE_TEXT = 'Departure';
    const FLOW_ARRIVAL = 2;
    const FLOW_ARRIVAL_TEXT = 'Arrival';
    const FLOW_HYBRID = 3;
    const FLOW_HYBRID_TEXT = 'Hybpocket_id';

    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 "
        . PocketMoney::defaultSelectedPockets . "
        $select
        FROM t_pocket_money 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 "
        . PocketMoney::defaultSelectedPockets . "
        FROM t_pocket_money 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_pocket_money WHERE dlt is null;");
    }

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

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

	public static function getPocketByPckDrop($pck_id, $drop_id)
    {
        return DB::select("SELECT * FROM t_pocket_money 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_pocket_money")->insertGetId($data);
        return $pocket_id;
    }

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

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

	public static function getTypes()
	{
		$datas = [];
		for ($i=0; $i<2; $i++) {
			$datas[$i] = new \stdClass();
			if ($i === 0) {
				$datas[$i]->id = PocketMoney::TYPE_BUNDLE;
				$datas[$i]->name = PocketMoney::TYPE_BUNDLE_TEXT;
			} else if ($i === 1) {
				$datas[$i]->id = PocketMoney::TYPE_CHECKPOINTS;
				$datas[$i]->name = PocketMoney::TYPE_CHECKPOINTS_TEXT;
			}
		}
		return $datas;
	}

	public static function getFlows()
	{
		$datas = [];
		for ($i=0; $i<3; $i++) {
			$datas[$i] = new \stdClass();
			if ($i === 0) {
				$datas[$i]->id = PocketMoney::FLOW_DEPARTURE;
				$datas[$i]->name = PocketMoney::FLOW_DEPARTURE_TEXT;
			} else if ($i === 1) {
				$datas[$i]->id = PocketMoney::FLOW_ARRIVAL;
				$datas[$i]->name = PocketMoney::FLOW_ARRIVAL_TEXT;
			} else if ($i === 2) {
				$datas[$i]->id = PocketMoney::FLOW_HYBRID;
				$datas[$i]->name = PocketMoney::FLOW_HYBRID_TEXT;
			}
		}
		return $datas;
	}
}
