<?php

namespace app\agent\controller;

use app\agent\BaseController;
use app\common\model\MobileFollow as MobileFollowModel;
use app\common\model\MobileFollowLog as MobileFollowLogModel;

class Mobilefollow extends BaseController
{

    public function followList()
    {
        $status_list = MobileFollowModel::$statusText;
        return view('mobilefollow/followlist', ['status_list' => $status_list]);
    }

    public function followForm()
    {
        $id          = input('id/d, 0');
        $follow      = MobileFollowModel::findOrEmpty($id);
        $status_list = MobileFollowModel::$statusText;
        return view('mobilefollow/followform', [
            'status_list' => $status_list,
            'follow'      => $follow,
        ]);
    }

    public function editFollow()
    {
        $agent = $this->access_agent;
        $id    = input('id/d', 0);
        $data  = [
            'agent_id'     => $agent['id'],
            'mobile'       => input('mobile/s', ""),
            'content'      => input('content/s', ""),
            'receive_time' => input('receive_time/s', ""),
            'status'       => input('status/d', 1),
        ];


        if (empty($id)) {
            MobileFollowModel::create($data);
        } else {
            $broker = MobileFollowModel::find($id);
            $broker->save($data);
        }

        exit(json_encode([
            'code' => 0,
        ]));
    }

    public function delFollow()
    {
        $idarr        = input('idarr/a');
        $follow_check = MobileFollowLogModel::whereIn('follow_id', $idarr)->find();
        if (!empty($follow_check)) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "该记录下有跟进记录，无法删除",
            ]));
        }
        $follow = MobileFollowModel::whereIn('id', $idarr)->select();
        $result = $follow->delete();
        if ($result) {
            exit(json_encode([
                'code' => 0,
                'msg'  => "",
            ]));
        }
        exit(json_encode([
            'code' => 1,
            'msg'  => "删除失败，请稍后重试",
        ]));
    }

    public function listFollow()
    {
        $agentid  = $this->access_agent['id'];
        $limit    = input('limit/d', 20);
        $page     = input('page/d', 1);
        $map      = [];
        $map[]    = ['agent_id', '=', $agentid];
        $keywords = input('keywords/s');
        if (!empty($keywords)) {
            $map[] = ['mobile', 'like', '%' . $keywords . '%'];
        }
        $status = input('status/d');
        if (!empty($status)) {
            $map[] = ['status', '=', $status];
        }
        $list  = MobileFollowModel::with(['user'])->where($map)->order('receive_time', 'DESC')->limit($limit)->page($page)->append(['status_text'])->select();
        $count = MobileFollowModel::where($map)->count();
        foreach ($list as $v) {
            $v['is_register'] = empty($v['user']) ? '否' : '是';
        }
        if ($count == 0) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "未查询到数据",
            ]));
        }
        exit(json_encode([
            'code'  => 0,
            'msg'   => "",
            'count' => $count,
            'data'  => $list,
        ]));
    }

    public function importView()
    {
        return view('mobilefollow/importview');
    }

    public function import()
    {
        $file_url = input('file_url/s', "");
        if (!file_exists($file_url)) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "文件不存在",
            ]));
        }

        $agentid = $this->access_agent['id'];
        $time    = time();
        $data    = ['account', 'mobile', 'content', 'receive_time'];
        $list    = importExecl($file_url, $data, 1);

        $create = [];
        foreach ($list as $k => $v) {
            $empty_check = [
                'mobile'       => '回复手机',
                'content'      => '回复内容',
                'receive_time' => '接收时间',
            ];
            foreach ($empty_check as $key => $value) {
                if (empty($v[$key])) {
                    exit(json_encode([
                        'code' => 1,
                        'msg'  => '第' . ($k + 2) . '行的' . $value . '不能为空',
                    ]));
                }
            }

            $item                 = [];
            $item['agent_id']     = $agentid;
            $item['mobile']       = $v['mobile'];
            $item['content']      = $v['content'];
            $item['receive_time'] = strtotime($v['receive_time']) ?: $time;
            $item['status']       = 1;

            $create[] = $item;
        }

        foreach ($create as $v) {
            MobileFollowModel::create($v);
        }

        exit(json_encode(['code' => 0]));
    }

    // 用户跟进记录
    public function follow()
    {
        $follow_id  = input('follow_id/d');
        $follow    = MobileFollowModel::findOrEmpty($follow_id);
        $followlist = MobileFollowLogModel::where('follow_id', $follow_id)->order('id', 'desc')->limit(100)->select();
        $type_list = MobileFollowLogModel::$typeText;
        return view('mobilefollow/follow', [
            'follow' => $follow,
            'followlist' => $followlist,
            'type_list' => $type_list,
        ]);
    }

    public function editFollowLog()
    {
        $follow_id = input('follow_id/d', 0);
        $follow    = MobileFollowModel::findOrEmpty($follow_id);
        if ($follow->isEmpty()) {
            exit(json_encode([
                'code' => 1,
                'msg'  => "记录不存在。",
            ]));
        }
        MobileFollowLogModel::create([
            'follow_id'  => $follow_id,
            'type'       => input('type/d', 1),
            'remark'     => input('remark/s', ""),
            'createtime' => time(),
        ]);
        $follow->save([
            'status' => 2,
        ]);
        exit(json_encode([
            'code' => 0,
        ]));
    }
}