<?php

namespace App\Http\Controllers\API\H5;

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

class ComboController extends Controller
{
  public function select(Request $request)
  {
    $combo=DB::table('combo_type')->select('id as value','name as label')->get();
    $person=DB::table('combo_crowd')->select('id as value','name as label')->get();
//    $combo = [[
//      'label' => '类型一',
//      'value' => '1',
//    ], [
//      'label' => '类型二',
//      'value' => '2',
//    ]];
//    $person = [[
//      'label' => '类型一',
//      'value' => '1',
//    ], [
//      'label' => '类型二',
//      'value' => '2',
//    ]];
    return \Yz::Return(true, '获取成功', [
      'combo' => $combo,
      'person' => $person
    ]);
  }

  public function list(Request $request)
  {
    $hospital = $request->post('hospital');
    $doctor = $request->post('doctor');
    $openid = $request->post('openid');
   $hospital= DB::table('hospitals')->select('id','name','address','latitude','longitude')->where(['id'=>$hospital])->first();
   $user=DB::table('web_users')->where(['openid'=>$openid,'status'=>1,'is_del'=>0])->first();
   if(!$user) return \Yz::echoError1('openid对应用户不存在');
   $person=DB::table('web_user_person')->where(['user_id'=>$user->id,'is_del'=>0,'is_default'=>1])->first();
   if(!$person) return \Yz::echoError1("请选择就诊人");
   //用户绑定就诊人数量
   $personCount=DB::table('web_user_person')->where(['user_id'=>$user->id,'is_del'=>0])->count();
   $combos=DB::select("select * from combos as a LEFT JOIN (
select combo_id as c_id,count(*) as count from orders where status in(2,4) group by combo_id
) as b on a.combo_id=b.c_id  where a.status=1");

   foreach ($combos as $key=>$combo){
     $tags=json_decode($combo->tags,true);
     $combo->tags2=json_decode($combo->tags2,true);
     foreach ($tags as$k=> $tag){
       $tags[$k]=['text'=>$tag,
         'text_color' => '#47ABD8',
         'color' => '#EBF5FC'
       ];
     }
     $combo->tag=$tags;
     $combo->tag[]=[
       'text' => $combo->item_count.'个项目',
       'text_color' => '#34C292',
       'color' => '#E9F8F3',
     ];
   }
//   $item = [
//      'cover' => '/assets/h5/combo.png',
//      'name' => '中老年体检套餐（女）',
//      'sex' => 2,
//      'combo_type' => '1',
//      'person_type' => '1',
//      'tag' => [[
//        'text' => '女已婚',
//        'text_color' => '#47ABD8',
//        'color' => '#EBF5FC',
//      ], [
//        'text' => '22个项目',
//        'text_color' => '#34C292',
//        'color' => '#E9F8F3',
//      ]],
//      'desc' => ['血糖', '腹部超声', '心电图'],
//      'price' => '1229.00',
//      'original_price' => '2048.88',
//      'count' => '77'
//    ];
//    $list = [];
//    for ($i = 0; $i < 20; $i++) {
//      $list[] = $item;
//    }
//    $hospital_info = [
//      'id' => $hospital,
//      'name' => '秀英院区',
//      'address' => '秀英院区秀英院区秀英院区地址',
//      'latitude' => '39.867671',
//      'longitude' => '119.514223',
//    ];
    $hospital_info=$hospital;
    $info = [
      'name' => $person->name,
      'sex' => $person->sex,
      'count' => $personCount
    ];

    $doctor_info = [
      'id' => $doctor,
      'name' => '张大夫'
    ];

    return \Yz::Return(true, '获取成功', [
      'list' =>$combos,
      'hospital' => $hospital_info,
      'doctor' => $doctor_info,
      'info' => $info,
    ]);
  }

  //获取套餐类型、适用人群
  public function GetComboSort()
  {
    $type=DB::table('combo_type')->get();
    $crowd=DB::table('combo_crowd')->get();
    return \Yz::Return(true,"查询成功",['type'=>$type,'crowd'=>$crowd]);
  }

