<?php
/**
 * Created by PhpStorm.
 * User: liuyingjie
 * Date: 2017/8/30
 * Time: 9:46
 */

namespace frontend\controllers;


use common\libs\MyLib;
use common\models\FixCarCategory;
use common\models\FixCarScale;
use yii\data\Pagination;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use Yii;
use yii\web\Response;

class FixCarScaleController extends BaseController
{
    /**
     *------------------------------------------------------------
     *  列表
     * @param
     * @return view
     * @author liuyingjie
     *------------------------------------------------------------
     */
    public function actionIndex(){
        $request = Yii::$app->request;
        $session = Yii::$app->session;

        $render['pid'] = $pid = $request->get('pid',0);
        $render['id'] = $id = $request->get('id',0);
        $render['page'] = $page = $request->get('page',0);
        $render['parents'] = ($p_res = FixCarCategory::findAll(['pid'=>0])) ? $p_res : [];

        $query = FixCarScale::find();

        $render['sons'] = [];
        if($pid > 0 ){
            $subs = FixCarCategory::findAll(['pid'=>$pid]);
            $subs = array_column(ArrayHelper::toArray($subs),'id');
            $subs = implode(',',$subs);
            $query->andWhere('category_id IN ('.$subs.')');
        }
        if($id > 0 ) {
            $query->andWhere('category_id='.$id);
            $render['sons'] = FixCarCategory::findAll(['pid'=>$pid]);
        }

        $sql = $query->createCommand()->rawSql;
        $total = $query->count();
        $session->remove('fix-car-scale-index');
        $session->set('fix-car-scale-index',$sql);

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

        $query = $query->offset($pagination->offset)->limit($pagination->limit);
        $render['scales'] = $query->all();

        $render['page_info'] = MyLib::getPageInfo($pagination);

        return $this->render('index',$render);
    }
    public function actionInfo(){
        $request = Yii::$app->request;

        //添加、编辑
        if($request->isAjax){
            Yii::$app->response->format = Response::FORMAT_JSON;

            $result = [];
            $result['success'] = false;
            $result['msg'] = '操作失败!';

            $id = $request->post('id');
            $category_id = $request->post('category_id');
            $scale = $request->post('scale');

            $fix_car_scale = $id > 0 ? FixCarScale::findOne($id) : new FixCarScale();
            $fix_car_scale->category_id = $category_id;
            $fix_car_scale->scale = $scale;
            if(!$fix_car_scale->validate()){
                $errors = array_values($fix_car_scale->errors);
                $result['msg'] = $errors[0];
                return $result;
            }

            try{
                $fix_car_scale->save();
            }catch(Exception $e){
                $result['msg'] = '每个配件只能对应一个比例!';
                return $result;
            }

            $result['success'] = true;
            $result['msg'] = '操作成功!';
            return $result;
        }

        //详情
        $id = $request->get('id',0);

        if($id > 0 ){
            $render['info'] = $info = FixCarScale::findOne($id);
            $render['sons'] = FixCarCategory::findAll(['pid'=>$info->category->pid]);
        }
        $render['parents'] = ($p_res = FixCarCategory::findAll(['pid'=>0])) ? $p_res : [];
        return $this->render('info',$render);
    }
}