<template>
    <view class="scroll-y-auto height-100pre">
        <view class="flex flex-warp padding-lr-40 justify-bewteen child-gap-40">
            <view class="card height-180 width-38per" @tap="go('switch', '/pages/dispatch/dispatch')">派工</view>
            <view class="card height-180 width-38per" @tap="go('switch', '/pages/work-order/work-order')">报工</view>
        </view>
    </view>
</template>

<script>
    import extend from '../../utils/extend.js';
    import token from '../../utils/token.js';
    import user from '../../utils/user.js';
    import appUpdate from '../../utils/appUpdate.js';
    import request from '../../utils/request.js';

    export default {
        data() {
            return {
                isLogin: false,
                list: [],
                isNew: true,
                appWgtVersion: '',
                isUpdating: false,
                updateProcess: 0
            }
        },
        async onShow() {
            // #ifdef APP

            let baseUrl = await request.getBaseUrl();
            // console.log('baseUrl', baseUrl);

            // 检查更新
            let updateRes = await appUpdate.check('api/PDAScan/GetVersion?name=PDA');
            this.isNew = updateRes.isNew;
            this.appWgtVersion = updateRes.appWgtVersion;

            let url = baseUrl + updateRes.url;
            // console.log(updateRes);
            // console.log('url:', url);
            // this.$refs.newVerPopup.open();
            if (updateRes.isNew) {
                uni.showModal({
                    title: '应用更新',
                    content: `发现新版本 v${updateRes.appWgtVersion}，立即更新？`,
                    async success(r) {
                        if (r.confirm) {
                            let showLoading = plus.nativeUI.showWaiting('已下载 0%', {
                                width: '120px',
                                height: '100px',
                                loading: {
                                    type: 'circle'
                                }
                            });
                            let ures = await appUpdate.update({
                                url: url,
                                progress(pr) {
                                    _this.updateProcess = pr.progress;
                                    showLoading.setTitle(`已下载 ${pr.progress}%`);
                                }
                            });
                            showLoading.close();
                            if (ures.success) {
                                uni.hideLoading();
                            } else {
                                uni.showModal({
                                    title: '错误',
                                    content: ures.message
                                });
                            }
                        }
                    }
                });
            }

            // #endif
        },
        methods: {
            /**
             * 页面跳转
             * @param {string} type 类型。navigate | switch。
             * @param {string} url 页面地址
             */
            go(type, url) {
                switch (type) {
                    case 'navigate':
                        uni.navigateTo({
                            url: url
                        });
                        break;
                    case 'switch':
                        uni.switchTab({
                            url: url
                        });
                        break;
                }
            }
        }
    }
</script>

<style lang="scss">
    .card {
        position: relative;
        display: flex;
        background-color: #ffffff;
        border: 1upx solid #dfdfdf;
        align-items: center;
        justify-content: center;
        box-shadow: 0 0 30upx rgba(0, 0, 0, 0.08);
        border-radius: 20upx;
        overflow: hidden;
    }
</style>