<?php
/**
 *  
 *  @author  Mike <m@9026.com>
 *  @version    1.0
 *  @date 2015年10月12日
 *
 */
namespace App\Http\Controllers\Admin\Base;

use App\Http\Controllers\Admin\Controller;
use App\Models\Config;
use App\Models\InnerDevice;
use App\Models\Option;
use App\Models\Order;
use App\Models\Project;
use App\Models\User;
use App\Models\UserAuth;
use App\Services\Base\Tree;
use App\Services\Base\BaseArea;
use Carbon\Carbon;

class IndexController extends Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    function index() {
        $menus = $this->getMenus();
        $title = Config::getTitle();
        return view('admin.base.index.index',compact('menus', 'title'));
    }
    function welcome() {
//        return redirect('/admin/AdminUser/index');
        $total_device = InnerDevice::count();
        $total_project = Project::count();
        $total_money = Order::sum('money') / 100;
        $users_total = User::count();
        $users_not_auth_total = UserAuth::where('active', 2)->count();
        $users_auth_total = UserAuth::count();

        $items = InnerDevice::get();
        $types = ['free', 'using', 'repair', 'scrap'];
        $device_stat = [];
        foreach ($types as $type) {
            $id = Option::get('inner_devices', 'status', $type);
            $num = $items->where('status', $id)->count();
            $device_stat[$type] = $num;
        }
        $user_auths = UserAuth::where('active', 2)->limit(8)->get();
        $min_order = Order::orderBy('created_at', 'desc')->first();
        $min_year = $min_order ? $min_order->created_at->year : Carbon::now()->year;
        $max_year = Carbon::now()->year;
        $year_options = [];
        for($i = $min_year; $i <= $max_year; ++$i) {
            array_push($year_options, ['id' => $i, 'name' => $i . '年']);
        }
        $month_options = [['id' => '', 'name' => '所有月份']];
        for($i = 1; $i <= 12; ++$i) {
            array_push($month_options, ['id' => $i, 'name' => $i . '月']);
        }
        $select_year = $max_year;
        $select_month = '';
        return view('admin.base.index.welcome', compact('total_device', 'total_project', 'total_money', 'users_total', 'users_auth_total', 'users_not_auth_total', 'device_stat', 'user_auths', 'year_options', 'month_options', 'select_year', 'select_month'));
    }
    
    function createAreaDate(){
        //Base-index-createareadate.do
        header("Content-type:text/html;charset=utf-8");
        $areaObj = new BaseArea();
        $data = $areaObj->getLevel();

        $treeObj = new Tree();
        $treeObj -> init($data);
        $info = $treeObj -> getTree();
        $output = array();
        
        foreach($info AS $key => $val){
            if($val['id'] == '100000') continue;
            $val['level'] = $val['level'] - 1;
            unset($val['grade'], $val['spacer']);
            $output[]= $val;
        }
        
        $str = json_encode($output);
        
        $area_path = public_path() . '/base/js/areadata.js';
        file_put_contents($area_path, $str);
        
        echo $str;exit;
    }
}