<?php

namespace app\admin\controller;

use app\BaseController;
use Exception;
use app\admin\model\ComplaintItem as ComplaintItemModel;
use app\admin\validate\ComplaintItemValidate;

/**
 * 投诉项
 */
class ComplaintItem extends BaseController
{


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

            $language_code = $params['language_code'] ?? $this->lang;
            $query = ComplaintItemModel::where('language_code', $language_code); 
            if (!empty($params['name'])) {
                $query = $query->where('name', 'like', '%'.$params['name'].'%');
            }
            $count = $query->count();
            $list = $query->order('weight','desc')
                        ->limit($limit)
                        ->page($page)
                        ->select();
            $levelList = ComplaintItemModel::levelList($language_code, false);
            foreach($list as &$item) {
                $item->level_name = isset($levelList[$item->level]) ? $levelList[$item->level] : '';
            }
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success(['levelList' => ComplaintItemModel::levelList($language_code, true), 'count' => $count, 'list' => $list]);
    }

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

            $exists = ComplaintItemModel::where('name', $params['name'])->where('language_code', $language_code)->find();
            if ($exists ) {
                if (!$id || $id != $exists->id) {
                    return $this->error('投诉项已存在');
                }
            }
            if (empty($id)) {
                $complaintItem = new ComplaintItemModel();
            } else {
                $complaintItem = ComplaintItemModel::where('id', $id)->find();
                if (!$complaintItem) {
                    return $this->error('投诉项不存在');
                }
            }
            $complaintItem->name = $params['name'];
            $complaintItem->level = $params['level'];
            $complaintItem->weight = $params['weight'];
            $complaintItem->language_code = $language_code;
            $complaintItem->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

    //删除
    public function delete()
    {
        try {
            $params = $params = (new ComplaintItemValidate)->goCheck('id');
            $info = ComplaintItemModel::where('id', $params['id'])->find();
            if (!$info) {
                return $this->error('投诉项不存在');
            }
            $info->delete();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([],'删除成功');
    }

}