<?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_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 Session;

class PrintController extends CommonController
{

    public function printed($id)
    {
        if($id == 10){
            $table_name = '成本审计汇总表';
            $th = [
                '合同编号',
                '姓名',
                '房栋号',
                '身份证',
                '联系电话',
                '有证面积',
                '无证面积',
                '房屋补偿金',
                '庭院面积',
                '庭院补偿',
                '装修',
            ];
            $th1 = [
                '构筑物及其他',
                '停产停业损失补偿（临街路补偿）',
                '临时安置',
                '搬迁',
                '低保',
                '全额奖励金',
                '整栋奖励',
                '违章自行拆除费',
                '残疾及其他',
                '补偿总额',
                '回迁套数',
            ];
            $th2 = [
                '回迁小区楼栋号',
                '回迁住宅面积',
                '回迁商铺面积',
                '回迁楼房金额',
                '应缴差价',
                '应退差价',
                '签约日期',
                '交房日期',
                '备注',
            ];

            $table[1] = Table_one::query()
                ->groupBy('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_addr')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(total_money) as total_money')
                ->selectRaw('SUM(money_home_jchae) as money_home_jchae')
                ->selectRaw('SUM(money_home_tchae) as money_home_tchae')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[2] = Table_two::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(chaichuje) as chaichuje')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[3] = Table_three::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(canbu) as canbu')
                ->selectRaw('SUM(linshibu) as linshibu')
                ->selectRaw('SUM(banqianbu) as banqianbu')
                ->selectRaw('SUM(total) as total')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[4] = Table_four::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(quanj) as quanj')
                ->selectRaw('SUM(chaj) as chaj')
                ->selectRaw('SUM(tongj) as tongj')
                ->selectRaw('SUM(zongj) as zongj')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[5] = Table_five::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(yuqij) as yuqij')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[6] = Table_six::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(anzhi) as anzhi')
                ->selectRaw('SUM(banqian) as banqian')
                ->selectRaw('SUM(dibu) as dibu')
                ->selectRaw('SUM(other) as other')
                ->selectRaw('SUM(totalj) as total')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[7] = Table_seven::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(data1_1+data1_7) as youzheng_area')
                ->selectRaw('SUM(data1_3) as wuzheng_area')
                ->selectRaw('SUM(data24) as fangbu')
                ->selectRaw('SUM(data25) as zhuangxiu')
                ->selectRaw('SUM(data26) as tingyuan')
                ->selectRaw('SUM(data27) as tingchan')
                ->selectRaw('SUM(data39_13) as gouzhu')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(data43) as chaiqian')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            $table[8] = Table_eight::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(data1_1+data1_7) as youzheng_area')
                ->selectRaw('SUM(data1_3) as wuzheng_area')
                ->selectRaw('SUM(data24) as fangbu')
                ->selectRaw('SUM(data26) as zhangxiu')
                ->selectRaw('SUM(data25) as tingyuan')
                ->selectRaw('SUM(data27) as tingchan')
                ->selectRaw('SUM(data39_13) as gouzhu')
                ->selectRaw('SUM(data43) as chaiqian')
                ->selectRaw('SUM(hours) as hours')
                ->selectRaw('SUM(data63) as huiqian_area')
                ->selectRaw('SUM(data50) as huiqian_home_area')
                ->selectRaw('SUM(data55) as huiqian_shang_area')
                ->selectRaw('SUM(data58 +data62 ) as huiqian')
                ->selectRaw('SUM(data59) as yingtui')
                ->selectRaw('SUM(data60) as yingjiao')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();


            $table[9] = Table_nine::query()
                ->groupBy('bzc_addr')
                ->selectRaw('bzc_addr')
                ->selectRaw('num')
                ->selectRaw('bzc_name')
                ->selectRaw('bzc_tel')
                ->selectRaw('bzc_cardid')
                ->selectRaw('SUM(data25) as fangbu')
                ->selectRaw('SUM(data26) as kongyuan')
                ->selectRaw('SUM(data27) as tingye')
                ->selectRaw('SUM(data27_6) as other')
                ->selectRaw('SUM(data40_5) as gouzhu')
                ->selectRaw('SUM(data44) as chaiqian')
                ->selectRaw('SUM(table_time) as table_time')
                ->selectRaw('SUM(sub_description) as sub_description')
                ->get();

            return view('admin.print.print_all',compact('table','th','th1','th2','table_name','user_data'));
        }

        if(!$table = Tablelist::find($id)) return back()->withErrors('网络错误！');

        $type = $table->table_id;
        $child_id = $table->child_table_id;

        switch($type){
            case 1:
                if(!$table = Table_one::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_4_one',compact('table'));
                break;
            case 2:
                if(!$table = Table_two::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_7_two',compact('table'));
                break;
            case 3:
                if(!$table = Table_three::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_8_three',compact('table'));
                break;
            case 4:
                if(!$table = Table_four::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_6_four',compact('table'));
                break;
            case 5:
                if(!$table = Table_five::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_9_five',compact('table'));
                break;
            case 6:
                if(!$table = Table_six::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_5_six',compact('table'));
                break;
            case 7:
                if(!$table = Table_seven::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_3_seven',compact('table'));
                break;
            case 8:

                if(!$table = Table_eight::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                $table['jsons'] = json_decode($table->json);
                return view('admin.print.table_1_eight',compact('table'));
                break;
            case 9:
                if(!$table = Table_nine::find($child_id)) return back()->withErrors('可能非法数据库改动造成的数据丢失！');
                return view('admin.print.table_2_nine',compact('table'));
                break;
        }

    }

    public function print_table(Request $request,$type)
    {
        if($type == 'create'){
            $table = '';
            switch($request->table_type){
                case 1:
                    return view('admin.print.table_4_one',compact('table'));
                    break;
            }
        }


    }

}