<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PlanTypeController extends Controller
{
    public function Save()
    {
      $Info =request('Info');
      $params = [
        'name' => isset($Info['name']) ? $Info['name'] : null,
        'is_vip' => isset($Info['is_vip'])  ? $Info['is_vip'] : null,
        'use_type' => isset($Info['use_type']) ? $Info['use_type'] : null,
        'sex' => isset($Info['sex']) ? $Info['sex'] : null,
        'checkup_type_id' => isset($Info['checkup_type_id']) ? json_encode($Info['checkup_type_id']) : null,
        'amount_limit1' => isset($Info['amount_limit1']) ? $Info['amount_limit1'] : 0,
        'amount_limit2' => isset($Info['amount_limit2'])  ? $Info['amount_limit2'] : 0,
        'status'=>isset($Info['status']) ? $Info['status'] : 0,
      ];
      $requiredFields = ['name'=>'名称','is_vip'=>'vip类型','use_type'=>'个检/团检类型','checkup_type_id'=>'体检类型','status'=>'状态'];
// 判断是否为空
      foreach ($requiredFields as $key=> $field) {
        if (!isset($params[$key]) || $params[$key] === null) {
             return \Yz::echoError1('参数 ' . $field . ' 不能为空');
        }
      }
      $do=false;
      $table=DB::table('plan_type');
      if($Info['id']==0){
        $do=$table->insert($params);
      }
      if($Info['id']>0){
        $do=$table->where(['id'=>$Info['id']])->update($params);
      }
      if($do){
        return \Yz::Return(true,'操作成功',[]);
      }else{
        return \Yz::echoError1('操作失败');
      }

    }
    public function GetList()
    {
        $page =request('page');
        $pageSize =request('pageSize');
        $searchInfo=request('searchInfo');
        $list=DB::table('plan_type')->where(['is_del'=>0]);
        if(isset($searchInfo['name'])){
             $list = $list->where('name', 'like','%'.$searchInfo['name'].'%');
        }
        if(isset($searchInfo['status'])){
          $list = $list->where('status',$searchInfo['status']);
        }
        $count=$list->count();
        if(isset($page) and isset($pageSize)){
          $list=$list->orderBy('id', 'desc')->limit($pageSize)->skip(($page - 1) * $pageSize)->take($pageSize);
        }
        $list=$list ->get();
        //匹配检查类型
        $check=DB::table('checkup_type')->where(['is_del'=>0])->get();

        foreach ($list as $key => $l){
          $checkup_type_id=json_decode($l->checkup_type_id,true);
          $CheckTypeName=[];
          foreach($check as $k2=>$l2){
            if(in_array($l2->id,$checkup_type_id)){
              $CheckTypeName[]=$l2->name;
            }
          }
          $l->check_type_name=$CheckTypeName;
        }
        return \Yz::Return(true,'查询完成',['list'=>$list,'count'=>$count]);
    }

    public function GetDetail()
    {
       $id =request('id');
       $info=DB::table('plan_type')->where(['id'=>$id,'is_del'=>0])->first();
       if(!!$info){
         $info->checkup_type_id=json_decode($info->checkup_type_id,true);
         return \Yz::Return(true,'查询完成',$info);
       }else{
         return \Yz::echoError1('查询失败');
       }
    }
    public function Del()
    {
      $id =request('id');
      $d=DB::table('plan_type')->where(['id'=>$id])->update([
        'is_del'=>1
      ]);
      if($d){
        return \Yz::Return(true,'操作完成',[]);
      }else{
        return \Yz::echoError1('操作失败');
      }
    }
}
