<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Sos;
use app\common\model\SosBill;
use app\common\model\UserRelation;
use liuniu\UtilService;
use liuniu\WechatTempleService;
use think\Request;
use \app\common\model\Rescue as RescueModel;

class Rescue extends Api
{
    protected $noNeedRight = ['*'];

    public function index(Request $request)
    {
        list($page, $limit, $latitude, $longitude) = UtilService::getMore(
            [
                ['page', 1],
                ['limit', 10],
                ['latitude', ''],
                ['longitude', ''],
            ],
            $request, true
        );
        if ($latitude == '' || $longitude == '') $this->error('请先获取定位');
        return $this->success('获取成功', RescueModel::lst($this->cid, $latitude, $longitude, $page, $limit, $this->auth->getUserinfo()['id'], 1, 1));
    }

    public function create(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['name', ''],
                ['phone', ''],
                ['institution_id', 0],
                ['volunteer_id', 0],
                ['address', ''],
                ['latitude', ''],
                ['longitude', ''],
                ['certificateimage', ''],
            ], $request
        );
        if ($where['name'] == '') $this->error('姓名不能为空');
        if ($where['phone'] == '') $this->error('电话不能为空');
        if ($where['address'] == '') $this->error('地址信息不能为空');
        if ($where['certificateimage'] == '') $this->error('证书不能为空');

        $where['cid'] = $this->cid;
        $where['user_id'] = $this->auth->getUserinfo()['id'];
        if (RescueModel::where('user_id', $where['user_id'])->where('status', '>', -1)->find()) $this->error('已经存，不能重复申请');
        if (!RescueModel::create1($where)) {
            $this->error(RescueModel::getErrorInfo());
        }
        $this->success('创建成功');
    }

    public function applylst(Request $request)
    {
        return $this->success('获取成功', RescueModel::lst($this->cid, '', '', 1, 10, $this->auth->getUserinfo()['id'], -2));
    }

    public function sos_create(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['mobile', ''],
                ['address', ''],
                ['latitude', ''],
                ['longitude', ''],
                ['rescuers_id', 0],
                ['rescuers_user_id', 0],
                ['from', 'weixin'],
                ['re_url', ''],
            ], $request
        );
        if ($where['rescuers_id'] == 0) $this->error('选择救授人员');
        if ($where['latitude'] == '' || $where['longitude'] == '') $this->error('请先获取定位');
        $where['user_id'] = $this->auth->getUserinfo()['id'];
        $where['cid'] = $this->cid;
        if (Sos::sos_create($where)) $this->success('创建成功');
        $this->error(Sos::getErrorInfo());


    }

    public function lst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['page', 1],
                ['limit', 10],
                ['type', 0],
                ['status', -3],
                ['latitude', ''],
                ['longitude', ''],
            ],
            $request
        );
        $where['user_id'] = 0;
        $where['rescue_id'] = 0;
        if ($where['type'] == 0) {
            $where['user_id'] = $this->auth->getUserinfo()['id'];
        } else {
            $where['rescuers_user_id'] = $this->auth->getUserinfo()['id'];
        }
        return $this->success('获取成功', SosBill::lst($where));
    }

    public function setstatus(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['status', 1],
                ['process_remark', ''],
                ['from', 'weixin'],
                ['re_url', ''],

            ], $request
        );
        $where['processtime'] = time();
        $where1 = $where;
        unset($where1['from']);
        unset($where1['re_url']);
        SosBill::where('id', input('id', 0))->update($where1);
        $info = SosBill::find(input('id', 0));
        switch (intval($where['status'])) {
            case  1:
                $temp = WechatTempleService::SUPPAORTER_REPLY;
                $res_user = RescueModel::where('user_id', $this->auth->getUserinfo()['id'])->find();
                $data['first'] = "你发起求救已受理，救员者正在赶来";
                $data['keyword1'] = $res_user['phone'];
                $data['keyword2'] = $res_user['address'];
                $data['keyword3'] = date("Y-m-d H:i:s");
                $data['remark'] = $where['process_remark'];
                $rs = WechatTempleService::sendTemplate($this->cid, UserRelation::userIdToOpenId($info['user_id']), $temp, $data, $where['re_url']);
                break;
            case -1:
                $temp = WechatTempleService::REQUEXST_REFUSE;
                $data['first'] = "你发起求救已拒绝";
                $data['keyword1'] = $where['process_remark'];
                $data['keyword2'] = date("Y-m-d H:i:s");
                $data['keyword3'] = "求救";
                $data['remark'] = "";
                $rs = WechatTempleService::sendTemplate($this->cid, UserRelation::userIdToOpenId($info['user_id']), $temp, $data, $where['re_url']);
                break;
        }
        $this->success('处理完成');
    }

    public function sos_del(Request $request)
    {
        $id = input('id');
        if (SosBill::where('sos_id', $id)->where('status', '<', 1)->find()) {
            SosBill::where('sos_id', $id)->where('status', '<', 1)->delete();
            $this->success('删除成功');
        }
        $this->error('已删除或已接受');

    }
}
