<template>
    <v-layout>
        <v-flex xs12>
            <v-card class="mb-2" color="white">
                <v-toolbar color="blue lighten-3" dark height="50px">
                    <v-toolbar-title>User : {{xcompany.name}}</v-toolbar-title>
                    <v-spacer></v-spacer>
                    <v-btn @click="openFormUser(0)" icon>
                        <v-icon>library_add</v-icon>
                    </v-btn>
                </v-toolbar>

                <v-layout row>
                    <v-flex xs12 pl-2 pr-2 pt-2 pb-2>
                        <v-data-table :headers="headers" :items="users" :loading="isLoading" :pagination.sync="pagination" class="elevation-1">
                            <template slot="items" slot-scope="props">
                                <td class="text-xs-left pa-2" v-html="props.item.M_UserUsername">
                                </td>
                                <td class="text-xs-left pa-2" v-html="props.item.M_UserFullName">
                                </td>
                                <td class="text-xs-left pa-2">
                                    <v-btn @click="editFormUser(props.item)" depressed small color="primary"><v-icon>edit</v-icon></v-btn>
                  <v-btn @click="deleteUser(props.item)" depressed small color="error"><v-icon>delete</v-icon></v-btn>
                                </td>
                            </template>
                            <template>
                                <div class="text-xs-center">
                                    <v-pagination v-model="page" :length="15" :total-visible="3"></v-pagination>
                                </div>
                            </template>
                        </v-data-table>
                    </v-flex>
                </v-layout>

                <template>
                    <v-layout row justify-center>
                        <v-dialog v-model="dialoguser" persistent max-width="600px">
                            <v-card>
                                <v-card-title>
                                    <span class="headline">FORM USER</span>
                                </v-card-title>
                                <v-card-text class="pt-0 pb-0">
                                    <v-form ref="formcompanyuser" v-model="validuser" lazy-validation>
                                        <v-layout wrap>
                                            <v-flex xs12>
                                                KELOMPOK PELANGGAN : {{xcompany.name}} <span> 
                                            </v-flex>
                                            <v-flex xs12>
                                                <v-text-field label="User Name" v-model="username"></v-text-field>
                                                <p v-if="checkError('requirename')" class="error pl-2 pr-2" style="color:#fff">User Name harus diisi dong</p>
                                            </v-flex>
                                            <v-flex xs12>
                                                <v-text-field label="Nama Lengkap" v-model="userfullname"></v-text-field>
                                             </v-flex>
                                            <v-flex xs12>
                                                <v-text-field v-if="this.xid == 0" label="Password" v-model="userpassword" type="password"></v-text-field>
                                                <p v-if="checkError('requirepassword')" class="error pl-2 pr-2" style="color:#fff">Password harus diisi dong</p>
                                            </v-flex>
                                        </v-layout>
                                </v-card-text>
                                <v-card-actions>
                                    <v-spacer></v-spacer>
                                    <v-btn color="blue darken-1" flat @click="updateDialogFormUser()">Tutup</v-btn>
                                    <v-btn color="blue darken-1" flat @click="saveFormUser()">Simpan</v-btn>
                                </v-card-actions>
                                </v-form>
                            </v-card>
                        </v-dialog>
                    </v-layout>
                </template>

            </v-card>
            </v-flex>

<one-dialog-print :title="printtitle" :width="printwidth" :height="500" :status="openprint" :urlprint="urlprint" @close-dialog-print="closePrint"></one-dialog-print>
<template>

          <v-dialog v-model="dialogdeletealertuser" max-width="30%">
            <v-card>
              <v-card-title class="headline grey lighten-2 pt-2 pb-2" primary-title>
                Peringatan !
              </v-card-title>
              <v-card-text class="pt-2 pb-2">
                <v-layout row>
                  <v-flex xs12 d-flex>
                    <v-layout row>
                      <v-flex pb-1 xs12>
                        <v-layout row>
                          <v-flex pt-2 pr-2 xs12>
                            {{msgalertuser}}
                          </v-flex>
                        </v-layout>
                      </v-flex>
                    </v-layout>
                  </v-flex>
                </v-layout>
              </v-card-text>
              <v-divider></v-divider>
              <v-card-actions>
                <v-spacer></v-spacer>
                <v-btn color="primary" flat @click="dialogdeletealertuser = false">
                  Tutup
                </v-btn>
                <v-btn color="primary" flat @click="closeDeleteAlertUser()">
                  Yakin lah
                </v-btn>
              </v-card-actions>
            </v-card>
          </v-dialog>

        </template>

    </v-layout>
