<?php

namespace frontend\controllers;

use common\Libs\MyLib;
use common\models\UserT;
use Yii;
use common\models\ScooterListT;
use yii\data\Pagination;
use yii\web\Response;

class ScooterController extends BaseController
{
    public $result = [
        'success' => false,
        'msg' => '操作失败'
    ];

    public function actionIndex()
    {
        $request = Yii::$app->request;

        $page = $request->get('page', 1);
        $car_no = $request->get('car_no');
        $car_man = $request->get('car_man');
        $is_lend = $request->get('is_lend');

        $query = ScooterListT::find();

        if($car_no != '')
            $query = $query->andWhere('car_no like :car_no', ['car_no' => $car_no]);

        if($car_man != '')
            $query = $query->andWhere('car_man like :car_man', ['car_man' => $car_man]);

        if($is_lend != '')
            $query = $query->andWhere(['is_lend' => $is_lend]);

        $total = $query->count();

        $pagination = new Pagination(['totalCount' => $total, 'pageSize' => 20]);
        $pagination->setPage($page-1);

        $query = $query->offset($pagination->offset)->limit($pagination->limit);

        $query = $query->orderBy('lend_at DESC');
        $items = $query->all();

        $page_info = MyLib::getPageInfo($pagination);

        return $this->render('index', [
            'items' => $items,
            'page_info' => $page_info,
            'car_no' => $car_no,
            'car_man' => $car_man,
            'page' => $page
        ]);
    }

    //添加 修改
    public function actionEdit()
    {
        $request = Yii::$app->request;

        $id = $request->get('id', 0);
        $back_params = $request->get('back_params');

        if($id > 0)
            $query = ScooterListT::findOne(['id' => $id]);
        else
            $query = new ScooterListT();

        return $this->render('edit', [
            'item' => $query,
            'back_params' => $back_params
        ]);
    }

    //保存修改
    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $id = $request->post('id', 0);
        $car_no = $request->post('car_no');
        $car_man = $request->post('car_man');
        $check_car_at = $request->post('check_car_at');
        $is_lend = $request->post('is_lend', 0);

        if($id > 0)
            $query = ScooterListT::findOne(['id' => $id]);
        else
            $query = new ScooterListT();

        $query->car_no = $car_no;
        $query->car_man = $car_man;
        $query->check_car_at = $check_car_at;
        $query->is_lend = $is_lend;

        if($query->save()){
            $this->result = [
                'success' => true,
                'msg' => '操作成功'
            ];
        }

        return $this->result;
    }

    /**
     * 删除
     * xzz
     * 2017-12-28
     */
    public function actionDel()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;

        $id = $request->post('id', 0);

        if($id > 0){
            $query = ScooterListT::findOne(['id' => $id]);

            if($query->delete()){
                $this->result = [
                    'success' => true,
                    'msg' => '删除成功'
                ];
            }
        }

        return $this->result;
    }
}