<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\GiftT;
use common\models\GiftTypeT;
use common\models\GroupT;
use common\models\StrategyT;
use common\models\UserT;
use Yii;
use yii\web\Response;

class StrategyController extends BaseController
{
    public function actionIndex()
    {
        $request = Yii::$app->request;
        $name = $request->get('name');

        $query = StrategyT::find();
        $items = $query->all();

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

    public function actionEdit()
    {
        $request = Yii::$app->request;
        $id = $request->get('id',0);
        if($id > 0) {
            $info = StrategyT::findOne(['id'=>$id]);
        } else {
            $info = new StrategyT();
            $info->sel_count = 1;
            $info->min_money = 0;
            $info->max_money = 0;
        }

        $group_items = GroupT::getTree();
        $gift_type_items = GiftTypeT::find()->all();
        $sel_gift_items = $info->gifts;
        $gift_ids = array();
        foreach($sel_gift_items as $item) {
            $gift_ids[] = $item->id;
        }

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

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

        if($request->isPost) {
            $id = (int)$request->post('id',0);
            $name = $request->post('name');
            $remark = $request->post('remark');
            $min_money = (float)$request->post('min_money');
            $max_money = (float)$request->post('max_money');
            $sel_count = (int)$request->post('sel_count');
            $group_id = (int)$request->post('group_id');
            $gift_ids = $request->post('gift_ids',array());

            $tran = StrategyT::getDb()->beginTransaction();
            try {
                $strategy_info = StrategyT::findOne(['id'=>$id]);
                if(!$strategy_info) {
                    $strategy_info = new StrategyT();
                    $strategy_info->user_id = $this->my->id;
                }
                $strategy_info->name = $name;
                $strategy_info->remark = $remark;
                $strategy_info->min_money = $min_money;
                $strategy_info->max_money = $max_money;
                $strategy_info->sel_count = $sel_count;
                $strategy_info->group_id = $group_id;
                $strategy_info->save();

                $strategy_info->unlinkAll("gifts",true);
                foreach($gift_ids as $gift_id) {
                    $gift_info = GiftT::findOne(['id'=>$gift_id]);
                    $strategy_info->link('gifts',$gift_info);
                }

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

                $tran->commit();
            } catch(\Exception $e) {
                $tran->rollBack();
                echo $e->getMessage();
            }
        }
        return $result;
    }

    public function actionDelete()
    {
        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 = StrategyT::findOne(['id'=>$id]);
                $row->unlinkAll('gifts',true);
                $row->delete();

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