<?php

namespace app\mobile\controller;

use app\common\model\InfoModel;
use app\common\model\UserFollowModel;
use app\mobile\MobileBaseController;
use app\mobile\validate\InfoValidate;
use think\App;
use think\exception\ValidateException;

class Info extends MobileBaseController
{
    private $_user = null;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->_user = get_user();
    }

    /**
     * 登记列表
     */
    public function index()
    {
        $list = InfoModel::where('user_id', $this->_user['id'])
            ->append(['type_text'])
            ->select();

        return view('', ['list' => $list]);
    }

    /**
     * 类型介绍
     */
    public function type()
    {
        $list     = InfoModel::TYPE;
        $describe = InfoModel::TYPE_DESCRIBE;

        return view('', ['list' => $list, 'describe' => $describe]);
    }

    /**
     * 登记表单
     */
    public function form()
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            $info = '{}';
        } else {
            $info        = InfoModel::find($id);
            $info['sex'] = (string)$info['sex'];
            empty($info) && jump('该信息不存在');
        }

        $type      = InfoModel::TYPE;
        $type_text = $info == '{}' ? '' : $type[$info['type']];
        return view('', [
            'info'      => $info,
            'type_text' => $type_text,
            'type_list' => array_to_vue($type),
        ]);
    }

    /**
     * 表单提交
     */
    public function formPost()
    {
        $data            = input('post.');
        $data['user_id'] = $this->_user['id'];

        try {
            validate(InfoValidate::class)->check($data);
        } catch (ValidateException $e) {
            ajax_return(1, $e->getError());
        }

        if (empty($data['id'])) {
            InfoModel::create($data);
        } else {
            InfoModel::update($data, ['id' => $data['id']]);
        }

        ajax_return();
    }

    /**
     * 删除
     */
    public function delete()
    {
        $id = input('post.id');
        empty($id) && ajax_return(1, '该信息不存在');

        $user_id = $this->_user['id'];
        $info    = InfoModel::where('id', $id)->where('user_id', $user_id)->find();
        empty($info) && ajax_return(1, '该信息不存在');

        UserFollowModel::destroy(['info_id' => $id]);
        $info->delete();

        ajax_return();
    }

    /**
     * 跟进
     */
    public function follow()
    {
        $id = input('id');
        empty($id) && jump('该记录不存在');

        $list = UserFollowModel::where('user_id', $this->_user['id'])->where('info_id', $id)->order('id', 'desc')->select();

        return view('', ['list' => $list, 'id' => $id]);
    }

    public function followPost()
    {
        $id = input('post.id');
        empty($id) && ajax_return(1, '数据异常');
        $remark = input('post.remark');
        empty($remark) && ajax_return(1, '请填写跟进记录');

        $data = [
            'user_id'   => $this->_user['id'],
            'info_id'   => $id,
            'user_type' => UserFollowModel::USER_TYPE_USER,
            'type'      => UserFollowModel::TYPE_OTHER,
            'remark'    => $remark,
        ];
        UserFollowModel::create($data);

        ajax_return();
    }
}
