<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\ExpressionCategory as ExpressionCategoryModel;
use app\admin\model\ExpressionCategoryLanguages;
use app\admin\validate\ExpressionCategoryValidate;
use Exception;

/**
 * 常用语分类管理
 */
class ExpressionCategory extends BaseController
{
    
    /**
     * @api {get} /expressioncategory/list 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $language_code = $params['language_code'] ?? $this->lang;

            $query = ExpressionCategoryLanguages::where('language_code', $language_code);            
            if (isset($params['status'])) {
                $query = $query->where('status', $params['status']);
            }
            $count = $query->count();
            $list = $query->where('language_code', $language_code)
                        ->order('weight','desc')
                        ->limit($limit)
                        ->page($page)
                        ->select();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }

    //新增/编辑
    public function update()
    { 
        try {
            $params = (new ExpressionCategoryValidate())->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $status = $params['status'] ?? 1;
            $category_id = $params['category_id'] ?? 0;
            $language_code = $params['language_code'] ?? $this->lang;

            $exists = ExpressionCategoryLanguages::where('name', $params['name'])->where('language_code', $language_code)->find();
            if ($exists ) {
                if (!$id || $id != $exists->id) {
                    return $this->error('分类名称已存在');
                }
            }
            if (!$category_id) {
                $category = ExpressionCategoryModel::create([
                    'created_at' => date('Y-m-d H:i:s'),
                ]);
                $category_id = $category->id;
            }
            if (empty($id)) {
                $categoryLanguage = new ExpressionCategoryLanguages();
            } else {
                $categoryLanguage = ExpressionCategoryLanguages::where('id', $id)->find();
                if (!$categoryLanguage) {
                    return $this->error('分类不存在');
                }
            }
            $categoryLanguage->category_id = $category_id;
            $categoryLanguage->name = $params['name'];
            $categoryLanguage->weight = $params['weight'];
            $categoryLanguage->status = $status;
            $categoryLanguage->language_code = $language_code;
            $categoryLanguage->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

    //设置状态
    public function setStatus()
    {
        try {
            $params = (new ExpressionCategoryValidate())->goCheck('id');
            $info = ExpressionCategoryLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('分类不存在');
            }
            $info->status = $info->status == 0 ? 1 : 0;
            $info->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

    //删除
    public function delete()
    {
        try {
            $params = (new ExpressionCategoryValidate())->goCheck('id');
            $info = ExpressionCategoryLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('分类不存在');
            }
            $category_id = $info->category_id;
            $info->delete();
            if (ExpressionCategoryLanguages::where('category_id', $category_id)->count() == 0) {
                ExpressionCategoryModel::where('id', $category_id)->delete();
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([],'删除成功');
    }

}