<?php

namespace frontend\controllers;

use common\models\FixCarCategory;
use common\models\FixItemRecord;
use common\models\FixOutOfStockList;
use common\models\FixPurchaseItem;
use common\models\SDYMiddle;
use common\models\StoreLog;
use common\models\StoreRoom;
use Yii;
use common\models\Message;
use yii\helpers\ArrayHelper;
use yii\web\Response;
use yii\widgets\Pjax;

class PurchaseController extends FixCarBaseController
{
    public function behaviors()
    {
        return [
            [
                'class' => 'yii\filters\ContentNegotiator',
                'only' => ['out-of-stock-list-create','create','update'],
                'formats' => [
                    'application/json' => Response::FORMAT_JSON,
                ]
            ],
            'verbs' => [
                'class' => \yii\filters\VerbFilter::className(),
                'actions' => [
                    'index'                         =>  ['get'],
                    'view'                          =>  ['get'],
                    'out-of-stock-list-create'      =>  ['post'],
                    'create'                        =>  ['post'],
                    'update'                        =>  ['post'],
                ],
            ],
        ];
    }

    public function actionIndex()
    {
        $purchase_item = FixPurchaseItem::find()->all();
        $out_of_stock_list_model = FixOutOfStockList::find();
        $out_of_stock_list = $out_of_stock_list_model->all();
        $store_logs = StoreLog::find()->where(['type'=>3])->orderBy('created_at DESC')->all();
        $out_of_stock_list_count = $out_of_stock_list_model->count();

        return $this->render('index',[
            'purchase_items'=>$purchase_item,
            'out_of_stock_list'=>$out_of_stock_list,
            'out_of_stock_list_count'=>$out_of_stock_list_count,
            'store_logs'=>$store_logs
        ]);
    }
    public function actionView(){
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        $info = null;
        if($id > 0){
            $info = FixPurchaseItem::findOne($id);
        }

        $brands = $this->getBrands();
        $series = $this->getSeries();
        $displacements = $this->getDisplacement();
        $years =$this->getYears();
        $data_lists = $this->getDataList();
        $parents = $this->getParents();

        return $this->render('view',[
            'brands'=>$brands,
            'series'=>$series,
            'displacements'=>$displacements,
            'years'=>$years,
            'info'=>$info,
            'data_lists'=>$data_lists,
            'parents'=>$parents
        ]);
    }
    public function actionCreate(){
        $request = Yii::$app->request;
        $id = $request->post('id', 0);
        $displacement_id = $request->post('displacement_id');
        $series_id = $request->post('series_id');
        $year_id = $request->post('car_year');
        $name = $request->post('name');

        $category = FixCarCategory::findOne(['name' => $name]);
        if (!$category) {
            $result['msg'] = '该配件不存在!';
            return $result;
        }

        if (!SDYMiddle::findOne(['series_id' => $series_id, 'displacement_id' => $displacement_id, 'year_id' => $year_id])) {
            return Message::getMessage(false,'该系列车不存在,请先到基础管理->车辆品牌型号管理添加!');
        }

        $purchase_model = $id > 0 ? FixPurchaseItem::findOne($id) : new FixPurchaseItem();
        $purchase_model->scenario = 'default';
        $purchase_model->attributes = $request->post();
        $purchase_model->get_user = $this->my->id;
        $purchase_model->status = 1;
        if(!$purchase_model->validate()){
            $error = array_values($purchase_model->errors);
            return Message::getMessage(false,$error[0]);
        }
        $purchase_model->save();

        //采购记录
        $store_log = new StoreLog();
        $store_log->op_man = $this->my->name;
        $store_log->op_time = date('Y-m-d H:i:s');
        $store_log->brand_id = $purchase_model->brand_id;
        $store_log->series_id = $purchase_model->series_id;
        $store_log->displacement_id = $purchase_model->displacement_id;
        $store_log->year_id = $purchase_model->car_year;
        $store_log->number = $purchase_model->number;
        $store_log->name = $purchase_model->name;
        $store_log->type = 3;
        if(!$store_log->save()) throw new Exception('记录采购日志失败!');

        return Message::getMessage(true,'操作成功!');
    }
    public function actionUpdate(){
        $request = Yii::$app->request;
        $id = $request->post('id');
        if(intval($id) < 1) return Message::getMessage();

        $item = FixPurchaseItem::findOne($id);
        $item->status = 2;
        $item->save();
        return Message::getMessage(true,'操作成功!');
    }
    public function actionDelete(){}
    public function actionOutOfStockListCreate(){
        $request = Yii::$app->request;
        $id = $request->post('id');

        if($id < 1) return Message::getMessage();

        $record = FixItemRecord::findOne($id);
        $out_of_stock_list = new FixOutOfStockList();
        $out_of_stock_list->name = StoreRoom::findOne($record->item->fk_id)->category->name;
        $out_of_stock_list->brand_id = $record->brand_id;
        $out_of_stock_list->series_id = $record->series_id;
        $out_of_stock_list->displacement_id = $record->displacement_id;
        $out_of_stock_list->car_year = $record->car_year;
        $out_of_stock_list->save();

        return Message::getMessage(true,'操作成功');
    }
}
