<?php

namespace app\admin\controller\client;

use app\admin\service\member\MemberService;
use app\controller\Curd;
use app\model\ClientContent;
use app\model\MemberRole;
use support\Request;
use support\Response;

class FeedbackController extends Curd
{
    public function __construct()
    {
        $this->model = new ClientContent();
    }

    public function select(Request $request): Response
    {
        $searchTimes = $request->get('client_content_addtimes', []);

        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'client_content_addtimes';
        $where['client_content_page'] = 'FEEDBACK';

        if (!empty($searchTimes)) {
            $searchTimes[0] = !empty($searchTimes[0]) ? strtotime($searchTimes[0]) : time();
            $searchTimes[1] = !empty($searchTimes[1]) ? strtotime($searchTimes[1]) : time();

            $where['client_content_addtimes'] = $searchTimes;
        }

        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    protected function doSelect(array $where, string $field = null, string $order = 'desc')
    {
        $model = $this->model->with([
            'member' => function ($query) {
                $query->select('member_id', 'member_mobile');
            },
            'cert' => function ($query) {
                $query->select('join_cert_member_id', 'member_cert_name');
            },
            'info' => function ($query) {
                $query->select('join_info_member_id', 'member_info_nickname');
            }
        ]);
        foreach ($where as $column => $value) {
            if (is_array($value)) {
                if ($value[0] === 'like' || $value[0] === 'not like') {
                    $model = $model->where($column, $value[0], "%$value[1]%");
                } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
                    $model = $model->where($column, $value[0], $value[1]);
                } elseif ($value[0] == 'in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereIn($column, $valArr);
                } elseif ($value[0] == 'not in' && !empty($value[1])) {
                    $valArr = $value[1];
                    if (is_string($value[1])) {
                        $valArr = explode(",", trim($value[1]));
                    }
                    $model = $model->whereNotIn($column, $valArr);
                } elseif ($value[0] == 'null') {
                    $model = $model->whereNull($column);
                } elseif ($value[0] == 'not null') {
                    $model = $model->whereNotNull($column);
                } elseif ($value[0] !== '' || $value[1] !== '') {
                    $model = $model->whereBetween($column, $value);
                }
            } else {
                $model = $model->where($column, $value);
            }
        }
        if ($field) {
            $model = $model->orderBy($field, $order);
        }
        return $model;
    }

    public function afterQuery($items)
    {
        foreach ($items as &$item) {
            $mobile = !empty($item['member']) && !empty($item['member']['member_mobile']) ? $item['member']['member_mobile'] : '';
            $certName = !empty($item['cert']) && !empty($item['cert']['member_cert_name']) ? $item['cert']['member_cert_name'] : '';
            $nickname = !empty($item['info']) && !empty($item['info']['member_info_nickname']) ? $item['info']['member_info_nickname'] : '';

            $item['member_name'] = MemberService::getMemberName($mobile, $certName, $nickname);
            if (!empty($item['client_content_extend_json'])) {
                $clientContentExtendJson = json_decode($item['client_content_extend_json'], true);
                if (isset($clientContentExtendJson['feedback'])) {
                    $item['feedback_content'] = mb_strlen($clientContentExtendJson['feedback']) > 30 ? mb_substr($clientContentExtendJson['feedback'], 0, 30) . '...' : $clientContentExtendJson['feedback'];
                }
            }

            unset($item['member'], $item['info'], $item['cert']);
        }

        return $items;
    }

    public function info(Request $request): Response
    {
        $feedbackId = $request->get('feedback_id');
        if (!$feedbackId) {
            return json_fail('参数异常');
        }

        $feedback = ClientContent::with([
            'member' => function ($query) {
                $query->select('member_id', 'member_mobile', 'join_member_role_id', 'member_is_owner', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
            },
            'cert' => function ($query) {
                $query->select('join_cert_member_id', 'member_cert_name');
            },
            'info' => function ($query) {
                $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
            }
        ])->where('client_content_id', $feedbackId)
            ->first();
        if (empty($feedback)) {
            return json_success('');
        }

        $feedback = $feedback->toArray();
        $feedback['member']['member_mobile'] = !empty($feedback['member']) && !empty($feedback['member']['member_mobile']) ? $feedback['member']['member_mobile'] : '';
        $feedback['cert']['member_cert_name'] = !empty($feedback['cert']) && !empty($feedback['cert']['member_cert_name']) ? $feedback['cert']['member_cert_name'] : '';
        $feedback['info']['member_info_nickname'] = MemberService::getNickname(!empty($feedback['info']) ? $feedback['info']['member_info_nickname'] : '', $feedback['member']['member_mobile']);

        if (!empty($feedback['client_content_extend_json'])) {
            $clientContentExtendJson = json_decode($feedback['client_content_extend_json'], true);
            $feedback['feedback_content'] = $clientContentExtendJson['feedback'] ?? '';
        }
        if (!empty($feedback['member']) && !empty($feedback['member']['join_member_role_id'])) {
            $feedback['role_name'] = MemberRole::where('member_role_id', $feedback['member']['join_member_role_id'])->value('member_role_name');
        }

        return json_success('success', $feedback);
    }
}