<template>
    <view class="write-off-index">
        <head-bar
            :range="stadiumList" range-key="name" @change="stadiumChange"
            placeholder="请选择场馆" :value="curStadium.name"
        ></head-bar>
        <view class="woi-section">
            <view class="ws-ipt-box">
                <input class="wib-ipt" type="text" placeholder="请输入券码" v-model="iptCode" />
                <image @click="scanCode" class="wib-img" mode="aspectFit" src="/subpackage/verification/static/images/scan.png"></image>
            </view>
            <view class="ws-txt">可输入订场、次卡、年月卡、赛事、商城到店领取商品的验证码/券码进行核销，或点扫码图标进入扫描界面扫码核销。</view>
            <view class="ws-btn">确认</view>
        </view>

        <view class="woi-btns">
            <view class="wb-items" @click="toAbnormal">
                <image class="wi-icon" mode="aspectFit" src="/subpackage/verification/static/images/tab0.png"></image>
                <view class="wi-txt">进场人数异常</view>
            </view>
            <view class="wb-items" @click="toSitePeople">
                <image class="wi-icon" mode="aspectFit" src="/subpackage/verification/static/images/tab1.png"></image>
                <view class="wi-txt">现场人数</view>
            </view>
            <view class="wb-items">
                <image class="wi-icon" mode="aspectFit" src="/subpackage/verification/static/images/tab2.png"></image>
                <view class="wi-txt">核销记录</view>
            </view>
        </view>
    </view>
</template>

<script>

import API from "../js/api.js";
import server from "../js/server.js";
import { showLoad, hideLoad, showModal, showNone, debounce, changeLowerCase, $_emit, routeTo } from "@/utils/util.js";
import { WRITE_OFF_ORDER_INFO } from '@/js/once_name';
import headBar from "../components/head_bar.vue";
export default {
    components: { headBar },
    data(){
        return{
            brand_id: '',
            stadiumList: [],
            curStadium: {},
            iptCode: '',

        }
    },
    onLoad(options){
        let _brand_id = options?.brand_id || '';
        this.brand_id = _brand_id;
        this.getStadiumLs({ brand_id: _brand_id });
    },
    methods: {
        // 现场人数
        toSitePeople(){
            let { brand_id } = this;
            routeTo(`/subpackage/verification/pages/site_people/index?brand_id=${brand_id}`, 'nT');
        },
        // 人数异常
        toAbnormal(){
            let { brand_id } = this;
            routeTo(`/subpackage/blacklist/pages/abnormal_list/abnormal_list?brand_id=${brand_id}`, 'nT');
        },
        scanCode: debounce(function(){
            let { curStadium } = this;
            if(!curStadium?.id)return showModal({ content: '请选择场馆！' });
            uni.scanCode({
                onlyFromCamera: true,
                scanType: 'qrCode',
                success: res=> {
                    if(changeLowerCase(res.scanType) !== 'qr_code')return showNone('不支持此类型！');
                    console.log('扫码结果--->', res);
                    let { curStadium } = this;
                    this.verifyOrder({
                        brand_id: curStadium?.brand_id || '',
                        stadium_id: curStadium?.id || '',
                        decrypt_text: res?.result || '',
                    })
                },
                fail: function(err) {
                    showNone('扫码失败！');
                    console.warn('扫码失败--->', err);
                }
            })
        }, 300, true),
        stadiumChange(e){
            let { stadiumList } = this;
            this.curStadium = stadiumList[e.detail.value];
        },
        // 获取门店列表
        getStadiumLs({ brand_id }){
			showLoad();
            return server.post({
                url: API.stadiumList,
                data: { page_size: 9999, page: 1, brand_id },
                isDefaultGet: false,
            })
            .then(res => {
				hideLoad();
                let _data = res?.data || {};
                if(_data.code === 0){
                    let _ls = _data?.data?.list || [];
                    this.stadiumList = _ls;
					return _ls;
                }else{
                    return Promise.reject(_data);
                }
            })
            .catch(err => {
                hideLoad();
                showModal({
                    title: '提示',
                    content: err.message || '加载店铺失败！'
                })
                console.warn('verification index getStadiumLs err --->', err);
				// return Promise.reject(err);
            })
		},
        /**
         * 订单预检验
         * @param {String} brand_id
         * @param {String} stadium_id
         * @param {String} verify_code 订单验证码
         * @param {String} decrypt_text 二维码数据
         * */ 
        verifyOrder({ brand_id, stadium_id, verify_code = '', decrypt_text = '' }){
            let _vType = verify_code ? 'verify_code' : decrypt_text ? 'decrypt_text' : '';
			showLoad();
            return server.post({
                url: API.enterVerifyOrder,
                data: { brand_id, stadium_id, verify_code, decrypt_text },
                isDefaultGet: false,
            })
            .then(res => {
				hideLoad();
                let _data = res?.data || {};
                if(_data.code === 0){
                    let _orderData = _data?.data || {};
                    // 商城订单
                    if(_orderData?.extension?.verify_order_type === 'product_order'){
                        $_emit(WRITE_OFF_MALL_ORDER_INFO, { data: _orderData });
                        routeTo(`/pages/write_off/ym_confirm/ym_confirm?type=${_vType}`, 'nT');
                        return
                    }
                     // 年月卡
                    if(_orderData?.extension?.verify_order_type === 'monthly_card'){
                        $_emit(WRITE_OFF_ORDER_INFO, { data: _orderData, query: { brand_id, stadium_id, verify_code, decrypt_text } });
                        routeTo(`/pages/write_off/ym_confirm/ym_confirm?type=${_vType}`, 'nT');
                        return
                    }

                    // 赛事
                    if(_orderData?.extension?.verify_order_type === 'match_order'){
                        $_emit(WRITE_OFF_ORDER_INFO, {..._orderData});
                        routeTo(`/pages/write_off/events_order/events_order?type=${_vType}`, 'nT');
                        return
                    }
                    
                    $_emit(WRITE_OFF_ORDER_INFO, { ..._orderData });
                    routeTo(`/pages/write_off/confirm_order/confirm_order?type=${_vType}`, 'nT');
                }else{
                    if(!_vType)return Promise.reject(_data);
                    routeTo(`/pages/write_off/null/null?type=${_vType}`, 'nT');
                    // return Promise.reject(_data);
                }
            })
            .catch(err => {
                hideLoad();
                showModal({
                    title: '提示',
                    content: err.message || '核销查询失败！'
                })
                console.warn('verification index verifyOrder err --->', err);
				// return Promise.reject(err);
            })
		},
    }
}
</script>

