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

namespace frontend\controllers;

use common\models\MetalPlatePart;
use common\models\MiddleDamageLevel;
use common\models\MiddleParintPrice;
use Yii;
use yii\db\Exception;
use yii\helpers\ArrayHelper;
use yii\web\Response;

class MetalPlateController extends BaseController
{
    public function actionIndex()
    {
        $parts = MetalPlatePart::find()->all();
        return $this->render('index', [
            'parts' => $parts
        ]);
    }

    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', 0);
            $name = $request->post('name');
            if (empty($name)) {
                $result['msg'] = '维修部位不可为空';
                return $result;
            }

            $metal_plate_part = $id > 0 ? MetalPlatePart::find($id) : new MetalPlatePart;

            $metal_plate_part->name = $name;
            $metal_plate_part->save();

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

            return $result;
        }

        //编辑
        $id = $request->get('id');
        $info = $paint_prices = $damage_levels = null;
        if ($id > 0) {
            $info = MetalPlatePart::findOne($id);
            $paint_prices = ArrayHelper::index(ArrayHelper::toArray(MiddleParintPrice::findAll(['metal_id' => $id])), 'min_price');
            $damage_levels = ArrayHelper::index(ArrayHelper::toArray(MiddleDamageLevel::findAll(['metal_id' => $id])), 'level');
        }
        return $this->render('info', [
            'info' => $info,
            'paint_prices' => $paint_prices,
            'damage_levels' => $damage_levels
        ]);
    }

    public function actionSave()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $id = $request->post('id');
        $name = $request->post('name');

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

        if ($id <= 0) return $result;
        if (empty($name)) {
            $result['msg'] = '维修部位不可为空!';
            return $result;
        }

        if (!$request->isAjax) return $result;

        $tran = Yii::$app->db->beginTransaction();
        try {
            $part = MetalPlatePart::findOne($id);
            $part->name = $name;
            $part->save();

            //喷漆价格
            MiddleParintPrice::deleteAll(['metal_id' => $id]);
            $all_price = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 200, 300];

            foreach ($all_price as $price) {
                $insert[] = ['metal_id' => $id, 'min_price' => $price, 'price' => intval($request->post('str_' . $price))];
            }
            if (isset($insert)) {
                if (!Yii::$app->db->createCommand()->batchInsert(MiddleParintPrice::tableName(), ['metal_id', 'min_price', 'price'], $insert)->execute()) throw new Exception('中间表1更新失败!');
            }

            MiddleDamageLevel::deleteAll(['metal_id' => $id]);
            unset($insert);
            $levels = [1, 2, 3, 4];
            foreach ($levels as $level) {
                $insert[] = ['metal_id' => $id, 'level' => $level, 'work_hour' => intval($request->post('damage_' . $level))];
            }
            if (isset($insert)) {
                if (!Yii::$app->db->createCommand()->batchInsert(MiddleDamageLevel::tableName(), ['metal_id', 'level', 'work_hour'], $insert)->execute()) throw new Exception('中间表2更新失败!');
            }

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

            $tran->commit();
        } catch (Exception $e) {
            $result['msg'] = $e->getMessage();
            $tran->rollBack();
        }

        return $result;
    }
}