<?php
/**
*	此文件是流程模块【rjz_car_apply.任家庄用车申请】对应控制器接口文件。
*/ 
class mode_rjz_car_applyClassAction extends inputAction{

	public $cartype;

	/**
	*	重写函数：保存前处理，主要用于判断是否可以保存
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id 0添加时，大于0修改时
	*	$addbo Boolean 是否添加时
	*	return array('msg'=>'错误提示内容','rows'=> array()) 可返回空字符串，或者数组 rows 是可同时保存到数据库上数组
	*/
	protected function savebefore($table, $arr, $id, $addbo){
		
	}
	
	/**
	*	重写函数：保存后处理，主要保存其他表数据
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id
	*	$addbo Boolean 是否添加时
	*/	
	protected function saveafter($table, $arr, $id, $addbo){
		
	}
    

	public function carList() {
		// $model = new renjiazhuangClassModel();
		// var_dump($model);
		// var_dump('dsadasd');
        // $sqlStr = 'select * from xinhu_rjz_car_list';
		// $rows 	= $this->db->getall($sqlStr);

		$arr = m('renjiazhuang')->getCarList();

		// $arr[] = array("name"=>"第一个下拉框","value"=>"0");
		// $arr[] = array("name"=>"第二个下拉框","value"=>"1");


		
        // $sqlStr = 'select * from xinhu_rjz_car_list';
		// $rows 	= $this->db->getall($sqlStr);
		// var_dump(json_encode($rows));
		// $arr = [];
        // foreach ($rows as $k => $v) {
            
        //     $arr[] = array('name' => $v['type'], 'value'=>$v['']);
        // }

		return $arr;

	}

    // 根据类型获取数量
	public function carNum() {
		$carType = $this->get('custom');
        $carTypeJson = json_decode($carType);
        if(!isset($carTypeJson)) return [];

        $arr = [];
        foreach($carTypeJson as $k=>$v) {
            $arr[] = $k;
        }
        $carTypeStr = str_replace(["[","]"],"",json_encode($arr, JSON_UNESCAPED_UNICODE));
        if ($carType) {
            $data = m('renjiazhuang')->getCarListByType($carTypeStr);
            foreach ($data as $k => $v) {
                $carArr[] = array('name' => $v['car_num'], 'subname'=> '【'.$v['car_type'].'】'.$v['car_state'], 'value'=>$v['id']);
            }
        }
        return $carArr;
	}

    public function carNumAjax() {

        $arr[] = array("name"=>'第一个下拉',"value"=>"0");
        $arr[] = array("name"=>'2',"value"=>"1");
        return $arr;
    }


    // 获取空闲数量
	public function getFreeCarConAjax() {
		$carType = $this->get('car_type');
        $applyStartTime = $this->get('start_time');
        if(isset($carType)) {
            $carTypeArr = [];
            $carType = json_decode($carType, true);

            foreach ($carType as $key => $val) {
                if (is_array($val)) {
                    $carTypeArr[] = $val['name'];
                } else {
                    $carTypeArr[] = $val;
                }
            }
            $carTypeStr = implode('",',$carTypeArr);
            $carTypeStr = str_replace(["[","]"],"",json_encode($carTypeArr, JSON_UNESCAPED_UNICODE));
            $data = m('renjiazhuang')->getCarIdleConByApplyStartTime($carTypeStr, $applyStartTime);
//            $data = m('renjiazhuang')->getCarIdleCon($carTypeStr);

            return returnsuccess($data);
        } else {
            return returnerror('未选择类型！');
        }

	}


    // 更新车辆状态
    public  function updateCarStateAjax() {
        $carname = '"'.str_replace(',','","',$this->get('carname')).'"';
        $carstate = $this->get('carstate');
        $data = m('renjiazhuang')->updateCarState($carname, $carstate);
        return $data;
    }

    public function usecardata() {

        $arr[] = array("name"=>"第一个下拉框","value"=>"0");

        $arr[] = array("name"=>"第二个下拉框","value"=>"1");

        return $arr;
    }

}