</template>

<style scoped>
    .searchbox .v-input.v-text-field .v-input__slot {
        min-height: 60px;
    }

    .searchbox .v-btn {
        min-height: 60px;
    }

    table.v-table tbody td,
    table.v-table tbody th {
        height: 40px;
    }

    table.v-table thead tr {
        height: 40px;
    }
</style>

<script>
    module.exports = {
        components: {
            'one-date-picker': httpVueLoader('../../common/oneDatePicker.vue'),
            'one-dialog-print': httpVueLoader('../../common/oneDialogPrintX.vue')
        },
        data() {
            return {
                query: "",
                items: [],
                openprint: false,
                urlprint: '',
                printtitle: '',
                printwidth: 600,
                formatreport: 'pdf',
                page: 1,
                headers: [{
                        text: "USER",
                        align: "Center",
                        sortable: false,
                        value: "lab",
                        width: "18%",
                        class: "blue lighten-4"
                    },
                    {
                        text: "NAMA",
                        align: "Center",
                        sortable: false,
                        value: "name",
                        width: "25%",
                        class: "blue lighten-4"
                    },
                    {
                        text: "AKSI",
                        align: "Center",
                        sortable: false,
                        value: "status",
                        width: "18%",
                        class: "blue lighten-4"
                    }
                ],
                pagination: {
                    descending: false,
                    page: 1,
                    rowsPerPage: 5,
                    sortBy: 'trx_id ASC',
                    totalItems: this.$store.state.user.total_transactions
                },
                statuss: [{
                        waktu: '16-07-2019 10:00',
                        status: 'Order Dibuat',
                        user: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 11:00',
                        status: 'Order Dikirim',
                        user: 'Nagita Slavina'
                    },
                    {
                        waktu: '16-07-2019 12:00',
                        status: 'Order dikonfirmasi',
                        user: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 13:00',
                        status: 'Telah dibuat Surat Jalan',
                        user: 'Chacha Frederica'
                    },
                    {
                        waktu: '16-07-2019 14:00',
                        status: 'Kurir akan mengambil sampel',
                        user: 'Satria Subiantoro'
                    }
                ],
                color: "success",
                validuser: false,
                xid: 0,
                countprice: 0,
                username: "",
                userfullname: "",
                userpassword: "",
                number: "",
                refnumber: "",
                mindp: "",
                agingnote: "",
                xnote: "",
                useremail: "",
                startdate: "",
                enddate: "",
                isbill: "",
                isdefault: "",
                isfavorit: "",
                isaging: "",
                isemail: "",
                menustartdate: false,
                menuenddate: false,
                date: new Date().toISOString().substr(0, 10),

                dialogdeletealertuser: false,
                dialogverifyalertuser: false,
                dialogreleasealertuser: false,
                alertverify: false,
                dialogunreleasealertuser: false,
                dialogunverifyalertuser: false,
                msgalertuser: ""
            };
        },
        mounted() {
            this.$store.dispatch("user/selectbase")
            this.$store.dispatch("user/selectomzettype")
            this.$store.dispatch("user/selectusertype")
            this.$store.dispatch("user/selectagingtype")
        },
        computed: {
            users() {
                return this.$store.state.user.users
            },
            xcompany() {
                return this.$store.state.company.selected_company
            },
            dialoguser() {
                return this.$store.state.user.dialog_form_user
            },
            xerrors() {
                return this.$store.state.user.errors
            },
            isLoading() {
                return this.$store.state.user.search_status == 1
            },
            startComputedDateFormatted() {
                return this.formatDate(this.xstartdate)
            },
            xstartdate: {
                get() {
                    return this.$store.state.user.startdate
                },
                set(val) {
                    this.$store.commit("user/update_startdate", val)
                }
            },
            endComputedDateFormatted() {
                return this.formatDate(this.xenddate)
            },
            xenddate: {
                get() {
                    return this.$store.state.user.enddate
                },
                set(val) {
                    this.$store.commit("user/update_enddate", val)
                }
            },
            xbases() {
                return this.$store.state.user.bases
            },
            xbase: {
                get() {
                    return this.$store.state.user.base
                },
                set(val) {
                    this.$store.commit("user/update_base", val)
                }

            },
            xomzettypes() {
                return this.$store.state.user.omzettypes
            },
            xomzettype: {
                get() {
                    return this.$store.state.user.omzettype
                },
                set(val) {
                    this.$store.commit("user/update_omzettype", val)
                }

            },
            xusertypes() {
                return this.$store.state.user.usertypes
            },
            xusertype: {
                get() {
                    return this.$store.state.user.usertype
                },
                set(val) {
                    this.$store.commit("user/update_usertype", val)
                }

            },
            xagingtypes() {
                return this.$store.state.user.agingtypes
            },
            xagingtype: {
                get() {
                    return this.$store.state.user.agingtype
                },
                set(val) {
                    this.$store.commit("user/update_agingtype", val)
                }

            },
            xstatuss() {
                return this.$store.state.user.statuss
            },
            dialogstatusorder() {
                return this.$store.state.user.dialog_status_order
            }
        },
        methods: {
            updateDialogFormUser() {
                this.$store.commit("user/update_dialog_form_user", false)
            },
            updateDialogStatusOrder() {
                this.$store.commit("user/update_dialog_status_order", false)
            },
            setStatusOrder(val) {
                this.$store.commit("user/update_users", {})
                this.$store.commit("user/update_dialog_status_order", true)
                this.$store.commit("user/update_statuss", val.statuss)
            },
            doPriceList(val) {
                console.log(location)
                var id = val.id
                location.replace("/one-ui/test/vuex/one-md-price/" + "?id=" + id)
            },
            doPrice() {
                console.log(location)
                var id = this.xid
                location.replace("/one-ui/test/vuex/one-md-price/" + "?id=" + id)
            },
            doPrint() {
                console.log('doprint')
                this.printwidth = 1028
                this.printtitle = ""
                let user = one_user()
                var id = this.xid
                var d = new Date();
                var n = d.getTime()
                var rptname = 'test_price'
                var formatrpt = this.formatreport

                this.urlprint = "/birt/run?__report=report/one/md/" + rptname + ".rptdesign&__format=" +
                    formatrpt + "&PUserID=" + id  + "&username=" + user.M_UserUsername + "&tm=" + n

                this.openprint = true
            },
			closePrint() {
                this.openprint = false
            },
            openFormUser(val) {
                this.xid = val
                this.username = ''
                this.userfullname = ''
                this.userpassword = ''
                this.$store.commit("user/update_dialog_form_user", true)
            },
            editFormUser(val) {
                this.xid = val.id
                this.username = val.M_UserUsername
                this.userfullname = val.M_UserFullName
                this.userpassword = ''
                this.$store.commit("user/update_dialog_form_user", true)
            },
            checkError(value) {
                var errors = this.$store.state.user.errors
                if (errors.includes(value)) {
                    return true
                } else {
                    return false
                }
            },
            saveFormUser() {
                this.$store.commit("user/update_errors", [])
                var errors = this.$store.state.user.errors
                if (this.username === '') {
                    errors.push("requirename")
                }
                if (errors.length === 0) {
                    this.$store.dispatch("user/save", {
                        xid: this.xid,
                        companyid: this.$store.state.company.selected_company.id,
                        companyname: this.$store.state.company.selected_company.name,
                        usergrup: 11,
                        username: this.username,
                        userfullname: this.userfullname,
                        password: this.userpassword
                    })
                }
            },
            set_date2(x) {
                this.startdate = x.new_date
            },
            set_date3(x) {
                this.enddate = x.new_date
            },
            formatDate(date) {
                if (!date) return null

                const [year, month, day] = date.split('-')
                return `${day}-${month}-${year}`
            },
            deFormatedDate(date) {
                if (!date) return null

                const [day, month, year] = date.split('-')
                return `${year}-${month.padStart(2, '0')}-${day.padStart(2, '0')}`
            },
            updateAlert_success(val) {
                this.$store.commit("company/update_alert_success", val)
            },
            deleteUser(data) {
                this.xid = data.id

                this.msgalertuser = "Yakin, mau hapus user ini?"
                this.dialogdeletealertuser = true
            },
            closeDeleteAlertUser() {
                this.$store.dispatch("user/delete", {
                    xid: this.xid,
                    companyid: this.$store.state.company.selected_company.id,
                    companyname: this.$store.state.company.selected_company.name,
                    name: this.username
                })
                this.dialogdeletealertuser = false
            }
        }
    }
</script>