<template>
    <div class="page">
        <LayoutTable :data="tableData" :config="tableConfig">
            <#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>
        </LayoutTable>

        <#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>

        <#if table.detailShow??&&table.detailShow==1 >
        <dialog-show ref="dialogform" @ok="getData" />
        <#elseif table.detailShow??&&table.detailShow==2>
        <drawer-show ref="drawerform" @ok="getData" />
        <#else>
        <dialog-show ref="dialogform" @ok="getData" />
        </#if>
    </div>
</template>

<script>
    /** 表单弹出框模式需引入 */
    <#if table.detailShow??&&table.detailShow==1 >
    import dialogShow from "./dialogshow";
    <#elseif table.detailShow??&&table.detailShow==2>
    import drawerShow from "./drawershow";
    <#else >
    import dialogShow from "./dialogshow";
    </#if>
    import table from "@/assets/mixins/table";
    export default {
        name: "${ClassName}List",
        components: {
            <#if table.detailShow??&&table.detailShow==1 >
            dialogShow
            <#elseif table.detailShow??&&table.detailShow==2>
            drawerShow
            <#else >
            dialogShow
            </#if>},
        mixins: [table],
        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() {
                if(this.isExport) {
                    this.$message.info("数据正在导出中，请勿重复点击！")
                    return false;
                }
                this.isExport = true;
                this.$download("/${RequestMapping}/exportExcel", {
                "idList": this.selection,
                <#list columns as column>
                <#if column.isQuery == 1 >
                    '${column.javaField}': this.$route.query['${column.javaField}']<#sep>,
                </#if>
                </#list>
                }, { type: "excel" }).then(() => this.isExport = false).catch(error => {
                    this.isExport = false;
                    this.$message.error(error.message);
                });
            },
            </#if>
            <#if table.tplCategory=="sub">
            renderTable(tableData) {
                return (
                    <el-table stripe data={tableData} class="total-table">
                    {this.columnSet.map((item) => this.renderTableColumn(item))}
                    </el-table>
                );
            },
            renderTableColumn(options) {
                return (
                    <el-table-column
                        prop={options.prop}
                        label={options.label}
                        width={options.width}
                        formatter={options.formatter}
                    >
                    </el-table-column>
                 );
            },
            </#if>
            <#if table.detailShow??&&table.detailShow==1 >
            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.dialogform.add(row);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.$refs.dialogform.edit(row);
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.dialogform.view(row);
            },
            <#elseif table.detailShow??&&table.detailShow==2>
            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.drawerform.add(row);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.$refs.drawerform.edit(row);
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.drawerform.view(row);
            },
            <#else>
            /** 重写新增方法 */
            toAdd(row) {
                this.$refs.dialogform.add(row);
            },
            /** 重写编辑方法 */
            toEdit(row) {
                this.$refs.dialogform.edit(row);
            },
            /** 重写查看方法 */
            toView(row) {
                this.$refs.dialogform.view(row);
            },
            </#if>

        },
        data() {
            return {
                <#if table.isGenExport==1 >
                // 用户导入参数
                upload: {
                    // 是否显示弹出层（${functionName}导入）
                    open: false,
                    // 弹出层标题（${functionName}导入）
                    title: "导入${functionName}数据",
                    // 是否禁用上传
                    isUploading: false,
                    // 是否更新已经存在的数据
                    updateSupport: 0,
                    // 上传的地址
                    url: "/m/${RequestMapping}/importData",
                },
                isExport: false,
                </#if>
                <#if table.tplCategory=="sub">
                /** 子表列元素 */
                columnSet:[
                <#list subColumns as column>
                    <#--忽略主键列 -->
                    <#if column.columnName != subPkColumn.columnName && !column.isSuperColumn(column.javaField)>
                    <#assign comment=column.subComment(column.columnComment) />
                    <#if column.javaType == "Long">
                    {label: "${comment}", prop: "${column.javaField}", formatter: this.formatter},
                    </#if>
                    <#if column.javaType == "String">
                    {label: "${comment}", prop: "${column.javaField}"},
                    </#if>
                    <#if column.javaType == "Integer">
                    {label: "${comment}", prop: "${column.javaField}",formatter: this.formatter},
                    </#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.formatter},
                    </#if>
                    </#if>
                </#list>
                ],
                </#if>
                config: {
                    search: [
                        <#list columns as column>
                        <#assign comment=column.subComment(column.columnComment) />
                        <#if column.isQuery == 1 &&column.htmlType==1>
                        {
                            name: "${column.javaField}",
                            type: "text",
                            label: "${comment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==3>
                        {
                            name: "${column.javaField}",
                            type: "select",
                            label: "${comment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==4>
                        {
                            name: "${column.javaField}List",
                            type: "checkbox",
                            label: "${comment}",
                            fuzzy: true
                        },
                        <#elseif column.isQuery == 1 &&column.htmlType==6>
                        {
                            name: "${column.javaField}",
                            type: "date",
                            label: "${comment}",
                            fuzzy: true
                        },
                        </#if>
                        </#list>
                    ],
                    columns: [
                        {type: "selection", width: 60},
                        {type: "index",label: "序号",width: 50},
                        <#list columns as column>
                        <#assign comment=column.subComment(column.columnComment) />
                        <#if column.columnName != pkColumn.columnName && column.isList==1>

                        <#if column.javaType == "Long">
                        {label: "${comment}", prop: "${column.javaField}", formatter: this.formatter},
                        </#if>
                        <#if column.javaType == "String">
                        {label: "${comment}", prop: "${column.javaField}"},
                        </#if>
                        <#if column.javaType == "Integer">
                        {label: "${comment}", prop: "${column.javaField}",formatter: this.formatter},
                        </#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.formatter},
                        </#if>
                        </#if>
                        </#list>
                        <#if table.tplCategory=="sub">
                        {label: "${subFunctionName}",
                          width: 120,
                          prop: "subColumns",
                          formatter: (row) => {
                              let widthsize = this.columnSet.reduce((pre, cur) => {
                                  return pre + Number(cur.width);
                              }, 50);
                              return (
                                  <el-popover placement="right" width={widthsize} trigger="click">
                                      {this.renderTable(row.${subclassName}List)}
                                      <el-button type="text" slot="reference">详细</el-button>
                                  </el-popover>
                              );
                          },
                        },
                        <#elseif table.tplCategory=="subone">
                        {
                            prop: "${subclassName}Entity",
                            label: "${subFunctionName}",
                            align: "center",
                            subColumns: [
                                <#list subColumns as column>
                                <#if column.columnName != subPkColumn.columnName && !column.isSuperColumn(column.javaField)>
                                <#assign comment=column.subComment(column.columnComment) />
                                {prop:"${column.javaField}",label:"${comment}",width:100}<#sep>,
                                </#if>
                                </#list>
                            ],
                        },
                        </#if>
                        {
                            label: "操作",
                            width: 240,
                            formatter: row => {
                                return (
                                    <table-buttons noAdd row={row} onEdit={this.toEdit} onView={this.toView} onDel={this.toDel} />
                            );
                            }
                        }
                    ]
                }
            };
        }
    };
</script>