<?php

namespace app\admin\controller;
use think\facade\View;
use think\facade\Lang;
use think\facade\Db;

class Card extends AdminControl
{
    public function initialize()
    {
        parent::initialize();
         Lang::load(base_path() . 'admin/lang/'.config('lang.default_lang').'/chain.lang.php');
    }
    
    public function index()
    {
        $condition = [];
        $order = 'id desc';
        $list = Db::name('sub_card')->where($condition)->order($order)->paginate(['list_rows'=>10,'query' => request()->param()],false);
        $cateIds = [];
            foreach ($list as $v) {
                if (!empty($v['goods_cate_ids'])) {
                    $cateData = explode('|', $v['goods_cate_ids']);
                    if (!empty($cateData)) {
                        foreach ($cateData as $c) {
                            $cateIds[] = $c;
                        }
                    }
                }
            }
            $showCateList = [];
            if (!empty($cateIds)) {
                $showCateList = Db::name('goodsclass')
                ->where([['gc_id', 'in', $cateIds]])->column('gc_name', 'gc_id');
            }
            
        $data = [];
        $state_text = [1=>'未使用', 2=>'使用中',3=>'已失效',5=>'已冻结'];
        foreach ($list as $key => $value){
           $state = $value['card_status'];
           $value['state_text'] = $state_text[$state]??'';
           
           $member = Db::name('member')->where(['member_id '=>$value['member_id']])->find();
           $value['member'] = $member['member_nickname'];
           
                if (empty($value['goods_cate_ids'])) {
                    $value['using_range'] = '所有';
                } else if($value['bind_cate'] == '9999'){
                    $value['using_range'] = '电影票';
                } else {
                    $cateIds = explode('|', $value['goods_cate_ids']);
                    $rangeList = [];
                    foreach ($cateIds as $c) {
                        if (!empty($showCateList[$c])) {
                            $rangeList[] = $showCateList[$c];
                        }
                    }
                    $value['using_range'] = str_replace(' ', '', implode('、', $rangeList));
                } 
                
                $data[$key] = $value;
        }
        
        View::assign('list', $data);
        View::assign('show_page', $list->render());
        $this->setAdminCurItem('index');
        return View::fetch();
    }
    
    public function state(){
        $id = input('param.id');
        $state = input('param.state');
        if($state != 5){
            $card = Db::name('sub_card')->where(['id'=>$id])->find();
            if($card){
                if($card['freeze'] == 0){
                    $state = 1;
                }else if($card['freeze'] == $card['balance']){
                    $state = 3;
                }
            }
             
        }
        $result =   Db::name('sub_card')->where(['id'=>$id])->update(['card_status'=>$state]);
        if(!$result){
            ds_json_encode('10001', lang('ds_common_op_fail'));
        }
        ds_json_encode('10000', lang('ds_common_op_succ'));
    }
}