<?php

namespace App\Http\Controllers\Admin;


use App\Model\Table_name;
use App\Model\Tablelist;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\CommonController;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

class TableController extends CommonController
{
    public function index()
    {
        $data = Table_name::orderby('id','asc')->paginate(10);
        return view('admin.table.table',compact('data'));
    }

//    待办案件
    public function on_table()
    {
        $user = session('user');
       if($user->role == 0){
           $data = Tablelist::where('status','=','0')->where('user_id','=',$user['id'])->orderby('id','asc')->paginate(10);
       }else{
           $data = Tablelist::where('status','=',$user->role)->orderby('id','asc')->paginate(10);
       }
        return view('admin.table.on_table',compact('data'));
    }

//    $table_id 表格类型id
    public function show($table_type_id)
    {
        if($table_type_id == 1){
            return view('admin.table.table_one',compact('table_type_id'));
        }
        if($table_type_id == 2){
            return view('admin.table.table_two',compact('table_type_id'));
        }
        if($table_type_id == 3){
            return view('admin.table.table_three',compact('table_type_id'));
        }
        if($table_type_id == 4){
            return view('admin.table.table_four',compact('table_type_id'));
        }
        if($table_type_id == 5){
            return view('admin.table.table_five',compact('table_type_id'));
        }
        if($table_type_id == 6){
            return view('admin.table.table_six',compact('table_type_id'));
        }
        if($table_type_id == 7){
            return view('admin.table.table_seven',compact('table_type_id'));
        }
        if($table_type_id == 8){
            return view('admin.table.table_eight',compact('table_type_id'));
        }
        if($table_type_id == 9){
            return view('admin.table.table_nine',compact('table_type_id'));
        }
    }

//    尚未完成表单仅保存
//    $table_id 表格类型id
    public function save(Request $request,$table_id)
    {
        $user = session('user');
        $input = $request->only('project_name','bzc_name','bzc_cardid','bzc_addr','bzc_area','house_type','bzc_tel','used','num','table_time');
//        表格未完成保存 0
        $data = $input;
        if($data['bzc_area'] == null){
            $data['bzc_area'] = 0;
        }
        if($data['house_type'] == null){
            $data['house_type'] = '未填';
        }
        if($data['bzc_cardid'] == null){
            $data['house_type'] = '未填';
        }
        if($data['table_time'] == null){
            $data['table_time'] = date('y-m-d h:i:s',time());
        }
        $data['status'] = '0';
        $data['user_id'] = $user->id;
        $data['table_id'] = $table_id;
        $data['json'] = json_encode($request->except('_token'));

        $re = Tablelist::create($data);
        if($re){
            return redirect('/on_table');
        }else{
            return back()->withErrors('errors','保存失败请重试!');
        }

//        把数据转化为json
//        $v = json_encode($request->all());
//        解析json为对象
//        $a = json_decode($v);
    }

//    新建表提交
//    $table_id 表格类型id
    public function create(Request $request,$table_type_id)
    {
        $user = session('user');
        $input = $request->only('project_name','bzc_name','bzc_cardid','bzc_addr','bzc_area','house_type','bzc_tel','used','num','table_time','sub_description');
        $data = $input;

        $data['status'] = '1';
        $data['table_id'] = $table_type_id;
        $data['user_id'] = $user->id;
        if($data['bzc_area'] == null){
            $data['bzc_area'] = 0;
        }
        if($data['house_type'] == null){
            $data['house_type'] = '默认:住房';
        }
        if($data['sub_description'] == null){
            $data['sub_description'] = '无';
        }
        $data['json'] = json_encode($request->except('_token'));
        $re = Tablelist::create($data);
        if($re){
            return redirect('/on_table');
        }else {
            return back()->with('errors','提交错误，请检查后重试！');
        }
    }
//    $table_id表格自增id
//        编辑表单提交
    public function submit(Request $request,$table_id)
    {
        $user = session('user');
        $input = $request->only('project_name','bzc_name','bzc_cardid','bzc_addr','bzc_area','house_type','bzc_tel','used','num','table_time','sub_description');
        $data = $input;

        $data['user_id'] = $user->id;
        if($data['bzc_area'] == null){
            $data['bzc_area'] = 0;
        }
        if($data['house_type'] == null){
            $data['house_type'] = '默认:住房';
        }
        switch($user->role){
            case 1:
                $data['hesuan'] = $user->surname;
                break;
            case 2:
                $data['fuhe'] = $user->surname;
                break;
            case 3:
                $data['chushen'] = $user->surname;
                break;
            case 4:
                $data['jianshen'] = $user->surname;
                break;
        }
//        0:未提交 1已核算  2已复核 3已提交 4：已初审 5：已复审 6已监审
        $find = Tablelist::find($table_id);
            //提交一次，在原status值上加1
            $data['status'] = $find['status'] + 1;
            $re = Tablelist::where('id',$table_id)->update($data);
            if($re){
                return redirect('/on_table');
            }else{
                return back()->with('errors','提交失败，请检查重试！');
            }
    }

