<?php

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

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

class InstitutionController extends Controller
{
    //获取体检机构总列表
    public function GetList()
    {
        $page = request('page');
        $pagesize = request('pageSize');
        $searchInfo = request('searchInfo');
        $list = DB::table('medical_institution as a');
        $list = $list->leftJoin('users as b', 'a.link_user_id', '=', 'b.id');
        $count = $list->count();
        $list = $list->select('a.*', 'b.username');
        $list=$list->orderBy('id', 'desc')->skip(($page-1)*$pagesize)->take($pagesize)->get();

        return \Yz::Return(true, '', ['list' => $list, 'count' => $count]);
    }
    //保存机构设置
    public function SavdSeting()
    {
        $SetingInfo= request('SetingInfo');
        if(isset($SetingInfo['institution_id'])){
            //更新机构表预约状态
            DB::table('medical_institution')->where(['id'=>$SetingInfo['institution_id']])->update([
               'enable_yuyue'=> $SetingInfo['enable_yuyue'],
               'enable_laonianren_mf'=> $SetingInfo['enable_laonianren_mf'],
               'enable_jiankangzheng_mf'=> $SetingInfo['enable_jiankangzheng_mf'],
            ]);
            DB::beginTransaction();
            $saveStatus=false;
            try {
                //查询是否有此机构的设置信息，如果有，先删除再添加（因为设置项有可能会变，所以直接删除再添加）
                $cha=DB::table('institution_seting')->where(['institution_id'=>$SetingInfo['institution_id']])->first();
                if(!!$cha){
                    DB::table('institution_seting')->where(['institution_id'=>$SetingInfo['institution_id']])->delete();
                }
                $i1_count=0;
                $i2_count=0;
                //添加设置
                foreach ($SetingInfo['laonianren_mf']['fenlei'] as $key=>$item){
                   $i1= DB::table('institution_seting')->insert([
                        'institution_id'=>$SetingInfo['institution_id'],
                        'big_check_type'=>"老年人免费体检",
                        'small_check_type'=>$item['name'],
                        'bind_check_type'=>$item['bind_check_type'],
                        'bind_unit_id'=>$item['bind_unit_id'],
                        'bind_group_id'=>$item['bind_group_id'],
                        'bind_batch_id'=>$item['bind_batch_id'],
                       'bind_user_type'=>$item['bind_user_type'],
                        'enable_yuyue'=>$item['enable_yuyue'],
                    ]);
                   if($i1) $i1_count++;
                }

                foreach ($SetingInfo['jiankangzheng_mf']['fenlei'] as $key=>$item){
                    $i2=  DB::table('institution_seting')->insert([
                        'institution_id'=>$SetingInfo['institution_id'],
                        'big_check_type'=>"健康证免费体检",
                        'small_check_type'=>$item['name'],
                        'bind_check_type'=>$item['bind_check_type'],
                        'bind_unit_id'=>$item['bind_unit_id'],
                        'bind_group_id'=>$item['bind_group_id'],
                        'bind_batch_id'=>$item['bind_batch_id'],
                        'bind_user_type'=>$item['bind_user_type'],
                        'enable_yuyue'=>$item['enable_yuyue'],
                    ]);
                    if($i2) $i2_count++;
                }

                if(count($SetingInfo['laonianren_mf']['fenlei'])== $i1_count and count($SetingInfo['jiankangzheng_mf']['fenlei'])== $i2_count){
                    DB::commit();
                    return \Yz::Return(true,'操作完成',[]);
                }else{
                    DB::rollback();
                    return \Yz::Return(false,'操作失败');
                }
            } catch (\Exception $e) {

                DB::rollback();
                return \Yz::Return(false,'操作异常'.$e);
            }



        }else{
            return \Yz::echoError1('参数缺失');
        }
    }
    public function GetSetingDetail()
    {
        $institution_id= request('institution_id');
        $seting=DB::table('institution_seting')->where(['institution_id'=>$institution_id])->get();
        $info=DB::table('medical_institution')->where(['id'=>$institution_id])->get();

        return \Yz::Return(true,'查询成功',['info'=>$info,'seting'=>$seting]);

    }
}
