<?php

namespace App\Http\Controllers\strategy;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\StrategyTask;
use Illuminate\Foundation\Validation\ValidatesRequests;
use App\Http\Controllers\Tool;
use Illuminate\Support\Facades\Validator;

class StrategyTaskController extends Controller
{
    use ValidatesRequests;
    use Tool;
    
    public function index(){
        //搜索分页？
        $pageSize=request()->pageSize;
        $projectArr = $this->getAllowProjects(1);
        //$strategyInfo = StrategyTask::all()->toArray();
        
        $searchArr = [];
        $searchArr['phone'] = request()->phone;
        $searchArr['is_created'] = request()->is_created;
        $searchArr['callTimeStart'] = request()->started_at;
        $searchArr['callTimeStop'] = request()->stoped_at;
        
        $strategyInfo = StrategyTask::where(function($query) use ($searchArr){
            if (isset($searchArr['phone']) && !empty($searchArr['phone'])){
                $query->where('phone','like',"%{$searchArr['phone']}%");
            }
            if (isset($searchArr['is_created']) && !empty($searchArr['is_created'])){
                $query->where('is_created','like',"%{$searchArr['is_created']}%");
            }
            
            if (isset($searchArr['callTimeStart']) && !empty($searchArr['callTimeStart'])){
                $query->whereRaw(" call_date || ' ' || call_time >=  '{$searchArr['callTimeStart']}'");
            }
            
            if (isset($searchArr['callTimeStop']) && !empty($searchArr['callTimeStop'])){
                $query->whereRaw(" call_date || ' ' || call_time <=  '{$searchArr['callTimeStart']}'");
            }
            
        });
        if(empty($pageSize)){
            $strategyInfo = $strategyInfo->orderBy('call_date','desc')->get()->toArray();
        }else{
            $strategyInfo = $strategyInfo->orderBy('call_date','desc')->paginate($pageSize)->toArray();
        }
        
        $listInfo = [];
        foreach ($strategyInfo['data'] as $k=>$v){
            $strategyInfo['data'][$k]['project_name'] = isset($projectArr[$v['project_sn']]) ? $projectArr[$v['project_sn']] : '';
            $strategyInfo['data'][$k]['is_created_name'] = $v['is_created'] == 0 ? '未建' : '已建';
        }
        return $this->successWithInfo($strategyInfo);
    }
    
    
    public function update(Request $request, $id)
    {
        
        $phone = $request->phone;
        $name = $request->name;
        $callTime = $request->call_time;
        
        
        $validator = Validator::make($request->all(), [
            'phone' => 'required|regex:/^1[3-8]{1}[0-9]{9}$/',
            'user_sn' => 'required',
            'call_time' => 'required',
            'name'=>'max:20'
        ]);
        
        if ($validator->fails()) {
            return $this->errorWithInfo('表单验证失败', 401);
        }
        
        $taskInfo = StrategyTask::find($id);
        if ($taskInfo->is_created == 1){
            return $this->errorWithInfo('任务已经创建不能编辑', 401);
        }
        $taskInfo->phone = $phone;
        $taskInfo->name = $name;
        $taskInfo->call_time = $callTime;
        $taskInfo->save();
        return $this->successWithInfo('更新成功');
        
    }
    
    
}
