<?php

namespace app\admin\controller;

use app\BaseController;
use Exception;
use app\admin\model\Category as CategoryModel;
use app\admin\model\CategoryLanguages;
use app\admin\model\QuestionLanguages;
use app\admin\validate\CategoryValidate;

/**
 * 问题分类管理
 */
class Category extends BaseController
{

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

            $query = CategoryLanguages::where('language_code', $language_code);            
            $count = $query->count();
            $list = $query->where('language_code', $language_code)
                        ->order('weight','desc')
                        ->limit($limit)
                        ->page($page)
                        ->select(); 
            foreach ($list as &$item) {
                $item['num'] = QuestionLanguages::where('category_id', $item->category_id)->count();
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['count' => $count, 'list' => $list]);
    }

    //新增/编辑
    public function update()
    { 
        try {
            $params = (new CategoryValidate())->post()->goCheck('add');
            $id = $this->request->param('id', 0);

            $status = $params['status'] ?? 1;
            $category_id = $params['category_id'] ?? 0;
            $language_code = $params['language_code'] ?? $this->lang;

            $exists = CategoryLanguages::where('name', $params['name'])->where('language_code', $language_code)->find();
            if ($exists ) {
                if (!$id || $id != $exists->id) {
                    return $this->error('分类名称已存在');
                }
            }
            if (!$category_id) {
                $category = CategoryModel::create([
                    'created_at' => date('Y-m-d H:i:s'),
                ]);
                $category_id = $category->id;
                
            }
            if (empty($id)) {
                $categoryLanguage = new CategoryLanguages();
            } else {
                $categoryLanguage = CategoryLanguages::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 CategoryValidate())->sceneId()->goCheck();
            $info = CategoryLanguages::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 CategoryValidate())->sceneId()->goCheck();
            $info = CategoryLanguages::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('分类不存在');
            }
            $category_id = $info->category_id;
            $info->delete();
            if (CategoryLanguages::where('category_id', $category_id)->count() == 0) {
                CategoryModel::where('id', $category_id)->delete();
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([],'删除成功');
    }

}