<?php

namespace App\Traits;

use DB;

trait SafetyAndPrivacyReportAble
{
    public static function provinceData($from_date = null, $date_to = null): array
    {
        if ($from_date && $date_to) {
            $from_date = $from_date.' 00:00:00';
            $date_to = $date_to.' 23:59:59';
        } else {
            $from_date = Date('Y-m-d').' 00:00:00';
            $date_to = Date('Y-m-d').' 23:59:59';
        }


        
        $country_name = "کشوری";
        $all[0]["info-89"]["step-1"] = 0;
        $all[0]["info-89"]["step-2"] = 0;
        $all[0]["info-89"]["step-3"] = 0;
        
        $all[0]["info-90"]["step-1"] = 0;
        $all[0]["info-90"]["step-2"] = 0;
        $all[0]["info-90"]["step-3"] = 0;

        $all[0]["info-91"]["step-1"] = 0;
        $all[0]["info-91"]["step-2"] = 0;
        $all[0]["info-91"]["step-3"] = 0;
        
        $all[0]["sum"] = 0;
        $all[0]["name_fa"] = $country_name;
        $all[0]["id"] = -1;
        
        foreach (\App\Models\Province::all() as $key => $value) {
            $all[$value->id]["info-89"]["step-1"] = 0;
            $all[$value->id]["info-89"]["step-2"] = 0;
            $all[$value->id]["info-89"]["step-3"] = 0;

            $all[$value->id]["info-90"]["step-1"] = 0;
            $all[$value->id]["info-90"]["step-2"] = 0;
            $all[$value->id]["info-90"]["step-3"] = 0;

            $all[$value->id]["info-91"]["step-1"] = 0;
            $all[$value->id]["info-91"]["step-2"] = 0;
            $all[$value->id]["info-91"]["step-3"] = 0;
            
            $all[$value->id]["sum"] = 0;
            $all[$value->id]["name_fa"] = $value->name_fa;
            $all[$value->id]["id"] = $value->id;
        }

        $temp = DB::select("SELECT COUNT(*) as qty,info_id, step,step_fa, info_fa FROM safety_and_privacy where created_at BETWEEN '".$from_date."' and '".$date_to."' GROUP BY info_id,step");

        $sum = 0;
        foreach ($temp as $key => $value) {
            $sum += $value->qty;
            $all[0]["info-".$value->info_id]["step-".$value->step] = $value->qty;
            $all[0]["sum"] = $sum;
        }

        $temp = DB::select("SELECT province_id, COUNT(*) as qty,info_id, step,step_fa, info_fa FROM safety_and_privacy where created_at BETWEEN '".$from_date."' and '".$date_to."' GROUP BY info_id,step,province_id order by province_id");
        $province_id_flag = "";
        $sum = 0;

        foreach ($temp as $key => $value) {

            if ($province_id_flag !== $value->province_id) {

                $province_id_flag = $value->province_id;
                $sum = $value->qty ;

            }else {

                $sum += $value->qty;

            }

            $all[$value->province_id]["info-".$value->info_id]["step-".$value->step] = $value->qty;
            $all[$value->province_id]["sum"] = $sum;
        }
        return $all;
    }

    public static function EdareData($from_date = null, $date_to = null, $province_id) : array {

        if ($from_date && $date_to) {
            $from_date = $from_date.' 00:00:00';
            $date_to = $date_to.' 23:59:59';
        } else {
            $from_date = Date('Y-m-d').' 00:00:00';
            $date_to = Date('Y-m-d').' 23:59:59';
        }


        $all = [];
        foreach (\App\Models\EdarateShahri::where('province_id', $province_id)->get() as $key => $value) {
            $all[$value->id]["info-89"]["step-1"] = 0;
            $all[$value->id]["info-89"]["step-2"] = 0;
            $all[$value->id]["info-89"]["step-3"] = 0;

            $all[$value->id]["info-90"]["step-1"] = 0;
            $all[$value->id]["info-90"]["step-2"] = 0;
            $all[$value->id]["info-90"]["step-3"] = 0;

            $all[$value->id]["info-91"]["step-1"] = 0;
            $all[$value->id]["info-91"]["step-2"] = 0;
            $all[$value->id]["info-91"]["step-3"] = 0;
            
            $all[$value->id]["sum"] = 0;
            $all[$value->id]["name_fa"] = $value->name_fa;
            $all[$value->id]["id"] = $value->id;
        }


        $temp = DB::select('SELECT edare_shahri_id, COUNT(*) as qty,info_id, step,step_fa, info_fa FROM safety_and_privacy where province_id ='.$province_id.' and created_at BETWEEN "'.$from_date.'" and "'.$date_to.'" GROUP BY info_id,step,edare_shahri_id order by edare_shahri_id');
        $edare_shahri_id_flag = "";
        $sum = 0;

        foreach ($temp as $key => $value) {

            if ($edare_shahri_id_flag !== $value->edare_shahri_id) {

                $edare_shahri_id_flag = $value->edare_shahri_id;
                $sum = $value->qty ;

            }else {

                $sum += $value->qty;

            }

            $all[$value->edare_shahri_id]["info-".$value->info_id]["step-".$value->step] = $value->qty;
            $all[$value->edare_shahri_id]["sum"] = $sum;
        }

        return $all;
    }

}
