<template>
    <theme-table :noneActions="true">
        <span slot="main-title">
            {{ $t('Branch') }}
        </span>

        <template slot="more-button">
            <input type="search" class="rounded form-control" style="width: 230px;" @input="Searching" :placeholder="$t('Search')" autocomplete="off" />
        </template>

        <div slot="body">
            <div class="table-responsive table_scroll">
                <table class="table table-bordered">
                    <thead class="text-nowrap fixed_table">
                        <tr>
                            <th>{{ $t("#") }}</th>
                            <th>{{ $t("Name EN") }}</th>
                            <th>{{ $t("Name KM") }}</th>
                            <th>{{ $t("Phone") }}</th>
                            <th>{{ $t("Address") }}</th>
                            <th>{{ $t("Action") }}</th>
                        </tr>
                    </thead>
                    <tbody class="text-nowrap">
                        <tr v-for="(bran, index) in branches.data.filter(b => b.status_id != 2)"
                            :key="bran.id">
                            <td>{{ index + 1 }}</td>
                            <td>{{ bran.name_en }}</td>
                            <td>{{ bran.name_km }}</td>
                            <td>{{ bran.phone }}</td>
                            <td>{{ bran.address }}</td>
                            <td>
                                <router-link v-if="$can('Branch Edit')" :to="{ name: 'BranchCreate',params:{id:bran.id} }" :title="$t('Edit')" class="btn btn-secondary s-btn-xs"><i class="fa-solid fa-pen-to-square fa-sm"></i></router-link>
                                <!-- <button type="button" :title="$t('Delete')" @click="funDelete(bran.id)"
                                    class="btn btn-danger  s-btn-xs">
                                    <i class="fa-solid fa-trash-can fa-sm"></i>
                                </button> -->
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
            <cumstom-pagination :pageShow="true" @PageChange="PageChange($event)" :data="branches" @onChangePage="funGetData($event)"></cumstom-pagination>
        </div>
    </theme-table>
</template>
<script>
    import ActiveStatus from "../../components/ActiveStatus.vue";
    
    import CumstomPagination from "../../components/CustomPagination.vue";
    import ThemeTable from '../../components/ThemeTable.vue';
    export default {       
        components: {
            ActiveStatus,
            
            CumstomPagination,
            ThemeTable
        },
        data() {
            return {
                branches: [],
                form: new Form({ 
                    id: "",
                    name_en: "",
                    name_km: "",
                    phone: "",
                    address: "",
                }),
                params: {
                    page: 1,
                    per_page: 25,
                },
            };
        },

        created() {
            this.funGetData();
        },
        methods: {
            funDelete(id) {
                this.$confirm({
                    message: "Are you sure?",
                    button: {
                        no: "No",
                        yes: "Yes",
                    },
                    callback: (confirm) => {
                        if (confirm) {
                            this.form.delete("admin-api/branches/" + id).then((res) => {
                                if (res.data.status == 1) {
                                    this.$toast.success(res.data.message);
                                    this.funGetData();
                                } else {
                                    this.$toast.warning(res.data.message);
                                }
                            });
                        }
                    },
                });
            },
            funGetData(page = 1) {
                this.params.page = page;
                axios
                    .get("admin-api/branches", {
                        params: this.params,
                    })
                    .then((res) => {
                        this.branches = res.data;
                    })
                    .catch((error) => {
                        if (error.message == "Request failed with status code 403") {
                            this.$router.push({ name: "permissionTest" });
                        } else {
                            console.log(error.message);
                        }
                    });
            },

            submitData() {
                this.form
                    .post("admin-api/branches")
                    .then((res) => {
                        if (res.data.status == 1) {
                            this.form.reset();
                            this.$toast.success(res.data.message);
                            $("#BranchForm").modal("hide");
                            this.funGetData();
                        } else {
                            this.$toast.warning(res.data.message);
                        }
                    })
                    .catch((error) => {
                        console.log(error);
                    });
            },
            funForm(id) {
                axios
                    .get("admin-api/branches/create", {
                        params: { id: id },
                    })
                    .then((res) => {
                        if (res.data.status == 1) {
                            this.form.fill(res.data.data);
                        } else {
                            this.form.reset();
                        }
                        $("#BranchForm").modal("show");
                    });
            },

            PageChange: function (e) {
                this.params.per_page = e.target.value;
                this.funGetData();
            },
            Searching(e) {
                // this.params.search_text = e.target.value;
                this.funGetData();
            },
        },
    };
</script>
