<?php

namespace app\api\controller\company;

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

/**
 * 套餐管理
 */
class Package extends Apic
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';


    //列表
    public function lists(){
        $status = input('status',1);
        $keyword = input('keyword','');

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

        if(!empty($keyword)){
            $where['p.title|p.info'] = ['LIKE','%'.$keyword.'%'];
        }

        $list = Db::name('package')->alias('p')
            ->field('p.*,type.title as servicetype_title')
            ->join('servicetype type','p.servicetype_id = type.id','LEFT')
            ->where($where)->order('p.id desc')->autopage()->select();
        $list = list_domain_image($list,['images','content_images']);
        $this->success(1,$list);

    }

    //新增
    public function add(){
        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置');
        }
        $field = ['title','info','servicetype_id','images','price','oldprice','content','content_images'];
        $data = request_post_hub($field);

        $data['company_id'] = $this->auth->company_id;
        $data['createtime'] = time();
        $data['updatetime'] = time();
        $data['status'] = 1;

        Db::name('package')->insertGetId($data);
        $this->success('添加成功');
    }

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

        $new_status = $status == 1 ? 0 : 1;

        $info = Db::name('package')->where('id',$id)->update(['status'=>$new_status,'updatetime'=>time()]);
        $this->success();
    }

    //详情
    public function info(){
        $id = input('id',0);
        $info = Db::name('package')->alias('p')
            ->field('p.*,type.title as servicetype_title')
            ->join('servicetype type','p.servicetype_id = type.id','LEFT')
            ->where('p.id',$id)->find();
        $info = info_domain_image($info,['images','content_images']);
        $this->success(1,$info);
    }

    //编辑
    public function edit(){
        //验证
        if($this->auth->type != 1){
            $this->error('只有门店老板才能设置');
        }
        $id = input('id','');

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

        //
        $field = ['title','info','servicetype_id','images','price','oldprice','content','content_images'];
        $data = request_post_hub($field);
        $data['updatetime'] = time();

        Db::name('package')->where('id',$id)->update($data);
        $this->success('编辑成功');
    }

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

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

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



}