    public function delete($table_id)
    {
        $re = Tablelist::where('id',$table_id)->delete();
        if($re){
            $data = [
                'status'=>1,
                'msg' => '删除成功！'
            ];
        }else{
            $data = [
                'status'=>0,
                'msg' => '删除失败！'
            ];
        }
        return $data;
    }


//    $table_id表格自增id
    public function edit($table_id)
    {
        $table = Tablelist::find($table_id);
        $temp = [
            'project_name' => $table->project_name,
            'bzc_name' => $table->bzc_name,
            'bzc_cardid' => $table->bzc_cardid,
            'bzc_addr' => $table->bzc_addr,
            'bzc_area' => $table->bzc_area,
            'house_type' => $table->house_type,
            'bzc_tel' => $table->bzc_tel,
            'used' => $table->used,
            'num' => $table->num,
            'table_time' => $table->table_time,
            'sub_description' => $table->sub_description
        ];
        $table_type_id = $table['table_id'];
        $json = json_decode($table['json'],true);
        $table = json_encode($table);
        $table = json_decode($table,true);
        $table = array_merge($table,$json);
        $user = session('user');
        $table['judge']=$user->role;
        $table['project_name'] = $temp['project_name'];
        $table['bzc_name'] = $temp['bzc_name'];
        $table['bzc_cardid'] = $temp['bzc_cardid'];
        $table['bzc_addr'] = $temp['bzc_addr'];
        $table['bzc_area'] = $temp['bzc_area'];
        $table['house_type'] = $temp['house_type'];
        $table['bzc_tel'] = $temp['bzc_tel'];
        $table['used'] = $temp['used'];
        $table['num'] = $temp['num'];
        $table['table_time'] = $temp['table_time'];
        $table['sub_description'] = $temp['sub_description'];
        $table = (object)$table;

        if($table_type_id == 1){
            return view('admin.edit_table.table_one',compact('table_id','table'));
        }
        if($table_type_id == 2){
            return view('admin.edit_table.table_two',compact('table_id','table'));
        }
        if($table_type_id == 3){
            return view('admin.edit_table.table_three',compact('table_id','table'));
        }
        if($table_type_id == 4){
            return view('admin.edit_table.table_four',compact('table_id','table'));
        }
        if($table_type_id == 5){
            return view('admin.edit_table.table_five',compact('table_id','table'));
        }
        if($table_type_id == 6){
            return view('admin.edit_table.table_six',compact('table_id','table'));
        }
        if($table_type_id == 7){
            return view('admin.edit_table.table_seven',compact('table_id','table'));
        }
        if($table_type_id == 8){
            return view('admin.edit_table.table_eight',compact('table_id','table'));
        }
        if($table_type_id == 9){
            return view('admin.edit_table.table_nine',compact('table_id','table'));
        }
    }

