<?php

namespace App\Models;

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

class Banks extends Model
{

    const IS_ACTIVE = 1;
    const IS_INACTIVE = 2;

    const DFT_BANK_ID = 1; // BCA
    const DFT_BANK_CODE = 014;
    const DFT_BANK_NAME = "Bank Central Asia";
    const DFT_BANK_SHORT_NAME = "BCA";
    const DFT_BANK_ACC_NUMBER = 5270860721;
    const DFT_BANK_ACC_NAME = "PT Bonceng Indonesia";

    const defaultSelectedBanks = "
    banks.*
    ";

    public static function listBanks($filter = [])
    {
		$params = [];
		$select = '';
		$join = '';
		$where = '';
		if (isset($filter['is_active'])) {
			$where .= ' AND is_active = ?';
			$params[] = $filter['is_active'];
		}
        return DB::select("SELECT "
		. Banks::defaultSelectedBanks . "
		$select
        FROM t_banks as banks
		$join
        WHERE banks.dlt is null
		$where
		;", $params);
    }

    public static function showBankById($bid)
    {
        return DB::select("SELECT "
            . Banks::defaultSelectedBanks . "
        FROM t_banks as banks
        WHERE banks.dlt is null AND banks.id = ?
        LIMIT 1;", [$bid]);
    }

	public static function showBankByCode($code)
    {
        return DB::select("SELECT "
            . Banks::defaultSelectedBanks . "
        FROM t_banks as banks
        WHERE banks.dlt is null AND banks.bank_code = ?
        LIMIT 1;", [$code]);
    }

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

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

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

    public static function addBank($data)
    {
        $bid = DB::table("t_banks")->insertGetId($data);
        return $bid;
    }

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

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