<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\{Category, Help as HelpModel, HelpApply};
use liuniu\UtilService;
use think\Request;

class Help extends Api
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    public function index(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['category_id',0],
                ['page',1],
                ['limit',10],
                ['status',-2],
                ['isrec',-1],
                ['user_id_1',$this->auth->getUserinfo()['id']],
            ]
        );
       return $this->success('获取成功',HelpModel::lst($where));
    }
    public function ify()
    {
        $this->success('获取成功',Category::getCategoryArray('help'));
    }
    public function apply(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['category_id',0],
                ['full_name',''],
                ['contact',''],
                ['id_card',''],
                ['title',''],
                ['info',''],
                ['userimage',''],
                ['userimages',''],
                ['sex',0],
                ['politics',0],
                ['birthday',''],
                ['address',''],
                ['company',''],
                ['unit_nature',''],
                ['monthly_income',0],
                ['annual_household_income',0],
                ['annual_household_income_average',0],
                ['identity',''],
                ['marriage',0],
                ['medical_insurance',0],
                ['object_features',0],
                ['reason',0],
                ['family',[]],
            ],$request
        );
        $where['cid'] = $this->cid;
        $where['user_id'] = $this->auth->getUserinfo()['id'];
        $rs = HelpModel::help_create($where);
        if(!$rs) $this->error(HelpModel::getErrorInfo());
        $this->success('申请成功',$rs);
    }
    public function help_user_apply(Request $request)
    {
        $where = UtilService::postMore(
            [
                ['help_id',0],
                ['full_name',''],
                ['phone',''],
                ['user_id',$this->auth->getUserinfo()['id']],
                ['cid',$this->cid],
            ],$request
        );
        if($where['help_id']==0) $this->error('项目编号不能为0!');
        if($where['full_name']=='') $this->error('姓名不能为空!');
        if($where['phone']=='') $this->error('手机号不能为空!');
       if(HelpApply::where('user_id',$this->auth->getUserinfo()['id'])->where('help_id',$where['help_id'])->find()) $this->error('项目不能重复申请!');
        HelpApply::create($where);
        $this->success('申请成功');

    }
    public function user_apply_lst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['status',-2],
                ['page',''],
                ['limit',''],
                ['user_id',$this->auth->getUserinfo()['id']],
            ],$request
        );
        $this->success('获取成功',HelpApply::lst($where));
    }
    public function lst(Request $request)
    {
        $where = UtilService::getMore(
            [
                ['user_id',$this->auth->getUserinfo()['id']],
                ['user_id_1',$this->auth->getUserinfo()['id']],
                ['page',1],
                ['limit',10],
                ['status',-2],
            ]
        );
        return $this->success('获取成功',HelpModel::lst($where));
    }
    public function view()
    {
        $id = input('id',0);
        if($id==0) $this->error('参数错误!');
        $rs = HelpModel::info($this->cid,$id,$this->auth->getUserinfo()['id']);
        if(!$rs)$this->error(HelpModel::getErrorInfo());
        return $this->success('获取成功',$rs);
    }
}