<?php

namespace App\Traits;
use DB;

trait ReportAble
{
    public static function edarehPatrolData($from_date, $date_to, $province_id): array
    {
        return DB::select(
            'SELECT
            province_fa,
            province_id,
            edare_id,
            edare_name,
            COUNT(*) tedade,
            SUM(tedad_mavarede_moshede_shode_adi) as tedad_mavarede_moshede_shode_adi,
            SUM(tedad_mavarede_moshede_shode_fori) as tedad_mavarede_moshede_shode_fori,
            SUM(tedad_mavarede_moshede_shode_ani) as tedad_mavarede_moshede_shode_ani,
            SUM(tedad_mavarede_peygiri_shode) as tedad_mavarede_peygiri_shode,
            SUM(tedad_faliyat_sabt_shode) as tedad_faliyat_sabt_shode,
            ROUND(SUM(distance)) as masafat_tey_shode
            FROM (SELECT 
            province_id,
            province_fa,
            edare_id,
            edare_name,
            distance,            
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 1) AS tedad_mavarede_moshede_shode_adi,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 2) AS tedad_mavarede_moshede_shode_fori,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 3) AS tedad_mavarede_moshede_shode_ani,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NOT NULL) AS tedad_mavarede_peygiri_shode,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NULL) AS tedad_faliyat_sabt_shode
            FROM `road_patrols` rp where rp.province_id = "'.$province_id.'" AND rp.start_time BETWEEN "'.$from_date.'" AND "'.$date_to.'") temp 
            GROUP BY edare_id
            '
        );
    }

    public static function provincePatrolData($from_date, $date_to) : array {

        return DB::select('SELECT
            "کشوری" as province_fa,
            -1 as province_id,
            COUNT(*) tedade,
            SUM(tedad_mavarede_moshede_shode_adi) as tedad_mavarede_moshede_shode_adi,
            SUM(tedad_mavarede_moshede_shode_fori) as tedad_mavarede_moshede_shode_fori,
            SUM(tedad_mavarede_moshede_shode_ani) as tedad_mavarede_moshede_shode_ani,
            SUM(tedad_mavarede_peygiri_shode) as tedad_mavarede_peygiri_shode,
            SUM(tedad_faliyat_sabt_shode) as tedad_faliyat_sabt_shode,
            ROUND(SUM(distance)) as masafat_tey_shode
            FROM (SELECT 
            province_id,
            province_fa,
            distance,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 1) AS tedad_mavarede_moshede_shode_adi,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 2) AS tedad_mavarede_moshede_shode_fori,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 3) AS tedad_mavarede_moshede_shode_ani,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NOT NULL) AS tedad_mavarede_peygiri_shode,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NULL) AS tedad_faliyat_sabt_shode
            FROM `road_patrols` rp where rp.start_time BETWEEN "'.$from_date.'" AND "'.$date_to.'") temp
            union
            SELECT
            province_fa,
            province_id,
            COUNT(*) tedade,            
            SUM(tedad_mavarede_moshede_shode_adi) as tedad_mavarede_moshede_shode_adi,
            SUM(tedad_mavarede_moshede_shode_fori) as tedad_mavarede_moshede_shode_fori,
            SUM(tedad_mavarede_moshede_shode_ani) as tedad_mavarede_moshede_shode_ani,
            SUM(tedad_mavarede_peygiri_shode) as tedad_mavarede_peygiri_shode,
            SUM(tedad_faliyat_sabt_shode) as tedad_faliyat_sabt_shode,
            ROUND(SUM(distance)) as masafat_tey_shode
            FROM (SELECT 
            province_id,
            province_fa,
            distance,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 1) AS tedad_mavarede_moshede_shode_adi,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 2) AS tedad_mavarede_moshede_shode_fori,
            (SELECT COUNT(*)  FROM `observed_items` oi WHERE rp.`id`= oi.road_patrol_id AND priority = 3) AS tedad_mavarede_moshede_shode_ani,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NOT NULL) AS tedad_mavarede_peygiri_shode,
            (SELECT COUNT(*) FROM `observed_items` oii WHERE rp.`id`= oii.road_patrol_id AND oii.road_item_id IS NOT NULL AND priority IS NULL) AS tedad_faliyat_sabt_shode
            FROM `road_patrols` rp where rp.start_time BETWEEN "'.$from_date.'" AND "'.$date_to.'") temp 
            GROUP BY province_id'
        );

    }
}