<style lang="scss">
    .write-off-index{
        
        .woi-section{
            margin-top: 24upx;
            padding-top: 26upx;
            padding-bottom: 80upx;
            background: #fff;
            .ws-ipt-box{
                margin: 0 auto;
                padding: 0 20upx;
                width: 702upx;
                height: 92upx;
                background: #F2F2F7;
                border-radius: 10upx;
                @include ctf;
                .wib-ipt{
                    flex-grow: 1;
                    height: 100%;
                    @include flcw(28upx, 40upx, #333);
                }
                .wib-img{
                    flex-shrink: 0;
                    margin-left: 20upx;
                    width: 60upx;
                    height: 60upx;
                }
            }
            .ws-txt{
                margin-top: 24upx;
                padding: 0 32upx;
                @include flcw(28upx, 52upx, #333);
            }
            .ws-btn{
                margin: 22upx auto 0;
                text-align: center;
                width: 240upx;
                border-radius: 10upx;
                background: $mColor;
                @include flcw(32upx, 88upx, #fff);
            }
        }
        .woi-btns{
            margin-top: 24upx;
            padding: 38upx 24upx;
            background: #fff;
            @include ctf(space-between);
            .wb-items{
                padding: 18upx 0;
                flex-grow: 0;
                flex-shrink: 0;
                width: 226upx;
                .wi-icon{
                    display: block;
                    margin: 0 auto;
                    width: 64upx;
                    height: 64upx;
                }
                .wi-txt{
                    margin-top: 24upx;
                    text-align: center;
                    @include flcw(28upx, 40upx, #333);
                    @include tHide;
                }
            }
        }
    }
</style>