<?php

namespace app\admin\controller;

use app\BaseController;
use app\admin\model\Expression as ExpressionModel;
use app\admin\model\ExpressionLanguages;
use app\admin\validate\ExpressionValidate;
use Exception;

/**
 * 常用语管理
 */
class Expression extends BaseController
{

    /**
     * @api {get} /question/list 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();
            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $language_code = $params['language_code'] ?? $this->lang;

            $query = ExpressionLanguages::alias('expression_languages')->join('admin', 'admin.id = expression_languages.operation_id', 'left');
            $query = $query->where('expression_languages.language_code', $language_code);

            if (!empty($params['name'])) {
                $query = $query->where('expression_languages.name', 'like', "%{$params['name']}%");
            }
            if (!empty($params['category_id'])) {
                $query = $query->where('expression_languages.category_id', $params['category_id']);
            }
            if (!empty($params['operation_name'])) {
                $query = $query->where('admin.nickname', 'like', "%{$params['operation_name']}%");
            }
            

            $count = $query->count();
            $list = $query->join('expression_category_languages', 'expression_languages.category_id = expression_category_languages.category_id', 'left')
                        ->field(['expression_languages.*', 'expression_category_languages.name as category_name','admin.nickname as operation_name'])
                        ->order('expression_languages.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 ExpressionValidate())->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $expression_id = $params['expression_id'] ?? 0;
            $language_code = $params['language_code'] ?? $this->lang;
            
            if (empty($id)) {
                $expressionLanguage = new ExpressionLanguages();
            } else {
                $expressionLanguage = ExpressionLanguages::where('id', $id)->find();
                if (!$expressionLanguage) {
                    return $this->error('常用语不存在');
                }
            }

            if (!$expression_id) {
                $expression = ExpressionModel::create([
                    'category_id' => $params['category_id'],
                ]);
                $expression_id = $expression->id;
            }
            $expressionLanguage->expression_id = $expression_id;
            $expressionLanguage->category_id = $params['category_id'];
            $expressionLanguage->name = $params['name'];
            $expressionLanguage->img = $params['img'] ?? '';
            $expressionLanguage->weight = $params['weight'];
            $expressionLanguage->language_code = $language_code;
            $expressionLanguage->operation_id = $this->admin_id;
            // $expressionLanguage->created_at = date('Y-m-d H:i:s');
            $expressionLanguage->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([],'保存成功');
    }

    //设置状态
    public function setStatus()
    {
        try {
            $params = (new ExpressionValidate())->goCheck('id');
            $info = ExpressionLanguages::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 setWeight()
    {
        try {
            $id = $this->request->post('id');
            $weight = $this->request->post('weight');
            if (empty($id) || empty($weight) || !is_array($id)) {
                return $this->error('参数错误');
            }
            ExpressionLanguages::whereIn('id', $id)->update(['weight' => $weight]);
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

    //删除
    public function delete()
    {
        try {
            $params = (new ExpressionValidate())->goCheck('id');
            $info = ExpressionLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('常用语不存在');
            }
            $expression_id = $info->expression_id;
            $info->delete();
            if (ExpressionLanguages::where('expression_id', $expression_id)->count() == 0) {
                ExpressionModel::where('id', $expression_id)->delete();
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '删除成功');
    }

    public function click(){
        try {
            $params = (new ExpressionValidate())->goCheck('id');
            $info = ExpressionLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('常用语不存在');
            }
            $info->used_num = $info->used_num + 1;
            $info->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

}