<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\Scheme;
use common\models\UserT;
use Yii;
use yii\web\Response;

class SchemeController extends \yii\web\Controller
{
    public $my = null;

    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 actionIndex()
    {
        $request = Yii::$app->request;
        $name = $request->get('name');

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

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

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

        return $this->render('edit',[
            'info' => $info
        ]);
    }
    public function actionChaxun()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $request = Yii::$app->request;
        $result = array();
        $result['success'] = false;
        $result['msg'] = '查询失败';
        $result['info'] = null;

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

            $info = Scheme::findOne(['id'=>$id]);
            if($info){
                $result['success'] = true;
                $result['info'] = $info;
                $result['msg'] = '查询成功';
            }

        }
        return $result;
    }
    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 = $request->post('id',0);
            $name = $request->post('name');
            $baoer = $request->post('baoer');
            $baofei = $request->post('baofei');

            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }
            if($baoer == '') {
                $result['msg'] = '请输入总保额!';
                return $result;
            }
            if($baofei == '') {
                $result['msg'] = '请输入保费!';
                return $result;
            }
            $row = null;
            if($id > 0) {
                $row = Scheme::findOne(['id'=>$id]);
            } else {
                $row = new Scheme();
            }
            $row->name = $name;
            $row->baoer = $baoer;
            $row->baofei = $baofei;
            $row->save();

            $result['success'] = true;
            $result['msg'] = '保存成功';
        }
        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 = Scheme::findOne(['id'=>$id]);
                $row->delete();

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