<template>
    <div>
        <el-dialog width="25%" top="25vh" :title="`${title}数据上报时间段`" :visible="visible" :before-close="handleClose">
            <el-form>
                <el-form-item label="上报时间">
                    <el-date-picker @change="changeDate" v-model="scopeTime" style="width:230px" size="mini"
                        :picker-options="pickerOptions" value-format="yyyy-MM-dd" type="daterange" clearable
                        range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="handleSave">导 出 excel</el-button>
                <el-button @click="handleClose">取 消</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
// import swStQuery from '@/api/monitor/resMonitor/swStQuery.js'
	import wyQuery from '@/api/monitor/gqMonitor/wyQuery.js'
export default {
    name: 'TimeSelection',
    props: {
        appType: {
            type: String,
            default: ''
        },
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    //disabledDate 文档上：设置禁用状态，参数为当前日期，要求返回 Boolean
                    return time.getTime() > Date.now()//选当前时间之前的时间
                    //return time.getTime() < Date.now()//选当前时间之后的时间
                }
            },
            visible: false,
            title: '选择',
            scopeTime: '',
            queryParam: {
                pageBean: {
                    pageSize: 20,
                    page: 1
                },
                params: {
                    appType: null,
                    startTime: null,
                    endTime: null
                }
            },
        }
    },
    created() {
       
    },
    methods: {
        //默认时间
        getCurrentMonthLast() {
            var moment = require('moment') //打开时默认时间为一天前至获取当前年月日时分秒
            let endTime = moment().format('YYYY-MM-DD') //当前年月日
            let startTime = moment()
                .subtract(1, 'days')
                .format('YYYY-MM-DD')
            this.scopeTime = [startTime, endTime]
            let time = moment().format('LTS') //当前时分秒
            this.queryParam.params.startTime = this.scopeTime[0] + ' ' + '00:00:00'
            this.queryParam.params.endTime = this.scopeTime[1] + ' ' + time
        },
        openShare(val) {
			console.log(val);
			this.queryParam.params=val
            this.visible = true
			this.queryParam.params.appType = this.appType
			this.getCurrentMonthLast()
        },
        changeDate(val) {
            if (val) {
                var moment = require('moment') //打开时默认时间为一天前至获取当前年月日时分秒
                let time = moment().format('LTS') //当前时分秒
                this.queryParam.params.startTime = val[0] + ' ' + '00:00:00'
                this.queryParam.queryParam.endTime = val[1] + ' ' + time
            } else {
                this.queryParam.params.startTime = ''
                this.queryParam.params.endTime = ''
            }
        },
        // 导出数据
        handleSave() {
            //导出excl
            const loading = this.$loading({
                lock: true,
                text: '导出中...',
                spinner: 'el-icon-loading',
                background: 'rgba(0, 0, 0, 0.7)'
            });
            let param = {
                groupRelation: "AND",
                groupTree: {},
                pageBean: {
                    page: 1,
                    pageSize: -1,
                    showTotal: true
                },
                params:this.queryParam.params,
                // querys: [
                //     {
                //         group: "main",
                //         operation: "EQUAL",
                //         parentGroup: "",
                //         property: "",
                //         relation: "AND",
                //         value: {}
                //     }
                // ],
                sorter: [
                    {
                        direction: "ASC",
                        property: ""
                    }
                ]
            }
            wyQuery.resDataDerived(param, res => {
				if (res.type === 'application/json') {
					const fileReader = new FileReader()
					fileReader.onloadend = () => {
						const jsonData = JSON.parse(fileReader.result)
						this.$message.error(jsonData.message)
					}
					fileReader.readAsText(res)
				} else {
					this.$message.success('导出成功');
					this.handleClose()
				}
				loading.close();
            })
        },
        handleClose() {
            this.getCurrentMonthLast()
            this.queryParam = {
                pageBean: {
                    pageSize: 20,
                    page: 1
                },
                params: {
                    appType: null,
                    startTime: null,
                    endTime: null
                }
            }
            this.visible = false
        }


    },
}
</script>
<style lang="scss" scoped>
</style>
