<?php
namespace App\Http\Controllers\DatasBase;
use App\Http\Controllers\BaseController;
use App\Services\BmmcService;
use BlueCity\Core\Inc\ErrorInc;
use Illuminate\Http\Request;
use DB;

/**
 * @name 自动派单区域管理
 * Class DisareaController
 * @package App\Http\Controllers\Admin
 */
class DisareaController extends BaseController
{
    //编辑展示
    /**
     * @name 编辑展示
     * @param are_state 选中状态 1.选中；2：未选中；3：部分选中
     * @param Request $request
     * @return mixed
     * @author du
     * @date 2021/9/1
     */
    public function index (Request $request)
    {
        //dd(1);
        //$a = DB::table('disareanew')->get();
        //dd($a);
        //$file = public_path().'/storage/exports/excel/20190604161635商品数据.xls';
        //$file = 'http://mo2o9.sudianwang.com/public/storage/exports/excel/20190604161635.xls';
        //return response()->download($file,'qqq.xls');

        //$res = DB::table('disareanew')->get();
        //$list=M("sdceshi_bluezp.disareanew")->where()->order("are_shid asc,are_sid asc,are_xid asc")->select();
        $list = DB::table('disareanew')->orderBy('are_shid')->orderBy('are_sid')->orderBy('are_xid')->get();

        if (!$list->isEmpty()){
            $list = $list->toArray();
            $shiarray = array();
            $shengarray = array();
            foreach( $list as $k => $v )
            {
                $xdata['are_id'] = $v['are_xid'];
                $xdata['are_name'] = $v['are_xian'];
                $xdata['are_state'] = $v['are_state'];
                $sdata['are_id'] = $v['are_sid'];
                $sdata['are_name'] = $v['are_shi'];
                $sdata['are_state'] = '2';
                $hdata['are_id'] = $v['are_shid'];
                $hdata['are_name'] = $v['are_sheng'];
                $hdata['are_state'] = '2';
                $xian[$v['are_sid']][] = $xdata;
                if(!in_array($v['are_sid'],$shiarray)){
                    $shiarray[] = $v['are_sid'];
                    $shi[$v['are_shid']][] = $sdata;
                }if(!in_array($v['are_shid'],$shengarray)){
                $shengarray[] = $v['are_shid'];
                $sheng[] = $hdata;
            }
                unset($list[$k]);
            }
            foreach( $shi as $k => $v ){
                foreach( $v as $ka=> $va ){
                    if(isset($xian[$va['are_id']])){
                        $v[$ka]['are_children'] = $xian[$va['are_id']];
                        unset($xian[$va['are_id']]);
                    }

                }
                $shi[$k] = $v;
            }
            foreach( $sheng as $k => $v ){
                if(isset($shi[$v['are_id']])){
                    $sheng[$k]['are_children'] = $shi[$v['are_id']];
                    unset($shi[$v['are_id']]);
                }

            }
            $all = array( );
            $part = array( );
            //dd($sheng);
            foreach( $sheng as $k => $v ){
                $xz1 = '0';
                $wx1 = '0';
                if($v['are_children']){
                    foreach( $v['are_children'] as $ka => $va )
                    {
                        $xz2 = '0';
                        $wx2 = '0';
                        foreach( $va['are_children'] as $ke => $ve )
                        {
                            if($ve['are_state']=='2'){
                                $wx2 = '1';
                            }else{
                                $xz2 = '1';
                                $all[] = $ve['are_id'];
                                $part[] = $ve['are_id'];
                            }
                        }
                        if($xz2=='1' && $wx2=='1'){
                            $sheng[$k]['are_children'][$ka]['are_state'] = '3';
                            $xz1 = '1';
                            $wx1 = '1';
                            $all[] = $va['are_id'];
                        }elseif ( $xz2=='1' &&  $wx2=='0')
                        {
                            $sheng[$k]['are_children'][$ka]['are_state'] = '1';
                            $xz1 = '1';
                            $all[] = $va['are_id'];
                            $part[] = $va['are_id'];
                        }elseif ( $xz2=='0' &&  $wx2=='1')
                        {
                            $sheng[$k]['are_children'][$ka]['are_state'] = '2';
                            $wx1 = '1';
                        }
                    }
                }

                if($xz1=='1' && $wx1=='1'){
                    $sheng[$k]['are_state'] = '3';
                    $all[] = $v['are_id'];
                }elseif ( $xz1=='1' &&  $wx1=='0')
                {
                    $sheng[$k]['are_state'] = '1';
                    $all[] = $v['are_id'];
                    $part[] = $v['are_id'];
                }elseif ( $xz1=='0' &&  $wx1=='1')
                {
                    $sheng[$k]['are_state'] = '2';
                }
            }
            $data['sheng'] = $sheng;
            $data['all'] = $all;
            $data['part'] = $part;
            return $this->success('获取成功',$data);exit;
        }else{
            return $this->error('暂无数据', ErrorInc::DATA_EMPTY);exit;
        }
    }

    //展示列表

