<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\EmsT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class EmsController extends BaseController
{
    public function actionIndex()
    {
        $request = Yii::$app->request;

        $query = EmsT::find()
            ->orderBy('id DESC');

        return $this->render('index');
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '保存失败';

        if($request->isPost) {
            $car_id = $request->post('car_id',0);
            $order_id = $request->post('order_id');
            $ems_company = $request->post('ems_company');
            $ems_no = $request->post('ems_no');
            $ems_phone = $request->post('ems_phone');
            $ems_remark = $request->post('ems_remark');

            $ems_info = new EmsT();
            $ems_info->car_id = $car_id;
            $ems_info->order_id = $order_id;
            $ems_info->user_id = $this->my->id;
            $ems_info->ems_company = $ems_company;
            $ems_info->ems_no = $ems_no;
            $ems_info->ems_phone = $ems_phone;
            $ems_info->ems_remark = $ems_remark;
            $ems_info->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        return $result;
    }

    public function actionAjaxCarIndex()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_id = $request->get('car_id');
        $order_id = $request->get('order_id');

        $result = array();
        $result['success'] = false;
        $result['msg'] = '读取失败';

        $query = EmsT::find()
            ->where(['car_id'=>$car_id,'order_id'=>$order_id])
            ->orderBy('id ASC');

        $items = $query->all();

        $html = $this->renderPartial('ajax-car-index',[
            'car_id' => $car_id,
            'order_id' => $order_id,
            'items' => $items
        ]);
        $result['success'] = true;
        $result['html'] = $html;
        return $result;
    }
}
