<template>
    <view class="operate-container">
        <view class="store-bar">
            <text>当前门店</text>
            <picker mode="selector" :range="stadiumList" range-key="name" @change="stadiumChange">
                <view>
                    <input disabled v-model="curStadium.name" />
                    <image mode="aspectFit" src="/static/images/icon/arrow_c33.png"></image>
                </view>
            </picker>
        </view>
        <view class="c-scan-btn" @click="scanCodeBtn">
            <image mode="aspectFit" src="/static/images/icon/scan_code_btn.png"></image>
        </view>
        <view class="c-verification-code">
            <view><input placeholder="输入订单验证码" v-model="iptCode" /></view>
            <view @click="confirmBtn">确认核销</view>
        </view>
    </view>
</template>

<script>
import util from '../../../utils/util';
import { API } from '../../../js/api';
import { servers } from '../../../js/server';
import { WRITE_OFF_STORE_NAME, WRITE_OFF_ORDER_INFO, WRITE_OFF_MALL_ORDER_INFO } from '../../../js/once_name';
import { mapState } from 'vuex';
export default {
    data(){
        return {
            iptCode: '',
            stadiumList: [],
            curStadium: {},
            
            writeOffType: '', // 新增核销类型  site(场地订单)/ mall(商城订单)

        }
    },
    computed: {
        ...mapState([ 'brandInfo' ]),
    },
    onLoad(options){
        this.writeOffType = options.type || '';
        util.$_once(WRITE_OFF_STORE_NAME, data => {
            console.warn(data);
            if(!data)return this.initStore();
            this.curStadium = data.curStadium;
            this.stadiumList = data.stadiumList;
        })
        // console.warn(this.brandInfo)

    },
    methods: {
        async initStore(){
            let { brandInfo } = this;
            try{
                util.showLoad();
                let _storeList = await this.getStoreList({ brand_id: brandInfo.brand.id || '' });
                if(!_storeList || !_storeList.length)return util.showNone('没有店铺信息');
                this.stadiumList = _storeList || [];
                if(_storeList.length) this.curStadium = _storeList[0];
                util.hideLoad();
            }catch(err){
                util.hideLoad();
                util.showNone('初始化店铺数据失败！');
                console.warn('加载数据失败！', err);
            }
        },
        // 获取店铺列表
        getStoreList({
            page=1,
            page_size=9999,
            brand_id='',
        }){
            return servers.get({
                url: API.stadiumList,
                data: {
                    page,
                    page_size,
                    brand_id,
                },
                failMsg: '获取列表失败！'
            })
            .then(res=>{
                let _list = res.list || [];
                return _list
            })
        },
        
        scanCodeBtn: util.debounce(function(){
            uni.scanCode({
                onlyFromCamera: true,
                scanType: 'qrCode',
                success: res=> {
                    console.warn('success--->', res);
                    if(util.changeLowerCase(res.scanType) !== 'qr_code')return util.showNone('不支持此类型！');
                    console.warn(res.result)
                    this.analysisOrder({ decrypt_text: res.result });
                },
                fail: function(err) {
                    util.showNone('扫码失败！');
                    console.warn('扫码失败--->', err);
                }
            })
        }, 300, true),
        stadiumChange(e){
            let { stadiumList } = this;
            this.curStadium = stadiumList[e.detail.value];
        },
        confirmBtn: util.debounce(function(){
            let { iptCode } = this;
            if(!iptCode)return util.showNone('请输入核销码！');
            this.analysisOrder({ verify_code: this.iptCode });
        }, 300, true),


        // 核销请求
        analysisOrder({ verify_code, decrypt_text }){
            let { curStadium, brandInfo, writeOffType } = this;
            if(!verify_code&&!decrypt_text)return;

            let _query = {
                brand_id: brandInfo.brand.id,
                stadium_id: curStadium.id,
            }
            let _vType = '';
            if(!!verify_code){
                _vType = 'verify_code';
                writeOffType == 'site'&&(_query['verify_code'] = verify_code);
                writeOffType == 'mall'&&(_query['vcode'] = verify_code);
            }
            if(!!decrypt_text){
                _vType = 'decrypt_text';
                writeOffType == 'site'&&(_query['decrypt_text'] = decrypt_text);
                writeOffType == 'mall'&&(_query['vcode'] = decrypt_text);
            }
            if(writeOffType == 'site')return this.siteGet({ query: _query, vType: _vType, });
            if(writeOffType == 'mall')return this.mallGet({ query: _query, vType: _vType, });
        },
        // 商城订单
        mallGet({ query = {}, vType = '', }){
            util.showLoad();
            servers.get({
                url: API.writeOff.shop2WriteoffGet,
                data: query,
                isDefaultGet: false
            })
            .then(res=>{
                util.hideLoad();
                if(res.data.code == 0){
                    let _data = res.data.data || {};
                    if(_data.has_order){
                        util.$_emit(WRITE_OFF_MALL_ORDER_INFO, {..._data.order || {}});
                        util.routeTo(`/pages/write_off/mall/confirm/confirm?type=${vType}`, 'nT');
                    }else{
                        util.routeTo(`/pages/write_off/null/null?type=${vType}`, 'nT');
                    }
                }else{
                    util.showNone(res.data.message || '操作失败！')
                    // util.routeTo(`/pages/write_off/null/null?type=${vType}`, 'nT');
                }
                
            })
            .catch(util.hideLoad)
        },
        // 场地/ 年月卡 
        siteGet({ query = {}, vType = '', }){
            util.showLoad();
            servers.get({
                url: API.writeOff.enterVerifyOrder,
                data: query,
                isDefaultGet: false
            })
            .then(res=>{
                util.hideLoad();
                if(res.data.code == 0){
                    let _data = res.data.data || {}
                    
                    if(_data.extension&&_data.extension.verify_order_type === 'monthly_card'){
                        util.$_emit(WRITE_OFF_ORDER_INFO, {..._data});
                        util.routeTo(`/pages/write_off/ym_confirm/ym_confirm?type=${vType}`, 'nT');
                        return
                    }

                    util.$_emit(WRITE_OFF_ORDER_INFO, {..._data});
                    util.routeTo(`/pages/write_off/confirm_order/confirm_order?type=${vType}`, 'nT');
                }else{
                    util.routeTo(`/pages/write_off/null/null?type=${vType}`, 'nT');
                }
                console.log('订单查询---->', res);
            })
            .catch(util.hideLoad)
        }

    }
}
</script>