    /**
     * @param Request $request
     * @return mixed
     * @author du
     * @date 2021/9/1
     */
     public function search_list (Request $request)
     {
         $all_button = BmmcService::whereButton($request->input('token'),$request->input('Pri_id'));
         $export = BmmcService::getTopButton('save_state','',$all_button);
         $data['button_top'] = $export['button_top']??[];//顶部按钮
         $data['button_search'] = $export['button_search']??[];//搜索栏按钮

         $list = DB::table('disareanew')->where('are_state',1)->orderBy('are_shid')->orderBy('are_sid')->orderBy('are_xid')->get();
         //dd($list);
         if (!$list->isEmpty()){
             $list = $list->toArray();
             $shiarray=array();
             $shengarray=array();
             foreach( $list as $k => $v )
             {
                 $xdata['are_id'] = $v['are_xid'];
                 $xdata['are_name'] = $v['are_xian'];
                 $xdata['are_state'] = $v['are_state'];
                 $sdata['are_id'] = $v['are_sid'];
                 $sdata['are_name'] = $v['are_shi'];
                 $sdata['are_state'] = '2';
                 $hdata['are_id'] = $v['are_shid'];
                 $hdata['are_name'] = $v['are_sheng'];
                 $hdata['are_state'] = '2';
                 $xian[$v['are_sid']][] = $xdata;
                 if(!in_array($v['are_sid'],$shiarray)){
                     $shiarray[] = $v['are_sid'];
                     $shi[$v['are_shid']][] = $sdata;
                 }if(!in_array($v['are_shid'],$shengarray)){
                 $shengarray[] = $v['are_shid'];
                 $sheng[] = $hdata;
             }
                 unset($list[$k]);
             }
             foreach( $shi as $k => $v ){
                 foreach( $v as $ka=> $va ){
                     $v[$ka]['are_children'] = $xian[$va['are_id']];
                     unset($xian[$va['are_id']]);
                 }
                 $shi[$k] = $v;
             }
             foreach( $sheng as $k => $v ){
                 $sheng[$k]['are_children'] = $shi[$v['are_id']];
                 unset($shi[$v['are_id']]);
             }
             $all = array( );
             $part = array( );
             //dd($sheng);
             foreach( $sheng as $k => $v ){
                 $xz1 = '0';
                 $wx1 = '0';
                 foreach( $v['are_children'] as $ka => $va )
                 {
                     $xz2 = '0';
                     $wx2 = '0';
                     foreach( $va['are_children'] as $ke => $ve )
                     {
                         if($ve['are_state']=='2'){
                             $wx2 = '1';
                         }else{
                             $xz2 = '1';
                             $all[] = $ve['are_id'];
                             $part[] = $ve['are_id'];
                         }
                     }
                     if($xz2=='1' && $wx2=='1'){
                         $sheng[$k]['are_children'][$ka]['are_state'] = '3';
                         $xz1 = '1';
                         $wx1 = '1';
                         $all[] = $va['are_id'];
                     }elseif ( $xz2=='1' &&  $wx2=='0')
                     {
                         $sheng[$k]['are_children'][$ka]['are_state'] = '1';
                         $xz1 = '1';
                         $all[] = $va['are_id'];
                         $part[] = $va['are_id'];
                     }elseif ( $xz2=='0' &&  $wx2=='1')
                     {
                         $sheng[$k]['are_children'][$ka]['are_state'] = '2';
                         $wx1 = '1';
                     }
                 }
                 if($xz1=='1' && $wx1=='1'){
                     $sheng[$k]['are_state'] = '3';
                     $all[] = $v['are_id'];
                 }elseif ( $xz1=='1' &&  $wx1=='0')
                 {
                     $sheng[$k]['are_state'] = '1';
                     $all[] = $v['are_id'];
                     $part[] = $v['are_id'];
                 }elseif ( $xz1=='0' &&  $wx1=='1')
                 {
                     $sheng[$k]['are_state'] = '2';
                 }
             }

             /*foreach ($sheng as $k=>$v){
                 if ($v['are_state'] != 2){
                    $pdd[$k][]=$v;
                    foreach ($v as $kk=>$vv){
                        if ($vv['are_state'] != 2){
                            $pdd[$k]['area_children'][$kk][]=$vv;
                        }
                    }
                 }
             }*/

             $data['sheng'] = $sheng;
             $data['all'] = $all;
             $data['part'] = $part;
             return $this->success('获取成功',$data);exit;
         }else{
             return $this->error('暂无数据', ErrorInc::DATA_EMPTY);exit;
         }
     }

    /**|---------------------------------------------------------------------------------
     * @name 编辑数据状态
     * @param are_ids
     * @return \Illuminate\Http\JsonResponse
     * |---------------------------------------------------------------------------------
     */
    public function save_state (Request $request)
    {
        if ($request->filled('are_ids')){
            $are_ids = $request->input('are_ids');
            if ($request->input('type') == 1){//选中
                $pdd['are_state'] = 1;
            }elseif($request->input('type') == 2){
                $pdd['are_state'] = 2;
            }
            $info = DB::table('disareanew')->whereRaw("are_xid in (".trim($are_ids,',').")")->update($pdd);
            if ($info){
                $this->insertingLog("编辑，","","编辑数据状态","自动派单区域管理",$request->ip(), $request->get('session_token')['admin_acc']);
                return $this->success('操作成功');exit;
            } else {
                return $this->error('操作失败', ErrorInc::MYSQL_FAILED);exit;
            }
        }else{
            return $this->error('参数错误', ErrorInc::FAILED_PARAM);exit;
        }

    }
}
