<?php

namespace app\controller;

use app\model\Customer;
use app\model\Follow;
use app\util\Util;
use config\constants\Constants;
use Google\Cloud\Speech\V2\Client\SpeechClient;
use Google\Cloud\Speech\V2\RecognitionConfig;
use support\Request;

class FollowController
{
    /**
     * 客户分级
     * @param Request $request
     * @return \support\Response
     */
    public function getFollowType(Request $request)
    {
        $grade = [];
        foreach (Constants::FOLLOW as $key => $value) {
            $grade[] = [
                'status' => $key,
                'text' => $value,
            ];
        }
        return Util::success($grade);
    }


    /**
     * 跟进列表
     * @param Request $request
     * @return \support\Response
     */
    public function list(Request $request)
    {

        $page = $request->input('page', 1);
        $perPage = $request->input('per_page', 10);

        //客户id
        $customer_id = Util::getParam($request,'customer_id');
        if(Util::isEmpty($customer_id)){
            return Util::fail([],'customer_id'.trans('not_null'));
        }

        $where = [];
        $where['customer_id'] = $customer_id;

        //用户角色
        $userId = $request->user['id'];
        if(Util::isEmpty($userId)){
            return Util::fail(['user_id'=>$userId],trans('user_none'));
        }
        if($request->user['role']!=1){
            $where['user_id'] = $userId;
        }

        //跟进id
        $id = Util::getParam($request,'id');
        if(!Util::isEmpty($id)){
            $where['id'] = $id;
        }

        // 执行分页查询
        $data = Follow::where($where)
            ->orderBy('id', 'desc')
            ->paginate($perPage, ['*'], 'page', $page);

        foreach ($data->items() as &$value){
            $value = Follow::getFollow($value);
        }

        return Util::page($data);
    }

    /**
     * 跟进详情
     * @param Request $request
     * @return \support\Response
     */
    public function detail(Request $request)
    {
        //跟进id
        $id = Util::getParam($request,'id');
        if(Util::isEmpty($id)){
            return Util::fail([],'id'.trans('not_null'));
        }

        $res = Follow::select('id','user_id','customer_id','type','picture','remark','audio','audio_text','lat','lng','address')
            ->where('id',$id)
            ->first();
        if($res){
            $res->toArray();
        }else{
            return Util::fail([],'记录不存在');
        }

        $res = Follow::getFollow($res);

        return Util::success($res);
    }

    /**
     * 客户录入
     * @param Request $request
     * @return \support\Response
     */
    public function save(Request $request)
    {
        $customer_id = Util::getParam($request,'customer_id');
        if(Util::isEmpty($customer_id)){
            return Util::fail([],'customer_id'.trans('not_null'));
        }
        $type = Util::getParam($request,'type');
        if(Util::isEmpty($type)){
            return Util::fail([],'type'.trans('not_null'));
        }
        $picture = Util::getParam($request,'picture');
        if(Util::isEmpty($picture)){
            return Util::fail([],'picture'.trans('not_null'));
        }

        $data = [
            'user_id'=>$request->user['id'],
            'customer_id'=>$customer_id,
            'type'=>$type,
            'picture'=>$picture,
            'remark'=>Util::getParam($request,'remark',''),
            'audio'=>Util::getParam($request,'audio',''),
            'audio_text'=>Util::getParam($request,'audio',''),
            'lat'=>Util::getParam($request,'lat'),
            'lng'=>Util::getParam($request,'lng'),
            'address'=>Util::getParam($request,'address'),
        ];

        $res = Follow::create($data);

        $customer = Customer::find($customer_id);
        $customer->follow_last = $res->created_at;
        $customer->save();

        if($res){
            return Util::success($res);
        }else{
            return Util::fail($res);
        }
    }

}