<template>
    <div class="page">
        <LayoutTable ref="layoutTable" :data="tableData" :config="tableConfig" />
        <dialog-show ref="dialogform" @ok="getData" />
            <#if table.isGenExport==1 >
            <el-button
                    slot="table-head-left2"
                    style="margin-left: 10px"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="handleImport"
            >导入</el-button>
            <el-button
                    slot="table-head-left2"
                    style="margin-left: 10px"
                    icon="el-icon-tickets"
                    size="mini"
                    @click="doExport"
                    :disabled="isExport"
            >导出</el-button>
           </#if>

        <#if table.isGenExport==1 >
        <!-- ${functionName}导入对话框 -->
        <el-dialog
                :title="upload.title"
                :visible.sync="upload.open"
                width="400px"
                append-to-body
        >
            <el-upload
                    ref="upload"
                    :limit="1"
                    accept=".xlsx, .xls"
                    :headers="upload.headers"
                    :action="upload.url + '?updateSupport=' + upload.updateSupport"
                    :disabled="upload.isUploading"
                    :on-progress="handleFileUploadProgress"
                    :on-success="handleFileSuccess"
                    :auto-upload="false"
                    drag
            >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                    将文件拖到此处，或
                    <em>点击上传</em>
                </div>
                <div class="el-upload__tip" slot="tip">
                    <el-checkbox
                            v-model="upload.updateSupport"
                    />是否更新已经存在的数据
                    <el-link type="primary" style="font-size: 14px" @click="downloadTemplate"
                    >下载模板</el-link
                    >
                </div>
                <div class="el-upload__tip" style="color: red" slot="tip">
                    提示：仅允许导入“xls”或“xlsx”格式文件！
                </div>
            </el-upload>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitFileForm">确 定</el-button>
                <el-button @click="upload.open = false">取 消</el-button>
            </div>
        </el-dialog>
        </#if>
    </div>
</template>

