<?php

namespace App\Http\Controllers\Admin;


use App\Model\Table_eight;
use App\Model\Table_five;
use App\Model\Table_four;
use App\Model\Table_name;
use App\Model\Table_nine;
use App\Model\Table_one;
use App\Model\Table_seven;
use App\Model\Table_six;
use App\Model\Table_three;
use App\Model\Table_two;
use App\Model\Tablelist;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Admin\CommonController;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
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)
    {

        $view = [
            '',
            'admin.table.table_one',
            'admin.table.table_two',
            'admin.table.table_three',
            'admin.table.table_four',
            'admin.table.table_five',
            'admin.table.table_six',
            'admin.table.table_seven',
            'admin.table.table_eight',
            'admin.table.table_nine'
        ];

        return view($view[$table_type_id], ['table_type_id'=>$table_type_id]);

    }

//    尚未完成表单仅保存
//    $table_type_id 表格类型id
    public function save(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');
//        表格未完成保存 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());
        }

        $child_table_data = $request->all();
        $child_table_data['status'] = '0';
        $data['status'] = '0';
        $data['user_id'] = $user->id;
        $data['table_id'] = $table_type_id;
        $data['json'] = '';
//        $data['json'] = json_encode($request->except('_token'));
        switch($table_type_id){
            case 1:
                $re_child = Table_one::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 2:
                $re_child = Table_two::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 3:
                $re_child = Table_three::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 4:
                $re_child = Table_four::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 5:
                $re_child = Table_five::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 6:
                $re_child = Table_six::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 7:
                $re_child = Table_seven::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 8:
                $re_child = Table_eight::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
                break;
            case 9:
                $re_child = Table_nine::create($child_table_data);
                $data['child_table_id'] = $re_child['id'];
                Tablelist::create($data);
                return redirect('/on_table');
            break;
        }

//        把数据转化为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;

