<template>
	<view class="page">
		<page-header></page-header>
		<view class="ant-layout content">
		  <view class="ant-layout-header header">
		      <div class="title">参数设置</div>
		  </view>
		  <view class="ant-layout-content container">
            <uni-row :gutter="20">
                <uni-col :span="12">
                    <div class="btn-group">
                        <button class="ant-btn-lg" @click="go('device')">设备信息</button>
                        <button class="ant-btn-lg" @click="asyncNet('time')">时间同步</button>
                        <button class="ant-btn-lg" @click="go('endomanage')">内镜信息管理</button>
                        <button class="ant-btn-lg" @click="go('personmanage')">人员信息管理</button>
                        <button class="ant-btn-lg" @click="go('run')">运行参数设置</button>
                    </div>
                </uni-col>
                <uni-col :span="12">
                    <div class="btn-group">
                        <button class="ant-btn-lg" @click="go('base')">基础设置</button>
                        <button class="ant-btn-lg" @click="go('net')">服务器设置</button>
                        <button class="ant-btn-lg" @click="asyncNet('endo')">内镜信息同步</button>
                        <button class="ant-btn-lg" @click="asyncNet('user')">人员信息同步</button>
                        <button class="ant-btn-lg" @click="asyncNet('run')">运行参数同步</button>
                    </div>
                </uni-col>
            </uni-row>
		  </view>
		  <input-num-pop ref="inputPop" @submit="inputModalSubmit" />
		</view>
        <notice ref="notice"></notice>
		<page-footer></page-footer>
	</view>
</template>

<script>
import InputNumPop from '@/components/InputNumPop.vue';
import PageHeader from '@/components/PageHeader.vue'
import PageFooter from '@/components/PageFooter.vue'
import Notice from '@/components/Notice.vue';
import storage from '@/utils/storage.js';
import * as db from '@/db/sqlite.js'
import * as Api from '@/api/index.js'
export default {
	components: {
		PageFooter, PageHeader,
        InputNumPop, Notice
    },
    data() {
        return {
            action: '' // 当前要执行的动作
            , password: ''
        }        
    },
    onBackPress() {
        uni.navigateTo({ url: '/pages/index', animationType: 'fade-in' })
        return true;
    },
    onShow() {
        let base = storage.get('base')
        if (base) {
            this.password = base.settingPw
        }
    },
    methods: {

        go(name) {
            if (this.password) {
                this.action = { 'nav': name }
                this.$refs.inputPop.show()
            } else {
                this.action = ''
                uni.navigateTo({
                    url: '/pages/set/' + name
                })
            }
        },
        // 密码框提交
        inputModalSubmit(val) { 

            this.$refs.inputPop.close()
            if (val == this.password) {
                uni.showToast({
                    title: '验证通过',
                    icon: 'none'
                })
                this.doAction()
            } else { 
                uni.showToast({
                    title: '密码错误',
                    icon: 'none',
                })
            }
        },
        asyncNet(type) {
            this.action = { 'async': type }
            if (this.password) {
                this.$refs.inputPop.show()
            } else {
                this.doAction()
            }
        },
        // 执行动作
        doAction() {
            if (this.action.hasOwnProperty('nav')) {
                uni.navigateTo({
                    url: '/pages/set/' + this.action.nav,
                    animationType: 'fade-in'
                })
            }
            if (this.action.hasOwnProperty('async')) { 
                if (this.action.async == 'endo') {
                    // 同步内镜信息
                    this.asyncEndo()
                }
                if (this.action.async == 'user') {
                    // 同步人员信息
                    this.asyncUser()
                }
                if (this.action.async == 'run') {
                    // 同步运行参数
                    this.asyncSetting()
                }

                this.$refs.notice.open({
                    title: '正在同步数据...',
                    content: '请稍候'
                })

            }
            this.action = ''
        },
        async asyncEndo() {
            let tableName = 'endo'
            let isTable = await db.isTable(tableName)
            if (!isTable) {
                await db.addEndoTable()
            }
            let list = await Api.endoscopelist().then(res => { 
                console.log('endo', res)
                if (res) { 
                    return res.endoscope_list
                } else { 
                    return []
                }
            })
            list.forEach(el => {
                let form = {
                    rfid: el.endoscope_rfid,
                    ic: '',
                    model: '',
                    brand: '',
                    type: ''
                }
            });
        },
        async asyncUser() {
            Api.userlist().then(res => {
                console.log('user', res)
            })
        },
        async asyncSetting() {
            let res = await Api.parameterlist()
        }


    }

}
</script>

<style scoped>
.page{
	height: 100%;
	display: flex;
	flex-direction: column;
}
.content{
    color: #747A8D;
    height: 100%;
}
.content,
.header,
.footer{
    background: transparent;
}
.title{
    color: #747A8D;
    font-size: 26px;
}
.container{ 
    justify-content: center;
    display: flex;
    flex-direction: column;
    margin: 0 10px;
}
.btn-group{ 
    flex-direction: column;
    display: flex;
    align-items: center;
}
.ant-btn-lg{
    background: #2F3242;
    width: 280px;
    height: 80px;
	line-height: 60px;
    border-radius: 10px;
    font-size: 24px;
    color: #fff;
    border: 2px dashed #5E5F69;
    margin-bottom: 24px;
}

</style>