<script>
    import { handleTree } from "@/assets/utils/table";
    import Treeselect from "@riophae/vue-treeselect";
    import "@riophae/vue-treeselect/dist/vue-treeselect.css";
    import table from "@/assets/mixins/table";
    import dialogShow from "./dialogshow";
    export default {
        name: "${ClassName}",
        mixins: [table],
        components: { Treeselect, dialogShow },
        created() {
        },
        methods: {
            <#if table.isGenExport==1 >
            /** 导入 */
            handleImport() {
                this.upload.title = "${functionName}导入";
                this.upload.open = true;
            },
            /** 下载模板操作 */
            downloadTemplate() {
                this.isExport = true;
                this.$download("/${RequestMapping}/downloadTemplate", {}, { type: "excel" })
                    .then(() => (this.isExport = false))
                    .catch((error) => {
                        this.isExport = false;
                        this.$message.error(error.message);
                    });
            },
            /** 文件上传中处理 */
            handleFileUploadProgress(event, file, fileList) {
                this.upload.isUploading = true;
            },
            /** 文件上传成功处理 */
            handleFileSuccess(response, file, fileList) {
                this.upload.open = false;
                this.upload.isUploading = false;
                this.$refs.upload.clearFiles();
                this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
                this.getData();
            },
            /** 提交上传文件 */
            submitFileForm() {
                this.$refs.upload.submit();
            },
            /** 导出Excel */
            doExport() {
                this.isExport = true;
                this.$download("/${RequestMapping}/exportExcel", {
                <#list columns as column>
                <#if column.isQuery == 1 >
                    'query.${column.javaField}': this.$route.query['query.${column.javaField}']<#sep>,
                </#if>
                </#list>
                }, { type: "excel" }).then(() => this.isExport = false).catch(error => {
                    this.isExport = false;
                    this.$message.error(error.message);
                });
            },
            </#if>
            afterRender(data) {
                data.data = handleTree(data.data, "${treeCode}","${treeParentCode}");
                this.${className}Options = data.data;
                this.$refs.layoutTable.showType="treetable"
            },

            handleAdd(row) {
                this.$refs.dialogform.add(row, this.${className}Options);
            },

            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.dialogform.add(row, this.${className}Options);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.loading = true;
                this.$post(this.pageInfo.exclude, {${pkColumn.javaField} : row.${pkColumn.javaField} })
                    .then((res) => {
                        if (res && res.code && res.code == 1) {
                            this.${className}Options = handleTree(res.data.data);
                            this.$refs.dialogform.edit(row, this.${className}Options);
                            this.loading = false;
                        }
                    })
                    .catch((error) => {
                        this.$message.error(error.message);
                    });
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.dialogform.view(row, this.${className}Options);
            },
        },
        data() {
            return {
                <#if table.isGenExport==1 >
                // 用户导入参数
                upload: {
                    // 是否显示弹出层（${functionName}导入）
                    open: false,
                    // 弹出层标题（${functionName}导入）
                    title: "导入${functionName}数据",
                    // 是否禁用上传
                    isUploading: false,
                    // 是否更新已经存在的数据
                    updateSupport: 0,
                    // 上传的地址
                    url: "/m/${RequestMapping}/importData",
                },
                isExport: false,
                </#if>
                config: {
                    /** 树表是否默认展开 */
                    expand:true,
                    showType: "treetable",
                    search: [
                        <#list columns as column>
                        <#if column.isQuery == 1 &&column.htmlType==1>
                        {
                            name: "${column.javaField}",
                            type: "text",
                            label: "${column.columnComment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==3>
                        {
                            name: "${column.javaField}",
                            type: "select",
                            label: "${column.columnComment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==4>
                        {
                            name: "${column.javaField}",
                            type: "checkbox",
                            label: "${column.columnComment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==6>
                        {
                            name: "${column.javaField}",
                            type: "date",
                            label: "${column.columnComment}",
                            fuzzy: true
                        },
                        </#if>
                        </#list>
                    ],
                    columns: [
                        <#list columns as column>
                        <#if column.columnName != pkColumn.columnName && !column.isSuperColumn(column.javaField)&& column.isList==1>
                        <#assign comment=column.subComment(column.columnComment) />
                        <#if column.javaType == "Long">
                        {label: "${comment}", prop: "${column.javaField}", formatter: this.formatterString},
                        </#if>
                        <#if column.javaType == "String"||column.javaType == "Integer">
                        {label: "${comment}", prop: "${column.javaField}"},
                        </#if>
                        <#if column.javaType == "Date">
                        {label: "${comment}", prop: "${column.javaField}", formatter: this.formatterDate},
                        </#if>
                        <#if column.javaType == "BigDecimal">
                        {label: "${comment}", prop: "${column.javaField}", formatter: this.formatterMoney},
                        </#if>
                        </#if>
                        </#list>

                        {
                            label: "操作",
                            width: 280,
                            formatter: row => {
                                return (
                                    <div>
                                        <el-button
                                            size="mini"
                                            type="text"
                                            icon="el-icon-edit-outline"
                                            onClick={() => {
                                                this.handleAdd(row);
                                            }}
                                        >
                                            新增
                                        </el-button>
                                        <table-buttons
                                            noAdd
                                            noView
                                            row={row}
                                            onEdit={this.toEdit}
                                            onView={this.toView}
                                            onDel={this.toDel}
                                        />
                                    </div>
                                );
                            }
                        }
                    ]
                }
            };
        }
    };
</script>
<style lang="scss">
    .ml-row {
        margin-bottom: 5px;
    }

    .el-radio-button__inner {
    // width: 82px;
        height: 36px;
        border-radius: 4px 0px 0px 4px;
        border: 1px solid rgba(0, 0, 0, 0) !important;
        border-radius: 2px !important;
        background-color: rgba(0, 0, 0, 0);
        font-size: 14px;
        font-weight: 400;
        color: #606677;
        line-height: 14px;
        outline: none;
        box-shadow: none;
    }

    .el-radio-button__orig-radio:checked+.el-radio-button__inner {
        background: rgba(0, 0, 0, 0);
        border: 1px solid #3E7BFA !important;
        color: #3E7BFA;
        line-height: 14px;
        outline: none;
        box-shadow: none;
    }

    .ml-top-button {
        border-color: #ffffff;
        background: rgba(0, 0, 0, 0);
        font-size: 16px;
        margin: 0 10px 0 10px;
    }
</style>