  //获取购买详情内容（详情页信息）
  public function BuyInfo()
  {
    $hospital_id =request('hospital');
    $combo_id =request('combo_id'); //购买的套餐id
    $item_ids =request('item_ids'); //自选项目ids
    $group_id =request('group_id'); //团检登记id
    if(!isset($hospital_id)) return \Yz::echoError1("医院id不能为空");
    $hospital=DB::table('hospitals')->where(['id'=>1,'status'=>1,'is_del'=>0])->first();
    $combo_info=[];//套餐信息
    if(isset($combo_id)){
   //   $combo=DB::table('combos')->where(['hospital_id'=>$hospital_id,'combo_id'=>$combo_id,'status'=>1])->first();
      $combo=DB::select("select a.*,b.*,c.name as crowd_name from combos as a LEFT JOIN (
select combo_id as c_id,count(*) as sale_count from orders where status in(2,4) group by combo_id
) as b on a.combo_id=b.c_id left join combo_crowd as c on a.crowd_id=c.id where a.combo_id=? and a.status=1 ",[$combo_id]);

      if(!$combo) return \Yz::echoError1("套餐不存在");
      $combo=$combo[0];
      $combo_info['hospital_name']=$hospital->name;
      $combo_info['combo_name']=$combo->name;
      $combo_info['crowd_name']=$combo->crowd_name;
      $combo_info['img']=$combo->cover;
      $combo_info['sale_count']=$combo->sale_count;
      $tags=json_decode($combo->tags,true);
      $combo->tags2=json_decode($combo->tags2,true);
      foreach ($tags as$k=> $tag){
        $tags[$k]=['text'=>$tag,
          'text_color' => '#47ABD8',
          'color' => '#EBF5FC'
        ];
      }
      $combo->tag=$tags;
      $combo->tag[]=[
        'text' => $combo->item_count.'个项目',
        'text_color' => '#34C292',
        'color' => '#E9F8F3',
      ];
      $combo_info['tags']=$combo->tag;
      $combo_info['tags2']=$combo->tags2;
      $combo_info['price']=$combo->price;
      $combo_info['original_price']=$combo->original_price;
      $combo_items=json_decode($combo->items,true);
      $groupedData = [];
      foreach ($combo_items as $item){
        $keshiName = $item['keshi_name'];
        if (!isset($groupedData[$keshiName])) {
          $groupedData[$keshiName] = [];
        }
        $groupedData[$keshiName][] = $item;
      }
      foreach ($groupedData as $keshiName => $children) {
        $combo_info['items'][] = [
          'keshi_name' => $keshiName,
          'children'   => $children
        ];
      }

    }
    $items_info=[];//自选项目信息
    if(isset($item_ids) and !empty($item_ids)){
      $price=0;
       $items=DB::table('items')->whereIn('item_id',$item_ids)->where(['status'=>1])->get();
      $groupedData = [];
      foreach ($items as $item){
        $price=$price+$item->price;
        $keshiName = $item->keshi_name;
        if (!isset($groupedData[$keshiName])) {
          $groupedData[$keshiName] = [];
        }
        $groupedData[$keshiName][] = ['desc'=>$item->jianjie,'name'=>$item->name];
      }
      $items_info['price']=$price;
      foreach ($groupedData as $keshiName => $children) {
        $items_info['items'][] = [
          'keshi_name' => $keshiName,
          'children'   => $children
        ];
      }

    }
    //调用his接口查询用户积分和预存款，计算可以抵扣的金额
    $integral_money = 90;
    $save_money = 150;
    $coupon_money=2;//优惠券数量

    $true_price=$combo_info['price']+$items_info['price']; //用户真实支付价格，应减去抵扣（二期实现）
    $data=[
      'combo_info'=>$combo_info,
      'items_info'=>$items_info,
      'integral_money'=>$integral_money,
      'save_money'=>$save_money,
      'coupon_money'=>$coupon_money,
      'true_price'=>$true_price,//需要用户支付的金的
    ];
    return \Yz::Return(true,"查询成功",$data);
  }

  public function ComboCompare()
  {
    $combo_ids =request('combo_ids'); //购买的套餐id
    $combos=DB::table('combos as a')
      ->leftJoin('checkup_type as b','a.checkup_type_id','=','b.id')
      ->select('a.*','b.name as checkup_type_name')
      ->whereIn('a.combo_id',$combo_ids)->where(['a.status'=>1])->get();
    foreach ($combos as $key =>$combo){
      $combo->items=json_decode($combo->items,true);
     $count= DB::table('orders')->where(['combo_id'=>$combo->combo_id])->whereIn('status',[2,4])->count();
      $combo->saleCount=$count;
    }
    return \Yz::Return(true,"查询完成",['list'=>$combos]);
  }
}
