<template>
    <el-card class="tjm_card_style_custom">
        <div class="tjm_card_tools">
            <div class="tjm_card_tools_left">
                <el-button type="primary" icon="plus" @click="table_add">新增</el-button>
            </div>
        </div>

        <div class="tjm_card_table">
            <el-table v-loading="loading" row-key="id" header-cell-class-name="tjm_card_table_header" height="400" :data="tableData" border>
                <el-table-column type="index" label="序号" width="55"></el-table-column>
                <el-table-column label="模板名称" prop="templateName"></el-table-column>
                <el-table-column label="模板类型">
                    <template #default="scope">{{ formatType(scope.row.refType) }}</template>
                </el-table-column>

                <el-table-column label="操作" fixed="right" width="240">
                    <template #default="scope">
                        <el-button type="primary" link icon="edit" @click="table_edit(scope.row)">修改</el-button>
                        <el-button type="primary" link icon="delete" @click="table_del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
    </el-card>

    <temp-dialog v-if="dialog" ref="tempDialog" @success="reloadTable" @closed="closed"></temp-dialog>
</template>

<script>
import API from "@/api/system/template";
import { typeDic } from "./main";
import tempDialog from "./dialog.vue";

export default {
    components: {
        tempDialog
    },

    data() {
        return {
            loading: false,
            tableData: [],

            dialog: false
        }
    },

    mounted() {
        this.reloadTable();
    },

    methods: {
        formatType(type) {
            return typeDic[type] || "";
        },

        reloadTable() {
            this.loading = true;
            API.get().then(res => {
                this.loading = false;
                if (res.code === 200) this.tableData = res.data.records;
                else ElMessage.error(res.msg);
            }).catch(() => this.loading = false);
        },

        table_add() {
            this.dialog = true;
            nextTick(() => this.$refs.tempDialog.open());
        },

        table_edit(row) {
            this.dialog = true;
            nextTick(() => this.$refs.tempDialog.open("edit").setData(row));
        },

        table_del(row) {
            ElMessageBox.confirm(`是否确认删除"${row.templateName}"?`, "删除警告", {
                type: "warning",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(() => {
                API.del({ ids: row.id }).then(res => {
                    if (res.code == 200) {
                        ElMessage.success("操作成功");
                        this.reloadTable();
                    } else ElMessage.error(res.msg);
                });
            }).catch(() => ElMessage.info("已取消"));
        },

        closed(e) {
            e && this.reloadTable();
            this.dialog = false;
        }
    }
}
</script>