<style lang="scss">
    @import '~style/public.scss';
    page{
        background-color: #f2f2f7;
    }
    .operate-container{
        .store-bar{
            margin-bottom: 24upx;
            padding: 0 24upx;
            height: 144upx;
            background-color: #fff;
            @include centerFlex(space-between);
            >text{
                margin-right: 20upx;
                flex-shrink: 0;
                font-size: 28upx;
                color: #9C9C9F;
            }
            >picker{
                flex-grow: 1;
            }
            view{
                padding: 0 20upx;
                height: 92upx;
                border-radius: 10upx;
                background: #f2f2f2;
                @include centerFlex(space-between);
                >input{
                    flex-grow: 1;
                    height: 100%;
                    font-size: 28upx;
                    color: #333;
                }
                >image{
                    flex-shrink: 0;
                    flex-grow: 0;
                    width: 28upx;
                    height: 28upx;
                }
            }
        }
        .c-scan-btn{
            margin: 0 auto 24upx;
            width: 702upx;
            height: 360upx;
            border-radius: 10upx;
            background-color: #fff;
            @include centerFlex(center);
            >image{
                width: 172upx;
                height: 172upx;
            }
        }
        .c-verification-code{
            padding: 40upx;
            border-radius: 10upx;
            background-color: #fff;
            >view{
                &:first-child{
                    margin-bottom: 30upx;
                    padding: 0 20upx;
                    height: 112upx;
                    border-radius: 10upx;
                    background-color: #f2f2f7;
                    >input{
                        height: 100%;
                        width: 100%;
                        font-size: 32upx;
                        color: #1a1a1a;
                    }
                }
                &+view{
                    height: 112upx;
                    text-align: center;
                    line-height: 112upx;
                    border-radius: 10upx;
                    font-size: 32upx;
                    color: #fff;
                    background-color: $themeColor;
                }
            }
        }
    }
</style>