<?php
namespace app\admin\controller\system;
use app\admin\controller\AuthController;
use crmeb\services\{JsonService as Json,  FormBuilder as Form};
use crmeb\services\UtilService as Util;
use app\models\system\{Card as CardModel,CardInfo};
use think\facade\Route as Url;

class Card extends AuthController
{
    public function index()
    {
        $this->assign([
            'key' => $this->request->get('key', ''),
            'type' => $this->request->param('type', ''),
        ]);
        return $this->fetch();
    }
    public function lst()
    {
        $where = Util::getMore([
            ['status', -1],
            ['key', $this->request->param('key', '')],
            ['data', ''],
            ['type', -1],
            ['page', 1],
            ['limit', 20],
            ['excel', 0]
        ]);
        return Json::successlayui(CardModel::lst($where));
    }

    public function create()
    {
        $f = [];
        $f[] = Form::input('title', '批次名称');
        $f[] = Form::number('totle_num', '生成数量', 500);
        $f[] = Form::number('amount', '卡券金额');
        $f[] = Form::textarea('remark','备注');
        $f[] = Form::hidden('status',1);
        $f[] = Form::radio('type','卡券类型',0)->options([['label'=>'充值卡','value'=>1],['label'=>'会员卡','value'=>0]]);
        $form = Form::make_post_form('修改订单', $f, Url::buildUrl('save'));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }
    public function edit($id)
    {
        if (!$id) return $this->failed('数据不存在');
        $product = CardModel::find($id);
        if (!$product) return Json::fail('数据不存在!');
        $f = [];
        $f[] = Form::input('title', '批次名称',$product->title)->disabled(true);
        $f[] = Form::number('amount', '卡券金额')->disabled(true);
        $f[] = Form::radio('status','状态',1)->options([['label'=>'开启','value'=>1],['label'=>'关闭','value'=>0]]);
        $form = Form::make_post_form('修改订单', $f, Url::buildUrl('update',['id'=>$id]));
        $this->assign(compact('form'));
        return $this->fetch('public/form-builder');
    }
    public function save()
    {
        $data = Util::postMore([
            ['title',''],
            ['totle_num',0],
            ['amount',0],
            ['remark',''],
            ['status',1],
            ['type',0],
        ]);
        if(!CardModel::do_create($data))
        {
            return Json::fail(CardModel::getErrorInfo());
        }
        return Json::successful('生成成功!');
    }
    public function update($id)
    {
        if (!$id) return $this->failed('数据不存在');
        $product = CardModel::find($id);
        if (!$product) return Json::fail('数据不存在!');
        $status = input('post.status',0);
        if($product->status <>1)
        {
            return Json::fail('非正常状态不可修改');
        }
        CardModel::where('id',$id)->update(['status'=>$status]);
        CardInfo::where('card_id',$id)->update(['status'=>1]);
        return Json::successful('修改完成!');
    }
    public function info()
    {
        $this->assign([
            'key' => $this->request->get('key', ''),
            'type' => $this->request->param('type', ''),
        ]);
        return $this->fetch();
    }
    public function excal($id)
    {
        $where = Util::getMore([
            ['status', 0],
            ['card_id',$id],
            ['page', 1],
            ['limit', 20],
            ['excel', 1]
        ]);
        return CardInfo::lst($where);
    }
    public function info_lst()
    {
        $where = Util::getMore([
            ['status', 0],
            ['key', $this->request->param('key', '')],
            ['data', ''],
            ['type', -1],
            ['page', 1],
            ['limit', 20],
            ['excel', 0]
        ]);
        return Json::successlayui(CardInfo::lst($where));
    }
    public function use_card()
    {
        $this->assign([
            'key' => $this->request->get('key', ''),
            'type' => $this->request->param('type', ''),
        ]);
        return $this->fetch();
    }
    public function  use_card_lst()
    {
        $where = Util::getMore([
            ['status', 1],
            ['key', $this->request->param('key', '')],
            ['data', ''],
            ['type', -1],
            ['page', 1],
            ['limit', 20],
            ['excel', 0]
        ]);
        return Json::successlayui(CardInfo::lst($where));
    }

}
