<?php

namespace app\admin\controller;

use app\BaseController;
use Exception;
use app\admin\model\Complaint as ComplaintModel;
use app\admin\validate\ComplaintValidate;

/**
 * 客服投诉列表
 */
class Complaint extends BaseController
{

    /**
     * @api {get} /complaint/list 列表
     */
    function list()
    {
        try {
            $params = $this->request->param();

            $page = $params['page'] ?? 1;
            $limit = $params['limit'] ?? 15;
            $language_code = $params['language_code'] ?? $this->lang;
            
            $query = ComplaintModel::alias('complaint')
                                    ->join(env('database.DATABASE').'.bot_users user', 'complaint.cs_uid = user.user_id', 'left');
                                    // ->join('user', 'user.user_id=complaint.cs_uid','left');
                                    //->where('complaint.language_code', $language_code);
            if (!empty($params['keyword'])) {
                $query = $query->where('user.realname', 'like', '%'.$params['keyword'].'%')
                    ->whereOr('user.phone', 'like', '%'.$params['keyword'].'%');
            }
            if (!empty($params['complaint_item_id'])) {
                $query = $query->where('complaint.complaint_item_id', $params['complaint_item_id']);
            }
            if (isset($params['status'])) {
                $query = $query->where('complaint.status', $params['status']);
            }
            if (!empty($params['start_time'])) {
                $query = $query->where('complaint.created_at', '>=', $params['start_time'] . ' 00:00:00');
            }
            if (!empty($params['end_time'])) {
                $query = $query->where('complaint.created_at', '<=', $params['end_time'] . ' 23:59:59');
            }
            $count = $query->count();
            $list = $query->with(['complaint_item', 'user', 'operation'])
                        ->field(['complaint.*', 'user.realname','user.phone'])
                        ->order('complaint.created_at','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 ComplaintValidate())->post()->goCheck('edit');
            $id = $this->request->param('id',0);
            $result = $params['result'] ?? '';
            $complaint = ComplaintModel::where('id', $id)->find();
            if (!$complaint) {
                return $this->error('投诉项不存在');
            }
            if ($complaint->status == 1) {
                return $this->error('已处理');
            }
            $complaint->result = $result;
            $complaint->status = 1;
            $complaint->operation_id = $this->admin_id;
            $complaint->save();
        } catch (Exception $e) {
            return $this->error($e->getMessage());
        }
        return $this->success([], '操作成功');
    }

}