﻿<template>
    <div class="container-fluid">
        <v-card>
            <v-card-title class="bg-success white--text text-center">
                <h5 class="mb-0">統計報表</h5>
                <v-spacer></v-spacer>
                <!--<v-btn color="primary" @click="submitData">確認送出</v-btn>-->
                <v-btn color="primary" class="ml-2" @click="closeWindow">關閉</v-btn>
            </v-card-title>
            <v-card-text>
                <v-row>
                    <v-col cols="2" md="2">
                        <v-select :items="category"
                                  label="統計分類"
                                  item-text="name"
                                  item-value="id" v-model="category_id">
                        </v-select>
                    </v-col>
                    <v-col cols="2" md="2">
                        <v-text-field label="法會時間(起)" v-model="query.s_date"></v-text-field>
                    </v-col>
                    <v-col cols="2" md="2">
                        <v-text-field label="法會時間(迄)" v-model="query.e_date"></v-text-field>
                    </v-col>
                    <v-col cols="2" md="2">
                        <v-btn @click.prevent="getRpt()" :disabled="isdisabled">
                            匯出
                        </v-btn>
                        <img src="/admin/images/loading.gif" v-show="isloading"/>
                    </v-col>
                </v-row>
            </v-card-text>
        </v-card>
        <v-dialog v-model="message_dialog.show" class="dialog_width">
            <v-card>
                <v-card-title>
                    {{ message_dialog.message }}
                </v-card-title>
                <v-card-text>
                    <v-btn @click.prevent="message_dialog.show=false" v-if="!message_dialog.show_confirm">關閉</v-btn>

                </v-card-text>
            </v-card>
        </v-dialog>
        </div>
</template>
<script>
    module.exports = {
        data() {
            return {
                message_dialog: { show: false, show_confirm: false, message: "" },
                category: [
                    { id: "01", name: "依法會+功德項目", selected: "" },
                    { id: "02", name: "依信眾", selected: "" },
                    { id: "03", name: "依法會+信眾", selected: "" },
                ],
                category_id: "",
                isdisabled: false,
                isloading: false,
                query: {s_date:"",e_date:""}
            }
        },
        methods: {
            closeWindow() {
                this.$emit('close-dialog');
            },
            getRpt() {
                if (this.category_id == "") {
                    this.message_dialog.message = "請選擇統計類型";
                    this.message_dialog.show = true;
                    return false;
                }
                const params = { statistic_mode: this.category_id }
                const search = {
                    num: 0, kind: 0, kindName: "", order_no: "", u_name: "", pay_type: "", subject: "", amount: 0, price: 0, pay_mode: "", status: "",
                    acc_num: 0, f_num: 0, acc_kind: 0, check_amount: 0, remain_amount: 0, check_status: "", actItem_num: 0, draft: "", d_nnum: 0
                }
                this.isdisabled = true;
                this.isloading = true;
                axios
                    .post(HTTP_HOST + 'api/statistics/GetStatistic', search, { params: params })
                    .then(response => {
                        if (response.data.result == "N") {
                            this.message_dialog.message = response.data.message;
                            this.message_dialog.show = true;
                        } else if (response.data.result == "Y") {
                            const link = document.createElement('a');
                            link.href = HTTP_HOST + "/" + response.data.filename;
                            link.target = "_blank";

                            // 5. Specify the filename for the download
                            //link.setAttribute('download', 'my-downloaded-file.pdf');

                            // 6. Append link to the body, click it, and remove it immediately
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);

                            // 7. Free up memory by revoking the object URL
                            //window.URL.revokeObjectURL(blobUrl);
                            //window.open(HTTP_HOST +"/" +response.data.filename, "_blank");
                        }
                        this.isdisabled = false;
                        this.isloading = false;

                    })
                    .catch(error => console.log(error))
            }
        }
    }
</script>
<style>
    .v-dialog {
        width: 80% !important;
    }
</style>