<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\BrandT;
use common\models\CarGiftT;
use common\models\CarLogT;
use common\models\CarT;
use common\models\CarTypeT;
use common\models\DirectionT;
use common\models\DisplacementT;
use common\models\FixCarT;
use common\models\FixItemT;
use common\models\FixTypeUserT;
use common\models\RangeT;
use common\models\SeriesT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class FixCarController extends BaseController
{
    public $my = null;
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = UserT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    public function actionMySearch() {
        return $this->render('my-search');
    }

    public function actionMySearchResult()
    {
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $engine_no = $request->get('engine_no');
        $car_frame_no = $request->get('car_frame_no');
        $car_man = $request->get('car_man');
        $car_phone = $request->get('car_phone');

        $query = CarT::find();
        if($car_no != '') {
            $query = $query->andWhere('car_no like "'.$car_no.'"');
        }
        if($engine_no != '') {
            $query = $query->andWhere('engine_no like "'.$engine_no.'"');
        }
        if($car_frame_no != '') {
            $query = $query->andWhere('car_frame_no like "'.$car_frame_no.'"');
        }
        if($car_man != '') {
            $query = $query->andWhere('car_man like "'.$car_man.'"');
        }
        if($car_phone != '') {
            $query = $query->andWhere('phone like "'.$car_phone.'"');
        }
        $items = $query->asArray()->all();

        return $this->render('my-search-result',[
            'items' => $items
        ]);
    }

    public function actionMyList() {
        return $this->render('my-list');
    }

    public function actionMyListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find()
            ->where('status=0');
        $user_ids = $this->getChildrenUserIDs();
        $query = $query->andWhere(['in','user_id',$user_ids]);

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

        $total = $query->count();
        $query = $query->orderBy('id DESC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionMyListFinish() {
        return $this->render('my-list-finish');
    }

    public function actionMyListFinishJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find()
            ->where('status>0');
        $user_ids = $this->getChildrenUserIDs();
        $query = $query->andWhere(['in','user_id',$user_ids]);

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

        $total = $query->count();

        $query = $query->orderBy('status ASC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

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

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

            if($id > 0) {
                $row = FixCarT::findOne(['id'=>$id]);
                $row->delete();
                FixItemT::deleteAll('fix_id='.$id);

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

    public function actionMyListEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $car_id = $request->get('car_id',0);
        $status = $request->get('status',0);
        $type = $request->get('type',0);
        $gift_enable = $request->get('gift_enable',0);
        if($id > 0) {
            $info = FixCarT::findOne(['id'=>$id]);
        } else {
            $info = new FixCarT();
        }
        if($car_id > 0) {
            $car_info = CarT::find()
                ->where(['id'=>$car_id])
                ->orderBy('register_date DESC')
                ->one();
            $info->car_id = $car_info->id;
            $info->car_no = $car_info->car_no;
            $info->engine_no = $car_info->engine_no;
            $info->car_frame_no = $car_info->car_frame_no;
            $info->id_man = $car_info->id_man;
            $info->id_man_phone = $car_info->phone;
            $info->link_man = $car_info->car_man;
            $info->link_phone = $car_info->phone;
            $info->company = $car_info->company;
            $info->brand_id = $car_info->brand_id;
            $info->series_id = $car_info->series_id;
            $info->displacement_id = $car_info->displacement_id;
            $info->car_year = $car_info->car_year;
            $info->status = $status;
        }
        if($car_id == 0) {
            $car_id = $info->car_id;
            $car_info = $info->car;
        }

        //礼品
        $gift_items = CarGiftT::find()
            ->with('user')
            ->with('gift')
            ->where('car_id='.$car_info->id)
            ->orderBy('strategy_id DESC')
            ->asArray()
            ->all();

        $direction_items = DirectionT::find()->all();
        $range_items = RangeT::find()->all();
        //司机
        $driver_items = UserT::find()
            ->where('group_id=34 and is_leave=0')
            ->orderBy('username ASC')
            ->all();
        //品牌
        $brand_items = BrandT::find()->all();
        //车系
        $series_items = SeriesT::find()
            ->where('brand_id='.intval($info->brand_id))
            ->all();
        //排量
        $displacement_items = DisplacementT::find()
            ->where('series_id='.intval($info->series_id))
            ->all();

        //读取记录
        $log_items = CarLogT::find()
            ->where('car_id='.$info->car_id.' and type=2')
            ->orderBy('id DESC')
            ->asArray()
            ->all();

        return $this->render('my-list-edit',[
            'info' => $info,
            'gift_items' => $gift_items,
            'direction_items' => $direction_items,
            'range_items' => $range_items,
            'log_items' => $log_items,
            'driver_items' => $driver_items,
            'brand_items' => $brand_items,
            'series_items' => $series_items,
            'displacement_items' => $displacement_items,
            'type' => $type,
            'gift_enable' => $gift_enable
        ]);
    }

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

        if($request->isPost) {
            $id = $request->post('id',0);
            $car_id = $request->post('car_id');
            $car_no = $request->post('car_no');
            $engine_no = $request->post('engine_no');
            $car_frame_no = $request->post('car_frame_no');
            $new_price = $request->post('new_price');
            $brand_id = $request->post('brand_id');
            $series_id = $request->post('series_id');
            $displacement_id = $request->post('displacement_id');
            $car_year = $request->post('car_year');
            $id_man = $request->post('id_man');
            $id_phone = $request->post('id_phone');
            $link_man = $request->post('link_man');
            $link_phone = $request->post('link_phone');
            $company = $request->post('company');
            $insurer1_no = $request->post('insurer1_no');
            $insurer2_no = $request->post('insurer2_no');
            $receive_date = $request->post('receive_date');
            $receive_type = $request->post('receive_type');
            $accident_type = $request->post('accident_type');
            $responsibility = $request->post('responsibility');
            $replace_car_id = $request->post('replace_car_id');
            $driver_id = $request->post('driver_id');
            $direction_id = $request->post('direction_id');
            $range_id = $request->post('range_id');
            $address = $request->post('address');
            $report_no = $request->post('report_no');
            $receive_mileage = $request->post('receive_mileage');
            $receive_oil = $request->post('receive_oil');
            $factory_mileage = $request->post('factory_mileage');
            $factory_oil = $request->post('factory_oil');
            $finish_date = $request->post('finish_date');
            $remark = $request->post('remark');
            $sys_remark = $request->post('sys_remark');
            $status = $request->post('status');

            $baoan_man = $request->post('baoan_man');
            $baoan_time = $request->post('baoan_time');
            $baoan_address = $request->post('baoan_address');
            $chuxian_time = $request->post('chuxian_time');
            $shigu_info = $request->post('shigu_info');
            $lipei_company = $request->post('lipei_company');
            $zhuche_money = $request->post('zhuche_money');
            $sanzhe_money = $request->post('sanzhe_money');
            $wusun_money = $request->post('wusun_money');
            $tuoche = $request->post('tuoche');
            $tuoche_money = $request->post('tuoche_money');
            $dingsun_hour_money = $request->post('dingsun_hour_money');
            $dingsun_huanjian_money = $request->post('dingsun_huanjian_money');
            $peifu_bili = $request->post('peifu_bili');
            $peifu_money = $request->post('peifu_money');
            $kehu_money = $request->post('kehu_money');

            if($car_no == '') {
                $result['msg'] = '请输入车牌号码';
                return $result;
            }
            if($accident_type == '') {
                $result['msg'] = '请选择事故类型';
                return $result;
            }
            if($id_man == '') {
                $result['msg'] = '请输入被保险人';
                return $result;
            }
            if($id_phone == '') {
                $result['msg'] = '请输入被保险人电话';
                return $result;
            }
            if($link_man == '') {
                $result['msg'] = '请输入联系人';
                return $result;
            }
            if($link_phone == '') {
                $result['msg'] = '请输入联系人电话';
                return $result;
            }
            if($company == '') {
                $result['msg'] = '请输入保险公司';
                return $result;
            }
            if($receive_date == '') {
                $result['msg'] = '请输入接车日期';
                return $result;
            }
            if($receive_type == '') {
                $result['msg'] = '请选择接车方式';
                return $result;
            }
            if($responsibility == '') {
                $result['msg'] = '请选择责任';
                return $result;
            }
            if($brand_id == 0) {
                $result['msg'] = '请选择品牌';
                return $result;
            }
            if($address == '') {
                $result['msg'] = '请输入接车地址';
                return $result;
            }
            if($car_year == ''){
                $result['msg'] = '请输入年份';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = FixCarT::findOne(['id'=>$id]);
            } else {
                $row = new FixCarT();
                $row->submit_date = date('Y-m-d');
                $row->user_id = $this->my->id;
                $row->car_id = $car_id;
            }
            $row->car_no = $car_no;
            $row->engine_no = $engine_no;
            $row->car_frame_no = $car_frame_no;
            $row->new_price = $new_price;
            $row->brand_id = $brand_id;
            $row->series_id = $series_id;
            $row->displacement_id = $displacement_id;
            $row->car_year = $car_year;
            $row->id_man = $id_man;
            $row->id_man_phone = $id_phone;
            $row->link_man = $link_man;
            $row->link_phone = $link_phone;
            $row->company = $company;
            $row->insurer1_no = $insurer1_no;
            $row->insurer2_no = $insurer2_no;
            $row->receive_date = $receive_date;
            $row->receive_type = $receive_type;
            $row->accident_type = $accident_type;
            $row->responsibility = $responsibility;
            $row->replace_car_id = $replace_car_id;
            $row->driver_id = $driver_id;
            $row->direction_id = $direction_id;
            $row->range_id = $range_id;
            $row->address = $address;
            $row->report_no = $report_no;
            $row->receive_mileage = $receive_mileage;
            $row->receive_oil = $receive_oil;
            $row->factory_mileage = $factory_mileage;
            $row->factory_oil = $factory_oil;
            $row->finish_date = $finish_date;
            $row->remark = $remark;
            if($row->status != $status && $status > 0) {
                $log_info = new CarLogT();
                $log_info->car_id = $car_id;
                $log_info->op_time = time();
                $log_info->op_man = $this->my->getShowName();
                $log_info->group_name = $this->my->group?$this->my->group->getPath():'';
                $log_info->type = 2;
                if($status == 1) {
                    $log_info->remark = '提交到维修调度';
                }
                if($status == 2) {
//                    $log_info->remark = '提交到司机接车';
                    $log_info->remark = '提单完成';
                }
                if($status == 3) {
                    $log_info->remark = '提交到前台派工';
                }
                if($status == 4) {
                    $log_info->remark = '提交到查勘定损';
                }
                if($status == 5) {
                    $log_info->remark = '提交到维修部';
                }
                $log_info->info = $sys_remark;
                if(!$log_info->save()) {
                    var_dump($log_info->errors);
                }
                //如果是客户自上门，则直接到前台派工
//                if($status == 2) {
//                    if($receive_type == '客户自送') {
//                        $status = 3;
//                    }
//                }
            }
            $row->status = $status;

            $row->baoan_man = $baoan_man;
            $row->baoan_time = $baoan_time;
            $row->baoan_address = $baoan_address;
            $row->chuxian_time = $chuxian_time;
            $row->shigu_info = $shigu_info;
            $row->lipei_company = $lipei_company;
            $row->zhuche_money = floatval($zhuche_money);
            $row->sanzhe_money = floatval($sanzhe_money);
            $row->wusun_money = floatval($wusun_money);
            $row->tuoche = $tuoche;
            $row->tuoche_money = floatval($tuoche_money);
            $row->dingsun_hour_money = floatval($dingsun_hour_money);
            $row->dingsun_huanjian_money = floatval($dingsun_huanjian_money);
            $row->peifu_bili = $peifu_bili;
            $row->peifu_money = floatval($peifu_money);
            $row->kehu_money = floatval($kehu_money);

            if(!$row->save()) {
                var_dump($row->errors);
                exit;
            }

            $result['success'] = true;
            $result['msg'] = '保存成功';
            $result['fix_id'] = $row->id;
        }
        return $result;
    }

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

        if($request->isPost) {
            $id = $request->post('id',0);
            $car_gift_ids = $request->post('car_gift_ids');

            if($id == 0 ) {
                $result['msg'] = '请输入保存维修信息!';
                return $result;
            }

            foreach($car_gift_ids as $car_gift_id) {
                $car_gift_info = CarGiftT::findOne(['id'=>$car_gift_id]);
                if($car_gift_info) {
                    $car_gift_info->status = 3;
                    $car_gift_info->save();
                }
            }

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

    public function actionAddItem()
    {
        $request = Yii::$app->request;
        if($request->isPost) {
            Yii::$app->response->format = Response::FORMAT_JSON;
            $fix_id = $request->post('fix_id');
            $name = $request->post('name');
            $price = floatval($request->post('price'));
            $count = intval($request->post('count'));
            $type = $request->post('type');
            $big_type_id = $request->post('big_type_id');
            $small_type_id = $request->post('small_type_id');
            $fix_remark = $request->post('fix_remark');

            $item_info = new FixItemT();
            $item_info->fix_id = $fix_id;
            $item_info->name = $name;
            $item_info->price = $price;
            $item_info->count = $count;
            $item_info->type = $type;
            $item_info->total = $price * $count;
            $item_info->big_type_id = $big_type_id;
            $item_info->small_type_id = $small_type_id;
            $item_info->fix_remark = $fix_remark;
            $item_info->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
            return $result;
        }
        $fix_id = $request->get('fix_id');
        $fix_info = FixCarT::findOne(['id'=>$fix_id]);
        return $this->render('add-item',[
            'fix_info' => $fix_info
        ]);
    }

    public function actionAjaxItemList()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result['success'] = true;
        $result['msg'] = '读取失败';

        $fix_id = $request->get('fix_id');
        $big_type_id = $request->get('big_type_id');
        $type1_items = FixItemT::find()
            ->where('fix_id='.$fix_id.' and big_type_id='.$big_type_id.' and small_type_id=1')
            ->asArray()
            ->all();
        $type2_items = FixItemT::find()
            ->where('fix_id='.$fix_id.' and big_type_id='.$big_type_id.' and small_type_id=2')
            ->asArray()
            ->all();
        $type3_items = FixItemT::find()
            ->where('fix_id='.$fix_id.' and big_type_id='.$big_type_id.' and small_type_id=3')
            ->asArray()
            ->all();
        $type4_items = FixItemT::find()
            ->where('fix_id='.$fix_id.' and big_type_id='.$big_type_id.' and small_type_id=4')
            ->asArray()
            ->all();
        $type5_items = FixItemT::find()
            ->where('fix_id='.$fix_id.' and big_type_id='.$big_type_id.' and small_type_id=5')
            ->asArray()
            ->all();

        return MyLib::ok3([
            'type1' => ['更换清单', $type1_items],
            'type2' => ['维修清单', $type2_items],
            'type3' => ['待定清单', $type3_items],
            'type4' => ['配件清单', $type4_items],
            'type5' => ['工时清单', $type5_items]
        ], '操作成功');
    }

    public function actionStep1List()
    {
        return $this->render('step1-list');
    }

    public function actionStep1ListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status=1');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $total = $query->count();
        $query = $query->orderBy('status ASC,receive_date ASC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['brand'] = $item->brand?$item->brand->name:'';
            $row['series'] = $item->series?$item->series->name:'';
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionStep1ListFinish()
    {
        return $this->render('step1-list-finish');
    }

    public function actionStep1ListFinishJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status>1');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $total = $query->count();
        $query = $query->orderBy('status ASC,updated_at DESC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['user'] = $item->getUser()->asArray()->one();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionPrintReceive()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $info = FixCarT::findOne(['id'=>$id]);
        $car_info = $info->car;

        $direction_items = DirectionT::find()->all();
        $range_items = RangeT::find()->all();
        $brand_items = BrandT::find()->all();
        $series_items = SeriesT::find()
            ->where('brand_id='.intval($car_info->brand_id))
            ->all();
        $displacement_items = DisplacementT::find()
            ->where('series_id='.intval($car_info->series_id))
            ->all();

        return $this->render('print_receive',[
            'info' => $info,
            'car_info' => $car_info
        ]);
    }

    public function actionStep2List()
    {
        return $this->render('step2-list');
    }

    public function actionStep2ListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status=2');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }

        $total = $query->count();
        $query = $query->orderBy('status ASC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['brand'] = $item->brand?$item->brand->name:'';
            $row['series'] = $item->series?$item->series->name:'';
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionStep2ListFinish()
    {
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status>2');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $query = $query->orderBy('status ASC, updated_at DESC');
        $items = $query->all();

        return $this->render('step2-list-finish',[
            'items' => $items,
            'car_no' => $car_no
        ]);
    }

    public function actionStep3List()
    {
        return $this->render('step3-list');
    }

    public function actionStep3ListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status>2');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }

        $total = $query->count();
        $query = $query->orderBy('status ASC,updated_at DESC')->offset($offset)->limit($limit);
        $items = $query->all();

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['brand'] = $item->brand?$item->brand->name:'';
            $row['series'] = $item->series?$item->series->name:'';
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionStep4List()
    {
        return $this->render('step4-list');
    }

    public function actionStep4ListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');
        $offset = $request->get('offset',0);
        $limit = $request->get('limit', 10);

        $query = FixCarT::find();
        $query = $query->where('status>3');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $total = $query->count();
        $query = $query->orderBy('status ASC')->offset($offset)->limit($limit);
        $items = $query->all();

        $lists = array();
        foreach($items as $item) {
            $count = $item->getItems()->where('big_type_id=1')->count();
            if($count > 0) {
                $lists[] = $item;
            }
        }

        $data = [];
        $data['total'] = $total;
        $data['rows'] = [];
        foreach($lists as $item) {
            $row = $item->toArray();
            $row['brand'] = $item->brand->name;
            $row['series'] = $item->series->name;
            $row['show_name'] = $item->user->getShowName();
            $row['status_txt'] = $item->getStatus();
            $data['rows'][] = $row;
        }
        return $data;
    }

    public function actionStep5List()
    {
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');

        $query = FixCarT::find();
        $query = $query->where('status>3');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $query = $query->orderBy('status ASC');
        $items = $query->all();

        return $this->render('step5-list',[
            'items' => $items,
            'car_no' => $car_no
        ]);
    }

    public function actionStep6List()
    {
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');

        $query = FixCarT::find();
        $query = $query->where('status>4');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $query = $query->orderBy('status ASC');
        $items = $query->all();

        return $this->render('step6-list',[
            'items' => $items,
            'car_no' => $car_no
        ]);
    }

    public function actionStep7List()
    {
        $request = Yii::$app->request;
        $car_no = $request->get('car_no');

        $query = FixCarT::find();
        $query = $query->where('status>1');
        if($car_no != '') {
            $query = $query->andWhere(['like','car_no',$car_no]);
        }
        $query = $query->orderBy('status ASC');
        $items = $query->all();

        return $this->render('step7-list',[
            'items' => $items,
            'car_no' => $car_no
        ]);
    }

    public function actionTypeIndex()
    {
        $request = Yii::$app->request;
        $name = $request->get('name');

        $query = FixTypeUserT::find();
        if($name != '') {
            $query = $query->andWhere(['like','name',$name]);
        }
        $items = $query->all();

        return $this->render('type-index',[
            'items' => $items,
            'name' => $name
        ]);
    }

    public function actionTypeEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = FixTypeUserT::findOne(['id'=>$id]);
        } else {
            $info = new FixTypeUserT();
        }

        return $this->render('type-edit',[
            'info' => $info
        ]);
    }

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

        if($request->isPost) {
            $id = $request->post('id',0);
            $name = $request->post('name');

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }

            $row = null;
            if($id > 0) {
                $row = FixTypeUserT::findOne(['id'=>$id]);
            } else {
                $row = new FixTypeUserT();
            }
            $row->name = $name;
            $row->save();

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

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

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

            if($id > 0) {
                $row = FixTypeUserT::findOne(['id'=>$id]);
                $row->delete();

                $result['success'] = true;
                $result['msg'] = '删除成功';
            }
        }
        return $result;
    }

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

        if($request->isPost) {
            $id = $request->post('id',0);
            $return_status_id = $request->post('return_status_id',0);
            $return_remark = $request->post('return_remark');

            $tran = FixCarT::getDb()->beginTransaction();
            try {
                $row = FixCarT::findOne(['id'=>$id]);
                if(empty($row)) {
                    $result['msg'] = '错误!';
                    return $result;
                }

                $row->status = 0;
                $row->return_status = $return_status_id;
                $row->return_remark = $return_remark;
                $row->return_time = time();
                $row->save();

                $tran->commit();

            } catch(\Exception $e) {
                $tran->rollBack();
                throw $e;
            }

            $result['success'] = true;
            $result['msg'] = '退回成功';
        }
        return $result;
    }
}
