<?php

namespace App\Http\Controllers;

use App\Models\Admin;
use App\Models\project;
use App\Models\User;
use App\Models\project_info;
use App\Models\projectWork;
use Illuminate\Http\Request;
use App\Classes\CalendarUtils;

class projectcontroller extends Controller
{

    public function project()
    {
        $projects = project::all();

        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.projects.project',compact('projects','Admin'));
            }
        }
        return view('content.project.projects.project',compact('projects'));
    }
    public function addproject()
    {
        $users = User::all();
        return view('content.project.projects.addProject',compact('users'));
    }
    public function editProject($project_id)
    {
        
        $users = User::all();
        $projects = project::where('id', $project_id)->first();
        return view('content.project.projects.editProject',compact('projects','users'));
    }
    public function saveEditedProject(Request $request,$project_id)
    {
        $project = project::where('id', $project_id)->first();
        $project->maker  = $request->admin  ;
        $project->title  = $request->title ;
        $project->body   = $request->body  ;
        $project->status   = $request->status  ;
        if ($request->user != ''){
            $projectU = explode('|-|',$request->user);
            $project->user_id   = $projectU[0]  ;
            $project->user   = $projectU[1]  ;
        }
        $project->save();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                $projects = project::all();
                return view('content.project.projects.project',compact('projects','Admin'));
            }
        }

    }
    public function saveproject(Request $request)
    {
        $project = new project();
        $project->maker  = $request->admin  ;
        $project->title  = $request->title ;
        $project->body   = $request->body  ;
        $project->status   = $request->status  ;
        if ($request->user != ''){
            $projectU = explode('|-|',$request->user);
            $project->user_id   = $projectU[0]  ;
            $project->user   = $projectU[1]  ;
        }
        $project->save();
        $projects = project::all();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.projects.project',compact('projects','Admin'));
            }
        }
        return view('content.project.projects.project',compact('projects'));
    }
    /*   !!!!!!!!!!!!!!!       Project_infos  !!!!!!!!!!!!!!!                 */

    public function projectInfo()
    {
        return 'این بخش فعال نشده';
        error_reporting(5);
        $project_infos = project_info::all();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_infos.infoProject',compact('project_infos','Admin'));
            }
        }
        return view('content.project.project_infos.infoProject',compact('project_infos'));
    }

    public function editProjectInfos($project_id)
    {

        $Admins = Admin::all();
        $projects = project_info::where('id', $project_id)->first();
        return view('content.project.project_infos.editinfoProject',compact('projects','Admins'));
    }

    public function saveEditedInfoProject(Request $request,$project_infos_id)
    {
        $project = project_info::where('id', $project_infos_id)->first();
        $project->admin_id  = $request->admin_id  ;
        $project->title  = $request->title ;
        $project->d1  = $request->d1 ;
        $project->d3  = $request->d3 ;
        $project->body   = $request->body  ;
        $project->status   = $request->status  ;
        $project->save();

        $project = project::where('id', $project->project_id)->first();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_infos.infoProjectByProject',compact('project','Admin'));
            }
        }
    }
    public function projectInfoByProject($project_id)
    {

        error_reporting(9);
        $project = project::where('id', $project_id)->first();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_infos.infoProjectByProject',compact('project','Admin'));
            }
        }

    }
    public function saveProject_infos(Request $request,$project_id)
    {
        error_reporting(9);
        $timechenger	= new CalendarUtils();
//        $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
//        $english = [ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ];
//        $d1Y = str_replace($persian,$english ,$request->d1Y);
//        $d1m = str_replace($persian,$english ,$request->d1m);
//        $d1d = str_replace($persian,$english ,$request->d1d);
//        $d3Y = str_replace($persian,$english ,$request->d3Y);
//        $d3m = str_replace($persian,$english ,$request->d3m);
//        $d3d = str_replace($persian,$english ,$request->d3d);
        $date1 = explode('/',$request->d1);
        $date3 = explode('/',$request->d3);

        $d1=$timechenger->toGregorian($date1[0],$date1[1],$date1[2]);
        $d3=$timechenger->toGregorian($date3[0],$date3[1],$date3[2]);

        $project = project::where('id', $project_id)->first();

        $project0 = new project_info();
        $project0->title  = $request->title ;
        $project0->body   = $request->body  ;
        $project0->d1    = $d1[0].'-'.$d1[1].'-'.$d1[2] ;
        $project0->d3    = $d3[0].'-'.$d3[1].'-'.$d3[2] ;
        if ($request->admin_id){
            $project1 = explode('|-|',$request->admin_id);
            $project0->admin_id   = $project1[0]  ;
            $project0->admin   = $project1[1]  ;
        }
        $project0->project_id   = $project_id;
        $project0->project   = $project->title;
        $project0->status   = $request->status  ;
        $project0->save();

        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_infos.infoProjectByProject',compact('project','Admin'));
            }
        }

    }
