<?php
// +----------------------------------------------------------------------
// | 天诚科技 [ 刘海东 17600099397赋能开发者,助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2020 https://www.tczxkj.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed 该系统并不是自由软件,未经许可不能去掉相关版权
// +----------------------------------------------------------------------
// | Author:甘肃天诚志信电子商务有限公司 刘海东 联系电话维系17600099397
// +----------------------------------------------------------------------

namespace app\merchant\controller\ump;

use app\merchant\controller\AuthController;
use app\merchant\model\special\Special;
use app\merchant\model\ump\StorePinkFalse;
use traits\CurdControllerTrait;
use service\JsonService as Json;
use think\Request;
use think\Url;
use app\wap\model\store\StorePink;

/**
 * 拼团管理
 * Class StoreCombination
 */
class StoreCombination extends AuthController
{

    use CurdControllerTrait;

    /**拼团列表
     * @return mixed
     */
    public function combina_list($cid = 0)
    {
        $special_type = $this->request->param('special_type');
        $this->assign('special_type', $special_type);
        $this->assign('cid', $cid);
        return $this->fetch();
    }

    /**
     * 获取拼团列表
     * @param array $where
     * @return json
     * */
    public function get_pink_list()
    {
        $where = parent::getMore([
            ['status', ''],
            ['data', ''],
            ['nickname', ''],
            ['page', 1],
            ['cid', 0],
            ['limit', 10],
        ], $this->request);
        $where['mer_id'] = $this->agent;
        return Json::successlayui(StorePink::getPinkList($where));
    }


    /**
     * 删除虚拟拼团
     * @param int $id 拼团id
     * @return json
     * */
    public function delete_pink($id = 0)
    {
        if (!$id) return Json::fail('缺少参数');
        if (StorePink::be(['is_false' => 0, 'id' => $id])) return Json::fail('不是虚拟拼团无法删除');
        if (StorePink::where('id', $id)->delete())
            return Json::successful('删除成功');
        else
            return Json::fail('删除失败');
    }

    /**
     * 下架拼团
     * @param int $id 拼团id
     * @return json
     * */
    public function down_pink($id = 0)
    {
        if (!$id) return Json::fail('缺少参数');
        $res = StorePink::downPink($id);
        if ($res === false)
            return Json::fail(StorePink::getErrorInfo());
        else
            return Json::successful('下架成功');
    }

    /**
     * 助力拼团
     * */
    public function helpe_pink($id = 0)
    {
        $this->assign('id', $id);
        return $this->fetch();
    }

    public function save_helpe_pink()
    {
        list($pink_id, $nickname, $avatar) = parent::postMore([
            ['pink_id', 0],
            ['nickname', ''],
            ['avatar', ''],
        ], $this->request, true);
        if (!$pink_id) return Json::fail('缺少助力团ID');
        if (!$nickname) return Json::fail('请输入助力用户用户名');
        if (!$avatar) return Json::fail('请上传助力用户头像');
        $res = StorePink::helpePink($pink_id, $nickname, $avatar);
        if ($res === false)
            return Json::fail(StorePink::getErrorInfo());
        else
            return Json::successful('助力成功');
    }

    /**拼团人列表
     * @return mixed
     */
    public function order_pink($id)
    {
        if (!$id) return $this->failed('数据不存在');
        $StorePink = StorePink::getPinkUserOne($id);
        if (!$StorePink) return $this->failed('数据不存在!');
        $list = StorePink::getPinkMember($id);
        $list[] = $StorePink;
        if ($id = StorePink::where(['k_id' => $id, 'is_false' => 1])->column('id')) {
            $falsePink = count($id) ? StorePinkFalse::where('pink_id', 'in', $id)->select()->toArray() : [];
            foreach ($falsePink as $item) {
                $item['uid'] = 0;
                $item['is_refund'] = 0;
                $item['is_false'] = 1;
                $item['total_price'] = $StorePink['total_price'];
                array_push($list, $item);
            }
        }
        $this->assign('list', $list);
        return $this->fetch();
    }


    /**
     * 创建虚拟拼团
     *
     * */
    public function create_pink_false()
    {
        $this->assign('list', Special::PreWhere()->field('id,title')->select());
        return $this->fetch();
    }

}