<?php
namespace app\api\controller;
use app\common\controller\Api;
use app\common\model\Category;
use app\common\model\project\ProjectDonation;
use app\common\model\project\ProjectDonationOrder;
use app\common\model\project\ProjectDonationRocord;
use app\common\model\project\ProjectDonationUser;
use think\Request;
use liuniu\UtilService;

class Project extends Api
{
    protected $noNeedRight = ['*'];
    /**
     * 获取捐赠列表
     * @param Request $request
     */
    public function lst(Request $request)
    {
        $where = UtilService::getMore([
            ['status',-2],
            ['page',1],
            ['limit',10],
            ['cid',$this->cid],
        ],$request);

        $this->success('获取成功',ProjectDonation::lst($where));
    }

    /**
     * 获取捐赠详情
     * @param Request $request
     */
    public function info(Request $request)
    {
        $id = input('id',0);
        $info = ProjectDonation::info($id)->toArray();
        if(!$info) return $this->error('此捐赠不存在!');
        $this->success('获取成功',$info);
    }

    /**
     * 获取我的捐赠
     * @param $where
     */
    public  function myorder(Request $request)
    {
        $where = UtilService::postMore([
            ['status',-2],
            ['page',1],
            ['limit',10],
            ['all',0],
        ],$request);
        $where['uid'] = $this->auth->getUserinfo()['id'];
        $this->success('获取成功',ProjectDonationOrder::lst($where));
    }

    /**
     * 获取所有状态
     * @param Request $request
     */
    public function bill(Request $request)
    {

        list($project_id,$page,$limit,$type) = UtilService::postMore([
            ['project_id',0],
            ['page',1],
            ['limit',10],
            ['type',''],
        ],$request,true);
        $this->success('获取成功',ProjectDonationRocord::getbill($project_id,$type,$page,$limit));
    }
    /**
     * 活动捐赠
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function  all_order(Request $request)
    {
        $where = UtilService::postMore([
            ['status',-2],
            ['page',1],
            ['limit',10],
            ['all',1],
            ['project_id',0],
        ],$request);
        //$where['uid'] = $this->auth->getUserinfo()['id'];
        $where['cid'] = $this->cid;
        $this->success('获取成功',ProjectDonationOrder::lst($where));
    }

    /**
     * 创建捐赠
     * @param Request $request
     */
    public function order_create(Request $request)
    {
        $where = UtilService::postMore([
            ['name',''],
            ['project_id',0],
            ['project_user_id',0],
            ['matter',''],
            ['qc',''],
            ['worth',''],
            ['item',[]],
            ['logistics',''],
        ],$request);
        if(empty($where['name'])) $this->error('名称不能为空!');
        if($where['project_user_id']<1) $this->error('用户不能为空!');
        if(sizeof($where['item'])==0) $this->error('捐赠物资不能为空!');
        $where['uid'] = $this->auth->getUserinfo()['id'];
        $where['cid'] = $this->cid;
        $order = ProjectDonationOrder::create_order($where);
        if ($order === false) return $this->error(ProjectDonationOrder::getErrorInfo('订单生成失败'));
        $this->success('创建成功',$order);
    }

    /**
     * 获取订单详情
     * @param Request $request
     */
    public function  order_info(Request $request)
    {
        $id = input('id',0);
        $this->success('获取成功',ProjectDonationOrder::info($id));
    }

    /**
     * 创建用户
     * @param Request $request
     */
    public function create_user(Request $request)
    {
        $where = UtilService::postMore([
            ['name',''],
            ['card_id',''],
            ['phone',''],
            ['tel',''],
            ['contacts',''],
            ['anonymous',0],
            ['invoice',0],
            ['user_type',0],
            ['logistics',''],
            ['cid',$this->cid],
        ],$request);
        $where['uid'] = $this->auth->getUserinfo()['id'];
        $id = input('id',0);
        if($where['user_type']==0)
        {
            if(empty($where['name'])) $this->error('姓名不能为空!');
            if(empty($where['phone'])) $this->error('联系方式不能为空!');
        }
        else
        {
            if(empty($where['name'])) $this->error('单位不能为空!');
            if(empty($where['contacts'])) $this->error('联系人不能为空!');
            if(empty($where['tel'])) $this->error('单位电话不能为空!');
        }
        if(empty($where['logistics'])) $this->error('物流类型不能为空!');
        if($id==0)
        {
            $rs =  ProjectDonationUser::create($where);
        }
        else
        {
            $rs = ProjectDonationUser::where('id')->update($where);
        }
        $this->success('获取成功',$rs);
    }

    /**
     * 获取用户列表
     * @param Request $request
     */
    public function user_list(Request $request)
    {
        $where['uid'] = $this->auth->getUserinfo()['id'];
        return  $this->success('获取成功',ProjectDonationUser::where($where)->order("id DESC")->select());
    }
    public  function Donation_info()
    {
        $this->success('获取成功',Category::getCategoryArray('project'));
    }
}