<?php

namespace app\api\controller\company;

use app\common\controller\Apic;
use think\Db;

/**
 * 储值卡
 */
class Recharge extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //首页
    public function lists(){
        $status = input('status',1);

        $where = [
            'company_id' => $this->auth->company_id,
            'status'     => $status,
        ];

        $list = Db::name('recharge_config')->where($where)->order('id desc')->select();

        //追加赠送
        if(!empty($list)){
            $config_ids = array_column($list,'id');
            $gift = Db::name('recharge_gift')->alias('gift')
                ->field('gift.*,coupons.name,coupons.info,coupons.days')
                ->join('coupons','gift.coupon_id = coupons.id','LEFT')
                ->where('gift.config_id','IN',$config_ids)
                ->where('coupons.status',1)
                ->select();

            foreach($list as $key => &$val){
                $val['gift'] = [];
                foreach($gift as $k => $v){
                    if($val['id'] == $v['config_id']){
                        $val['gift'][] = $v;
                    }
                }
            }

        }
        $this->success(1,$list);
    }

    //新增
    public function add(){
        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置');
        }

        $field = ['price','giftprice'];
        $data = request_post_hub($field);

        $data['company_id'] = $this->auth->company_id;
        $data['status'] = 1;

        Db::startTrans();
        $config_id = Db::name('recharge_config')->insertGetId($data);
        if(!$config_id){
            Db::rollback();
            $this->error('添加失败');
        }

        //赠送卡券
        $gift_data = input('gift_data','','trim');
        $gift_data = json_decode(htmlspecialchars_decode($gift_data),true);

        if(is_array($gift_data) && !empty($gift_data)){
            $recharge_gift = [];
            foreach($gift_data as $key => $val){
                $recharge_gift[] = [
                    'config_id' => $config_id,
                    'coupon_id' => $val['coupon_id'],
                    'number'    => $val['number'],
                ];
            }
            if(!empty($recharge_gift)){
                $rs_gift = Db::name('recharge_gift')->insertAll($recharge_gift);
                if($rs_gift === false){
                    Db::rollback();
                    $this->error('添加失败');
                }
            }
        }

        Db::commit();

        $this->success('添加成功');
    }

    //上下架
    public function changestatus(){
        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置');
        }
        $id = input('id',0);
        $info = Db::name('recharge_config')->where('id',$id)->update(['status'=>0]);
        $this->success();
    }

    //删除
    public function delete(){
        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置');
        }

        $id = input('id','');

        $check = Db::name('recharge_config')->where('id',$id)->where('company_id',$this->auth->company_id)->find();
        if(empty($check)){
            $this->error('不存在的储值卡');
        }

        Db::name('recharge_config')->where('id',$id)->delete();
        Db::name('recharge_gift')->where('config_id',$id)->delete();
        $this->success('删除成功');
    }





}
