<?php

namespace App\Models;

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

class OrdersCheckpoints extends Model
{
    const defaultSelectedCheckpoints = "
    point.*
    ,point.id as ord_checkpoint_id
	,pck.name as pck_name,pck.fulladdress as pck_fulladdress
	,drp.name as drop_name,drp.fulladdress as drop_fulladdress
    ";

    const IS_UNPAID = 0;
    const IS_PAID = 1;
    const IS_TF_FAIL = 2;

    const TF_METHOD_MANUAL = 1;
    const TF_METHOD_DANA = 2;

    public static function listCheckpoints($filter = [])
    {
		$select = ''; $join = ''; $where = ''; $group_by = ''; $order_by = ''; $limit = '';
		$params = [];

        if (isset($filter['id'])) {
			$where .= ' AND point.id = ?';
			$params[] = $filter['id'];
		}
		if (isset($filter['pocket_id'])) {
			$where .= ' AND point.pocket_id = ?';
			$params[] = $filter['pocket_id'];
		}
        if (isset($filter['ord_pocket_id'])) {
			$where .= ' AND point.ord_pocket_id = ?';
			$params[] = $filter['ord_pocket_id'];
		}
        if (isset($filter['sort'])) {
			$where .= ' AND point.pocket_sort = ?';
			$params[] = $filter['sort'];
		}
        if (isset($filter['ord_id'])) {
			$where .= ' AND point.ord_id = ?';
			$params[] = $filter['ord_id'];
		}
        if (isset($filter['ord_code'])) {
			$where .= ' AND point.ord_code = ?';
			$params[] = $filter['ord_code'];
		}
        if (isset($filter['tf_method'])) {
			$where .= ' AND point.tf_method = ?';
			$params[] = $filter['tf_method'];
		}
        if (isset($filter['where_not_tf_at'])) {
			$where .= ' AND point.tf_at != ?';
			$params[] = $filter['where_not_tf_at'];
		}

        if (isset($filter['order_by'])) {
            $order_by = 'ORDER BY ' . $filter['order_by'];
        }
        if (isset($filter['group_by'])) {
            $group_by = 'GROUP BY ' . $filter['group_by'];
        }
        if (isset($filter['limit'])) {
            $limit = 'LIMIT ' . $filter['limit'];
        }

        return DB::select("SELECT "
        . OrdersCheckpoints::defaultSelectedCheckpoints . "
        $select
        FROM t_orders_checkpoints as point
        INNER JOIN t_zones as pck ON point.pck_id = pck.id
        LEFT JOIN t_zones as drp ON point.drop_id = drp.id
        WHERE point.dlt is null
        $where
        $group_by
		$order_by
		$limit
        ;", $params);
    }

    public static function showCheckpointById($pocket_id)
    {
        return DB::select("SELECT "
        . OrdersCheckpoints::defaultSelectedCheckpoints . "
        FROM t_orders_checkpoints as point
        LEFT JOIN t_zones as pck ON point.pck_id = pck.id
        LEFT JOIN t_zones as drp ON point.drop_id = drp.id
        WHERE point.dlt is null AND point.id = ?
        LIMIT 1;", [$pocket_id]);
    }

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

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

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

    public static function addCheckpoint($data)
    {
        $pid = DB::table("t_orders_checkpoints")->insertGetId($data);
        return $pid;
    }

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

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

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

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

    public static function deleteCheckpointByOrdPocketId($ord_pocket_id)
    {
        return DB::table("t_orders_checkpoints")->where("ord_pocket_id", $ord_pocket_id)->delete();
    }
}