//    public function addproject_infos_list()
//    {
//        $projects = project_info::all();
//        return view('content.project.project_infos.infoProject',compact('projects'));
//    }
    public function addproject_infos($project_id)
    {
        $Admins = Admin::all();
        $projects = project::where('id', $project_id)->get();
        return view('content.project.project_infos.addInfoProject',compact('projects','Admins'));
    }





    /*projectWork*/
    public function addproject_works($project_id)
    {
        return view('content.project.project_works.addWorks');
    }

    public function project_worksByProjectInfo($project_infos_id)
    {
        $projectInfo = project_info::where('id', $project_infos_id)->first();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_works.project_works',compact('projectInfo','Admin'));
            }
        }
        return view('content.project.project_works.project_works',compact('projectInfo'));
    }

    public function project_workActions($project_infos_id,$workId)
    {
        $url = explode('/',$_SERVER['REQUEST_URI']);
        if ($url[3] == 'done'){
            $doneAction = projectWork::where('id', $workId)->first();
            if ($doneAction->done == 0){
                $doneAction->done = 1;$doneAction->save();
                $projectInfo = project_info::where('id', $project_infos_id)->first();
                $projectInfo->doneWorks ++;
                $projectInfo->save();

            }else{
                $doneAction->done = 0;$doneAction->save();
                $projectInfo = project_info::where('id', $project_infos_id)->first();
                $projectInfo->doneWorks --;
                $projectInfo->save();
            }

        }else{
            projectWork::where('id', $workId)->delete();
            $projectInfo = project_info::where('id', $project_infos_id)->first();
            $projectInfo->works --;
            $projectInfo->save();
        }


        $projectInfo = project_info::where('id', $project_infos_id)->first();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_works.project_works',compact('projectInfo','Admin'));
            }
        }
        return view('content.project.project_works.project_works',compact('projectInfo'));
    }
    public function saveproject_works(Request $request,$project_infos_id)
    {
        $titles = explode('<p>',$request->titles);
        $length = count($titles);
        $length =  $length -2;
        $cunter = 0;
        $cunterI = 0;
        foreach($titles as $title){

            if ($cunter == 1 and count($titles)!= $cunter){
                if ($length == $cunterI){

                    $project_works = new projectWork();
                    $project_works->title  =  substr($title,0,-4);
                    $project_works->done  =  0;
                    $project_works->project_info_id  =  $project_infos_id;
                    $project_works->save();
                    $cunterI++;
                }else{

                    $project_works = new projectWork();
                    $project_works->title  =  substr($title,0,-6);
                    $project_works->done  =  0;
                    $project_works->project_info_id  =  $project_infos_id;
                    $project_works->save();
                    $cunterI++;
                }

            }
            else{$cunter++;}
        }

        $projectInfo = project_info::where('id', $project_infos_id)->first();
        $projectInfo->works  += $cunterI ;
        $projectInfo->save();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_works.project_works',compact('projectInfo','Admin'));
            }
        }
        return view('content.project.project_works.project_works',compact('projectInfo'));
    }
    public function saveProject_works_excel(Request $request,$project_infos_id)
    {
        $titles = explode('<br />',$request->titles);

        $cunter = 0;
        $cunterI = 0;

        foreach($titles as $title){
            $project_works = new projectWork();
            $project_works->title  =  $title;
            $project_works->done  =  0;
            $project_works->project_info_id  =  $project_infos_id;
            $cunterI++;
            $project_works->save();


        }

        $projectInfo = project_info::where('id', $project_infos_id)->first();
        $projectInfo->works  += $cunterI ;
        $projectInfo->save();
        if ($Admin = Admin::where('name', $_COOKIE['admin'])->first()){
            if ($Admin->remember_token == $_COOKIE['token']){
                return view('content.project.project_works.project_works',compact('projectInfo','Admin'));
            }
        }
        return view('content.project.project_works.project_works',compact('projectInfo'));
    }
}
