<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\school\model\basic\Clazz;
use app\school\model\basic\College;
use app\school\model\basic\Specialty;
use app\school\model\ManystoreShop;
use think\Db;
use think\Lang;

/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Frontend
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    /**
     * 加载语言包
     */
    public function lang()
    {
        header('Content-Type: application/javascript');
        header("Cache-Control: public");
        header("Pragma: cache");

        $offset = 30 * 60 * 60 * 24; // 缓存一个月
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");

        $controllername = input("controllername");
        $this->loadlang($controllername);
        //强制输出JSON Object
        $result = jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
        return $result;
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        return action('api/common/upload');
    }


    public function getCollege($type="college",$college = 0){
        if ($type == 'college'){
            $CollegeVo = new College();
            $colleges = $CollegeVo->field('id as value,college_name as name')->where('school_id',$this->school_id)->select();
            $this->success('',null,$colleges);
        }else if($type == 'specialty'){
            $SpecialtyVo = new Specialty();
            $specialty =  $SpecialtyVo->field('id as value,specialty_name as name')->where('school_id',$this->school_id)->where('college_id',$college)->select();
            $this->success('',null,$specialty);
        }else if($type == 'clazz'){
            $ClazzVo = new Clazz();
            $clazz =  $ClazzVo->field('id as value,clazz_name as name')->where('school_id',$this->school_id)->where('specialty_id',$college)->select();
            $this->success('',null,$clazz);
        }else{
            $this->error("没有可选");
        }
    }

    public function getSubject($type = 'project_lv1_subject',$project_lv1_subject = 0)
    {
        $data = [];
        $query = Db::name('project_subject')->field('id as value,subject_name as name');
        if($project_lv1_subject) {
            $query->where(['pid' => $project_lv1_subject]);
        }else{
            $query->where(['pid' => 0]);
        }
        $data = $query->select();
        $this->success('',null,$data);
    }

    /**
     * 获取学校院校专业树状图
     */
    public function getSchoolList()
    {
        $type = request()->param('activeIndex');
        $model = new ManystoreShop();
        $list = $model->alias('sh')->with([ 'college' ,'college.specialty'])->where(['sh.id' => $this->auth->school_id])->select();
        $arr = [];
        $list = collection($list)->toArray();
        if($list && $type != 2) {
            foreach ($list as $first => $firstVal) {
                $arr[$first]['school_id']     = $firstVal['id'];
                $arr[$first]['title']   = $firstVal['name'];
                $arr[$first]['flag']          = true;
                if(!empty($firstVal['college'])) {
                    foreach ($firstVal['college'] as $second => $secondVal) {
                        $arr[$first]['children'][$second]['college_id'] = $secondVal['id'];
                        $arr[$first]['children'][$second]['title'] = $secondVal['college_name'];
                        $arr[$first]['children'][$second]['flag'] = false;
                        if($type == 0) {
                            if(!empty($secondVal['specialty'])) {
                                foreach ($secondVal['specialty'] as $third => $thirdVal) {
                                    $arr[$first]['children'][$second]['children'][$third]['specialty_id'] = $thirdVal['id'];
                                    $arr[$first]['children'][$second]['children'][$third]['title'] = $thirdVal['specialty_name'];
                                    $arr[$first]['children'][$second]['children'][$third]['flag'] = false;
                                }
                            }
                        }
                        
                    }
                }
            }
            $result = array('code' => 1,"lists" => $arr);
            return json($result);
        }
        $result = array('code' => 0,"lists" => $arr);
        return json($result);
    }

}