    //    $table_id表格自增id
    public function update(Request $request,$table_id)
    {
        $user = session('user');
        $input = $request->only('project_name','bzc_name','bzc_cardid','bzc_addr','bzc_area','house_type','bzc_tel','used','num','table_time');
//        表格未完成保存 0
        $data = $input;
        $data['user_id'] = $user->id;
        $data['json'] = json_encode($request->except('_token'));

        $re = Tablelist::where('id',$table_id)->update($data);
        if($re){
            return redirect('/on_table/');
        }else{
            return back()->with('errors','保存更新错误，请稍后重试！');
        }
    }


    public function  statistics($table_type_id)
    {
        if($table_type_id){
            switch($table_type_id){
                case 1:
//                all() get()对象集合
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $data1 = 0;
                    $data2 = 0;
                    $data4 = 0;
                    $data5 = 0;
                    $data6 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['total_money'];
                        $data2 += $json['home_area']+$json['money_bus_area'];
                        $data4 += $json['money_home_price']+$json['money_bus_price'];
                        $data5 += $json['money_home_jchae']+$json['money_bus_jchae'];
                        $data6 += $json['money_home_tchae']+$json['money_bus_tchae'];
                    }
                    $table_name = '国有土地房屋征收与补偿安置单（货币化安置）';
                    $th = [
                        '1'=>'拆迁补偿总金额',
                        '2'=>'购买存量房面积',
                        '4'=>'购买存量房总价格',
                        '5'=>'应交总差额',
                        '6'=>'应退总查额'
                    ];
                    $td = [
                        '1'=>$data1,
                        '2'=>$data2,
                        '4'=>$data4,
                        '5'=>$data5,
                        '6'=>$data6
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;

                case 2:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '违章建筑自行拆除费用申领表';
                    $data1 = 0;
                    $data2 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['bzc_area'];
                        $data2 += $json['chaichuje'];
                    }
                    $th = [
                        '1'=>'违章总面积（㎡）',
                        '2'=>'拆除总金额',
                    ];
                    $td = [
                        '1'=>$data1,
                        '2'=>$data2,
                    ];

                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;
                case 3:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '海勃湾区残疾人房屋征收补贴审批确认单';
                    $data1 = 0;
                    $data2 = 0;
                    $data3 = 0;
                    $data4 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['canbu'];
                        $data2 += $json['linshibu'];
                        $data3 += $json['banqianbu'];
                        $data4 += $json['total'];
                    }
                    $th=[
                        '1'=>'残疾补贴',
                        '2'=>'临时安置补贴',
                        '3'=>'搬迁补贴',
                        '4'=>'总计'
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                        '4'=>$data4,
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;
                case 4:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '海勃湾区房屋征收提前搬迁奖励单';
                    $data1 =0;
                    $data2 =0;
                    $data3 =0;
                    $data4 =0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['quanj'];
                        $data2 += $json['chaj'];
                        $data3 += $json['tongj'];
                        $data4 += $json['zongj'];
                    }
                    $th=[
                        '1'=>'全额奖励',
                        '2'=>'差额奖励',
                        '3'=>'同住奖励金',
                        '4'=>'奖励总金额',
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                        '4'=>$data4,
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                break;
                case 5:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '房屋逾期过渡安置补助费发放单';
                    $data1 = 0;
                    $data2 = 0;

                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['total1'];
                        $data2 += $json['yuqij'];
                    }
                    $data3 = $data1+$data2;
                    $th=[
                        '1'=>'补助标准合计',
                        '2'=>'逾期补偿金额	',
                        '3'=>'总计',
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                break;

                case 6:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '海勃湾区房屋征收安置补贴单';
                    $data1 = 0;
                    $data2 = 0;
                    $data3 = 0;
                    $data4 = 0;
                    $data5 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['anzhi'];
                        $data2 += $json['anqian'];
                        $data3 += $json['dibu'];
                        $data4 += $json['other'];
                        $data5 += $json['totalj'];
                    }
                    $th=[
                      '1'=>'临时安置费',
                      '2'=>'搬迁费',
                      '3'=>'低保补贴',
                      '4'=>'其他',
                      '5'=>'补贴总金额',
                    ];
                    $td=[
                      '1'=>$data1,
                      '2'=>$data2,
                      '3'=>$data3,
                      '4'=>$data4,
                      '5'=>$data5,
                    ];

                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;

                case 7:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '国有土地上房屋征收与补偿核算单（货币化安置）';
                    $data1 = 0;
                    $data2 = 0;
                    $data3 = 0;
                    $data4 = 0;
                    $data5 = 0;
                    $data6 = 0;
                    $data7 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['data3']+$json['data4'];
                        $data2 += $json['data7']+$json['data8'];
                        $data3 += $json['data11']+$json['data12'];
                        $data4 += $json['data15']+$json['data16'];
                        $data5 += $json['data25'];
                        $data6 += $json['data26'];
                        $data7 += $json['data27'];
                    }
                    $th=[
                      '1'=>'住宅有证面积补偿额',
                      '2'=>'无证面积补偿额',
                      '3'=>'往改非面积补偿额',
                      '4'=>'商业有证面积补偿额',
                      '5'=>'装修补偿额',
                      '6'=>'庭院补偿额',
                      '7'=>'停产停业损失补偿额',
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                        '4'=>$data4,
                        '5'=>$data5,
                        '6'=>$data6,
                        '7'=>$data7,
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;

                case 8:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name='国有土地上房屋征收与补偿核算单（产权调换）';
                    $data1 = 0;
                    $data2 = 0;
                    $data3 = 0;
                    $data4 = 0;
                    $data5 = 0;
                    $data6 = 0;
                    $data7 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['data3']+$json['data4'];
                        $data2 += $json['data7']+$json['data8'];
                        $data3 += $json['data11']+$json['data12'];
                        $data4 += $json['data15']+$json['data16'];
                        $data5 += $json['data25'];
                        $data6 += $json['data26'];
                        $data7 += $json['data27'];
                    }
                    $th=[
                        '1'=>'住宅有证面积补偿额',
                        '2'=>'无证面积补偿额',
                        '3'=>'往改非面积补偿额',
                        '4'=>'商业有证面积补偿额',
                        '5'=>'装修补偿额',
                        '6'=>'庭院补偿额',
                        '7'=>'停产停业损失补偿额',
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                        '4'=>$data4,
                        '5'=>$data5,
                        '6'=>$data6,
                        '7'=>$data7,
                    ];
                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                break;

