<?php

namespace frontend\controllers;

use common\libs\MyLib;
use common\models\MeetT;
use Yii;
use yii\web\Response;

class MeetController extends \yii\web\Controller
{
    public $my = null;
    public $enableCsrfValidation = false;
    public $layout = 'blue-main';
    private $_STATE = 1;

    public function init()
    {
        parent::init();

        $cookie = Yii::$app->request->cookies;
        $user_id = MyLib::encrypt($cookie->get('aid'),'DECODE');

        if($user_id != 0)
        {
            $this->my = MeetT::findOne(['id'=>$user_id]);
        } else {
            Yii::$app->response->redirect('/common/login')->send();
            exit;
        }
    }

    //列表页
    public function actionList()
    {
        return $this->render('list');
    }

    public function actionListJson()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $items = MeetT::find()
            ->where('state=1')
            ->orderBy('id asc')
            ->all();

        $data = [];
        $data['total'] = count($items);
        $data['rows'] = [];
        foreach($items as $item) {
            $row = $item->toArray();
            $row['type_name'] = $item->getShowType();
            $data['rows'][] = $row;
        }

        return $data;
    }

    //修改，添加页面
    public function actionEdit()
    {
        
        $request = Yii::$app->request;
        $id = $request->get('id');
        if($id > 0) {
            $info = MeetT::findOne(['id'=>$id]);
        } else {
            $info = new MeetT();
        }
        return $this->render('edit',[
            'info' => $info,
        ]);
    }
    
    //ajax修改，新增
    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');
            $code = $request->post('code');
            $mtype = $request->post('mtype');
            
            if($code == '') {
                $result['msg'] = '请输入状态码!';
                return $result;
            }
            if($mtype == 0) {
                $result['msg'] = '请选择类型!';
                return $result;
            }
            if($name == '') {
                $result['msg'] = '请输入名称!';
                return $result;
            }
            
            if($id > 0) {
                $row = MeetT::findOne(['id'=>$id]);
            } else {
                $row = new MeetT();
            }
            $row->code = $code;
            $row->mtype = $mtype;
            $row->name = $name;
            $row->state = 1;
            $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');
            if($id > 0){
                $row = MeetT::findOne(['id'=>$id]);
                $row->state = 0;
                $row->save();
                
                $result['success'] = true;
                $result['msg'] = '保存成功';
            }
            
            return $result;
        }
    }

}