//        save child_table
        if (isset($request)) {
            $request['status'] = '1';
            switch ($table_type_id) {
                case 1:
                    $child = Table_one::create($request->except('_token'));
                    break;
                case 2:
                    $child = Table_two::create($request->except('_token'));
                    break;
                case 3:
                    $child = Table_three::create($request->except('_token'));
                    break;
                case 4:
                    $child = Table_four::create($request->except('_token'));
                    break;
                case 5:
                    $child = Table_five::create($request->except('_token'));
                    break;
                case 6:
                    $child = Table_six::create($request->except('_token'));
                    break;
                case 7:
                    $child = Table_seven::create($request->except('_token'));
                    break;
                case 8:
                    $child = Table_eight::create($request->except('_token'));
                    break;
                case 9:
                    $child = Table_nine::create($request->except('_token'));
                    break;
            };
            if (isset($child)) {
//          create table_list
                $data['child_table_id'] = $child->id;
                $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'));
                $data['json'] = ' ';
                $re = Tablelist::create($data);
                if ($re) {
                    return redirect('/on_table');
                } else {
                    return back()->with('errors', '提交错误，请检查后重试！');
                }
            } else {
                return back()->with('errors', '提交错误，请检查后重试！');
            }
        } 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);
        $find_child = Table_one::find($find->child_table_id);
        //提交一次，在原status值上加1
        $data['status'] = $find['status'] + 1;
        $child_data['status'] = $find_child['status']+1;
        $re = Tablelist::where('id', $table_id)->update($data);
        switch($find->table_id){
            case 1:
                $response = Table_one::where('id',$find->child_table_id)->update($child_data);
                if ($re && $response) {
                    return redirect('/on_table');
                } else {
                    return back()->with('errors', '提交失败，请检查重试！');
                }
            break;
        }
    }

    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_list = Tablelist::find($table_id);
        $table_type_id = $table_list->table_id;
        $child_table_id = $table_list->child_table_id;
        $user = session('user');

        if ($table_type_id == 1) {
            $table = Table_one::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_one', compact('table_id', 'table'));
        }
        if ($table_type_id == 2) {
            $table = Table_two::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_two', compact('table_id', 'table'));
        }
        if ($table_type_id == 3) {
            $table = Table_three::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_three', compact('table_id', 'table'));
        }
        if ($table_type_id == 4) {
            $table = Table_four::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_four', compact('table_id', 'table'));
        }
        if ($table_type_id == 5) {
            $table = Table_five::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_five', compact('table_id', 'table'));
        }
        if ($table_type_id == 6) {
            $table = Table_six::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_six', compact('table_id', 'table'));
        }
        if ($table_type_id == 7) {
            $table = Table_seven::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_seven', compact('table_id', 'table'));
        }
        if ($table_type_id == 8) {
            $table = Table_eight::find($child_table_id);
            $table['judge'] = $user->role;
            return view('admin.edit_table.table_eight', compact('table_id', 'table'));
        }
        if ($table_type_id == 9) {
            $table = Table_nine::find($child_table_id);
            $table->judge = $user->role;
            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 sta_table(Request $request, $table_type_id)
    {
        if (isset($table_type_id)) {
            switch ($table_type_id) {
                case 1:
                    $table = DB::select('SELECT bzc_name ,SUM(home_area+money_bus_area) as area,SUM(total_money) as total_money,SUM(money_home_price+money_bus_price) as price,
SUM(money_home_jchae+money_bus_jchae) as jchae ,SUM(money_home_tchae+money_bus_tchae) as tchae
FROM table_one WHERE status > 5 GROUP BY bzc_name');
                    $table_name = '国有土地房屋征收与补偿安置单（货币化安置）';
                    $th = [
                        '1' => '姓名',
                        '2' => '购买存量房面积',
                        '3' => '拆迁补偿总金额',
                        '4' => '购买存量房总价格',
                        '5' => '应交总差额',
                        '6' => '应退总差额'
                    ];
                    return view('admin.sta.sta_one', compact('table_name', 'table', 'th', 'table_type_id'));
                    break;

                case 2:
                    $table_name = '违章建筑自行拆除费用申领表';
                    $th = [
                        '1' => '姓名',
                        '2' => '违章面积',
                        '3' => '拆除金额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(bzc_area) as area,SUM(chaichuje)as money
FROM table_two WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_two', compact('table_name', 'table', 'th'));
                    break;

                case 3:
                    $table_name = '残疾人房征补贴审批确认单';
                    $th = [
                        '1' => '姓名',
                        '2' => '残疾人补贴',
                        '3' => '临时安置补贴	',
                        '4' => '搬迁补贴',
                        '5' => '合计'
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(canbu) as canbu,SUM(linshibu)as linshibu,SUM(banqianbu) as banqianbu,SUM(total) as total
FROM table_three WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_three', compact('table_name', 'table', 'th'));
                    break;

                case 4:
                    $table_name = '房屋征收提前搬迁奖励单';
                    $th = [
                        '1' => '姓名',
                        '2' => '全额奖励',
                        '3' => '差额奖励金',
                        '4' => '同住奖励',
                        '5' => '奖励总金额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(quanj) as quanj,SUM(chaj)as chaj,SUM(tongj) as tongj,SUM(zongj) as zongj
FROM table_four WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_four', compact('table_name', 'table', 'th'));
                    break;

                case 5:
                    $table_name = '逾期过渡安置补助费发放单';
                    $th = [
                        '1' => '姓名',
                        '2' => '逾期月数',
                        '3' => '补助合计',
                        '4' => '逾期补偿金',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(yuqi_mothe1+yuqi_mothe2+yuqi_mothe3+yuqi_mothe4) as yuqi_mothe,SUM(total1+total2+total3+total4)as total,SUM(yuqij) as yuqij
FROM table_five WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_five', compact('table_name', 'table', 'th'));
                    break;

                case 6:
                    $table_name = '房屋征收安置补贴单';
                    $th = [
                        '1' => '姓名',
                        '2' => '临时安置费',
                        '3' => '搬迁费',
                        '4' => '低保补贴',
                        '5' => '其他',
                        '6' => '补贴总金额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(anzhi) as anzhi,SUM(banqian)as banqian,SUM(dibu) as dibu,SUM(other) as other,SUM(totalj) as totalj
FROM table_six WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_six', compact('table_name', 'table', 'th'));
                    break;

                case 7:
                    $table_name = '核算单（货币化安置）';
                    $th = [
                        '1' => '姓名',
                        '2' => '住宅有证面积可置换住宅面积',
                        '3' => '住宅有证面积可置换商业面积',
                        '4' => '住宅有证面积房务补偿',
                        '5' => '无证面积可置换住宅面积',
                        '6' => '无证面积可置换商业面积',
                        '7' => '无证面积房务补偿',
                    ];
                    $th1 = [
                        '1' => '姓名',
                        '2' => '其中住改非可置换住宅面积',
                        '3' => '其中住改非可置换商业面积',
                        '4' => '其中住改非房务补偿',
                        '5' => '商业有证面积可置换住宅面积',
                        '6' => '商业有证面积可置换商业面积',
                        '7' => '商业有证面积房务补偿',
                    ];
                    $th2 = [
                        '1' => '姓名',
                        '2' => '其他项可置换住宅面积',
                        '3' => '其他项可置换商业面积',
                        '4' => '其他项房务补偿',
                        '5' => '可置换住宅面积小计',
                        '6' => '可置换商业面积小计',
                        '7' => '房务补偿小计',
                    ];
                    $th3 = [
                        '1' => '姓名',
                        '2' => '装修补偿额',
                        '3' => '庭院补偿额',
                        '4' => '停产停业损失补偿额',
                        '5' => '构筑物及其他补偿金',
                        '6' => '拆迁补偿总金额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(bzc_area) as bzc_area,SUM(data2)as data2,SUM(data3+data4) as data3,SUM(data5) as data5,
SUM(data6) as data6,SUM(data7+data8) as data7 ,SUM(data9) as data9,SUM(data10) as data10,SUM(data11+data12) as data11,SUM(data13) as data13,
SUM(data14) as data14,SUM(data15+data16) as data15,SUM(data17) as data17,SUM(data18) as data18,SUM(data19+data20) as data19,SUM(data21) as data21,
SUM(data22) as data22,SUM(data23+data24) as data23,SUM(data25) as data25,SUM(data26) as data26,SUM(data27) as data27,SUM(data42) as data42,
SUM(data43) as data43
FROM table_seven WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_seven', compact('table_name', 'table', 'th', 'th1', 'th2', 'th3'));
                    break;

                case 8:
                    $table_name = '核算单（产权调换）';
                    $th = [
                        '1' => '姓名',
                        '2' => '住宅有证面积可置换住宅面积',
                        '3' => '住宅有证面积可置换商业面积',
                        '4' => '住宅有证面积房务补偿',
                        '5' => '无证面积可置换住宅面积',
                        '6' => '无证面积可置换商业面积',
                        '7' => '无证面积房务补偿',
                    ];
                    $th1 = [
                        '1' => '姓名',
                        '2' => '其中住改非可置换住宅面积',
                        '3' => '其中住改非可置换商业面积',
                        '4' => '其中住改非房务补偿',
                        '5' => '商业有证面积可置换住宅面积',
                        '6' => '商业有证面积可置换商业面积',
                        '7' => '商业有证面积房务补偿',
                    ];
                    $th2 = [
                        '1' => '姓名',
                        '2' => '其他项可置换住宅面积',
                        '3' => '其他项可置换商业面积',
                        '4' => '其他项房务补偿',
                        '5' => '可置换住宅面积小计',
                        '6' => '可置换商业面积小计',
                        '7' => '房务补偿小计',
                    ];
                    $th3 = [
                        '1' => '姓名',
                        '2' => '装修补偿额',
                        '3' => '庭院补偿额',
                        '4' => '停产停业损失补偿额',
                        '5' => '构筑物及其他补偿金',
                        '6' => '拆迁补偿总金额',
                    ];
                    $th4 = [
                        '1' => '姓名',
                        '2' => '住宅可调换面积',
                        '3' => '住宅实际调换面积',
                        '4' => '住宅大于可调换面积',
                        '5' => '住宅可调换面积价格核算',
                        '6' => '住宅大于可调换面积价格核算',
                        '7' => '住宅调换房屋价格',
                    ];
                    $th5 = [
                        '1' => '姓名',
                        '2' => '商业可调换面积',
                        '3' => '商业实际调换面积',
                        '4' => '商业大于可调换面积',
                        '5' => '商业可调换面积价格核算',
                        '6' => '商业大于可调换面积价格核算',
                        '7' => '商业调换房屋价格',
                        '8' => '应退差额',
                        '9' => '应缴差额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(bzc_area) as bzc_area,SUM(data2)as data2,SUM(data3+data4) as data3,SUM(data5) as data5,
SUM(data6) as data6,SUM(data7+data8) as data7 ,SUM(data9) as data9,SUM(data10) as data10,SUM(data11+data12) as data11,SUM(data13) as data13,
SUM(data14) as data14,SUM(data15+data16) as data15,SUM(data17) as data17,SUM(data18) as data18,SUM(data19+data20) as data19,SUM(data21) as data21,
SUM(data22) as data22,SUM(data23+data24) as data23,SUM(data25) as data25,SUM(data26) as data26,SUM(data27) as data27,SUM(data42) as data42,
SUM(data43) as data43,SUM(home_area) as home_area,SUM(data50) as data50,SUM(data51) as data51,SUM(data52) as data52,SUM(data57) as data57,
SUM(data58) as data58,SUM(data53) as data53,SUM(data54) as data54,SUM(data55) as data55,SUM(data56) as data56,SUM(data61) as data61,
SUM(data62) as data62,SUM(data59) as data59,SUM(data60) as data60
FROM  table_eight  WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_eight', compact('table_name', 'th', 'th1', 'th2', 'th3', 'th4', 'th5', 'table'));
                    break;

                case 9:
                    $table_name = '核算单（货币补偿）';
                    $th = [
                        '1' => '姓名',
                        '2' => '房1房屋面积',
                        '3' => '房2房屋面积',
                        '4' => '房3房屋面积',
                        '5' => '房4房屋面积',
                        '6' => '房5房屋面积',
                        '7' => '房6房屋面积',
                        '8' => '合计',
                    ];
                    $th1 = [
                        '1' => '姓名',
                        '2' => '房1装修',
                        '3' => '房2装修',
                        '4' => '房3装修',
                        '5' => '房4装修',
                        '6' => '房5装修',
                        '7' => '房6装修',
                        '8' => '小计',
                    ];
                    $th2 = [
                        '1' => '姓名',
                        '2' => '空院补偿额',
                        '3' => '停产停业损失补偿额',
                        '4' => '房屋补偿总额',
                        '5' => '构筑物及其他补偿金',
                        '6' => '拆迁补偿总金额',
                    ];
                    $table = DB::select('SELECT bzc_name ,SUM(data6) as data6,SUM(data9)as data9,SUM(data12) as data12,SUM(data15) as data15,
SUM(data18) as data18,SUM(data21) as data21,SUM(data24) as data24,SUM(data7) as data7,SUM(data10) as data10,SUM(data13) as data13,SUM(data16) as data16,
SUM(data19) as data19,SUM(data22) as data22,SUM(data25) as data25,SUM(data26) as data26,SUM(data27) as data27,SUM(data28) as data28,SUM(data43) as data43,
SUM(data44) as data44
FROM table_nine WHERE status > 5 GROUP BY bzc_name');
                    return view('admin.sta.sta_nine', compact('table_name', 'table', 'th', 'th1', 'th2'));
                    break;
            }
        } else {
            return redirect('/');
        }
    }

}