                case 9:
                    $table = Tablelist::where('table_id','=',$table_type_id)->where('status','>','5')->get();
                    $table_name = '国有土地上房屋征收与补偿核算单（货币补偿）';
                    $data1 = 0;
                    $data2 = 0;
                    $data3 = 0;
                    $data4 = 0;
                    $data5 = 0;
                    $data6 = 0;
                    $data7 = 0;
                    foreach($table as $co){
                        $json = json_decode($co->json,true);
                        $data1 += $json['data25'];
                        $data2 += $json['data26'];
                        $data3 += $json['data27'];
                        $data4 += $json['data28'];
                        $data5 += $json['data43'];
                        $data6 += $json['data44'];
                        $data7 += $json['data24'];
                    }
                    $th=[
                        '1'=>'房屋补偿金额	',
                        '2'=>'空院补偿额',
                        '3'=>'停产停业损失补偿额',
                        '4'=>'房屋补偿总额',
                        '5'=>'构筑物及其他补偿金',
                        '6'=>'拆迁补偿总金额',
                        '7'=>'房屋总面积'
                    ];
                    $td=[
                        '1'=>$data1,
                        '2'=>$data2,
                        '3'=>$data3,
                        '4'=>$data4,
                        '5'=>$data5,
                        '6'=>$data6,
                        '7'=>$data7,
                    ];

                    return view('admin.sta.sta_one',compact('th','td','table_name'));
                    break;
            }
        }else{
            return redirect('/');
        }
    }

}
