<?php

namespace App\Models;

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

class OrdersLogsTf extends Model
{
    const defaultSelectedLogsTf = "
    log_tf.*
    ";

	const TYPE_TF_CHECKPOINT = 1;

    const STTS_UNPAID = 0;
    const STTS_PAID = 1;
    const STTS_FAIL = 2;
    const STTS_PENDING = 3;

    const METHOD_MANUAL = 1;
    const METHOD_DANA = 2;

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

        if (isset($filter['ord_id'])) {
			$where .= ' AND log_tf.ord_id = ?';
			$params[] = $filter['ord_id'];
		}
        if (isset($filter['ord_code'])) {
			$where .= ' AND log_tf.ord_code = ?';
			$params[] = $filter['ord_code'];
		}
		if (isset($filter['checkpoint_id'])) {
			$where .= ' AND log_tf.checkpoint_id = ?';
			$params[] = $filter['checkpoint_id'];
		}
		if (isset($filter['type'])) {
			$where .= ' AND log_tf.type = ?';
			$params[] = $filter['type'];
		}
		if (isset($filter['method'])) {
			$where .= ' AND log_tf.method = ?';
			$params[] = $filter['method'];
		}

        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 "
        . OrdersLogsTf::defaultSelectedLogsTf . "
		$select
        FROM t_orders_logs_tf as log_tf
		$join
        WHERE log_tf.dlt is null
		$where
		$group_by
		$order_by
		$limit
		;", $params);
    }

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

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

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

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

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

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