<template>
    <div class="page-container list-container">
        <!-- 功能按钮区 -->
        <div class="list-btns-container">
            <el-button
                    type="primary"
                    size="small"
                    icon="Plus"
                    @click="handleAdd"
                    v-hasPermi="['${moduleName}:${businessName}:add']"
            >新增</el-button>

            <el-button
                    type="success"
                    size="small"
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['${moduleName}:${businessName}:edit']"
            >修改</el-button>

            <el-button
                    type="danger"
                    size="small"
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['${moduleName}:${businessName}:remove']"
            >删除</el-button>

            <el-button
                    type="warning"
                    size="small"
                    icon="Download"
                    @click="handleExport"
                    v-hasPermi="['${moduleName}:${businessName}:export']"
            >导出</el-button>
            <!--<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
        </div>
        <!-- 搜索区 -->
        <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
            #foreach($column in $columns)
                #if($column.query)
                    #set($dictType=$column.dictType)
                    #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
                    #set($parentheseIndex=$column.columnComment.indexOf("（"))
                    #if($parentheseIndex != -1)
                        #set($comment=$column.columnComment.substring(0, $parentheseIndex))
                    #else
                        #set($comment=$column.columnComment)
                    #end
                    #if($column.htmlType == "input")
                        <el-form-item label="${comment}：" prop="${column.javaField}">
                            <el-input
                                    v-model="queryParams.${column.javaField}"
                                    placeholder="请输入${comment}"
                                    clearable
                                    @keyup.enter="handleQuery"
                            />
                        </el-form-item>
                    #elseif(($column.htmlType == "select" || $column.htmlType == "radio") && "" != $dictType)
                        <el-form-item label="${comment}：" prop="${column.javaField}">
                            <el-select v-model="queryParams.${column.javaField}" placeholder="请选择${comment}" clearable>
                                <el-option
                                        v-for="dict in ${dictType}"
                                        :key="dict.value"
                                        :label="dict.label"
                                        :value="dict.value"
                                />
                            </el-select>
                        </el-form-item>
                    #elseif(($column.htmlType == "select" || $column.htmlType == "radio") && $dictType)
                        <el-form-item label="${comment}：" prop="${column.javaField}">
                            <el-select v-model="queryParams.${column.javaField}" placeholder="请选择${comment}" clearable>
                                <el-option label="请选择字典生成" value="" />
                            </el-select>
                        </el-form-item>
                    #elseif($column.htmlType == "datetime" && $column.queryType != "BETWEEN")
                        <el-form-item label="${comment}：" prop="${column.javaField}">
                            <el-date-picker clearable
                                            v-model="queryParams.${column.javaField}"
                                            type="date"
                                            value-format="YYYY-MM-DD"
                                            placeholder="请选择${comment}">
                            </el-date-picker>
                        </el-form-item>
                    #elseif($column.htmlType == "datetime" && $column.queryType == "BETWEEN")
                        <el-form-item label="${comment}：" style="width: 308px">
                            <el-date-picker
                                    v-model="daterange${AttrName}"
                                    value-format="YYYY-MM-DD"
                                    type="daterange"
                                    range-separator="-"
                                    start-placeholder="开始日期"
                                    end-placeholder="结束日期"
                            ></el-date-picker>
                        </el-form-item>
                    #end
                #end
            #end
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <!-- 列表区 -->
        <el-table v-loading="loading" :data="${businessName}List" size="small" border height="100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            #foreach($column in $columns)
                #set($javaField=$column.javaField)
                #set($parentheseIndex=$column.columnComment.indexOf("（"))
                #if($parentheseIndex != -1)
                    #set($comment=$column.columnComment.substring(0, $parentheseIndex))
                #else
                    #set($comment=$column.columnComment)
                #end
                #if($column.pk)
                    <el-table-column label="${comment}" align="center" prop="${javaField}" />
                #elseif($column.list && $column.htmlType == "datetime")
                    <el-table-column label="${comment}" align="center" prop="${javaField}" width="180">
                        <template #default="scope">
                            <span>{{ parseTime(scope.row.${javaField}, '{y}-{m}-{d}') }}</span>
                        </template>
                    </el-table-column>
                #elseif($column.list && $column.htmlType == "imageUpload")
                    <el-table-column label="${comment}" align="center" prop="${javaField}" width="100">
                        <template #default="scope">
                            <image-preview :src="scope.row.${javaField}" :width="50" :height="50"/>
                        </template>
                    </el-table-column>
                #elseif($column.list && "" != $column.dictType)
                    <el-table-column label="${comment}" align="center" prop="${javaField}">
                        <template #default="scope">
                            #if($column.htmlType == "checkbox")
                                <dict-tag :options="${column.dictType}" :value="scope.row.${javaField} ? scope.row.${javaField}.split(',') : []"/>
                            #else
                                <dict-tag :options="${column.dictType}" :value="scope.row.${javaField}"/>
                            #end
                        </template>
                    </el-table-column>
                #elseif($column.list && "" != $javaField)
                    <el-table-column label="${comment}" align="center" prop="${javaField}" />
                #end
            #end
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['${moduleName}:${businessName}:edit']">修改</el-button>
                    <el-button link type="danger" size="small" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['${moduleName}:${businessName}:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList"/>

        <!-- 表单 -->
        <${businessName}-form ref="${businessName}Ref" :get-list="getList"></${businessName}-form>
    </div>
</template>

<script setup name="${BusinessName}">
    import { list${BusinessName}, del${BusinessName} } from "@/api/${moduleName}/${businessName}";
    import ${businessName}Form from "./form"
    const { proxy } = getCurrentInstance()
    /** 字典数组区 */
        #if(${dicts} != '')
            #set($dictsNoSymbol=$dicts.replace("'", ""))
        const { ${dictsNoSymbol} } = proxy.useDict(${dicts});
        #end
    /** 查询 对象 */

    const ${businessName}List = ref([]);
        #if($table.sub)
        const ${subclassName}List = ref([]);
        #end
    const loading = ref(true);
    const ids = ref([])
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);
    /** 查询对象 */
   const queryParams=ref({
        pageNum: 1,
        pageSize: 10,
        #foreach ($column in $columns)
            #if($column.query)
                    $column.javaField: null#if($foreach.count != $columns.size()),#end
            #end
        #end
    })

  /***********************  方法区  ****************************/

    /** 查询${functionName}列表 */
   function getList() {
        loading.value = true;
        #foreach ($column in $columns)
            #if($column.htmlType == "datetime" && $column.queryType == "BETWEEN")
                queryParams.value.params = {};
                #break
            #end
        #end
        #foreach ($column in $columns)
            #if($column.htmlType == "datetime" && $column.queryType == "BETWEEN")
                #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
                if (null != daterange${AttrName} && '' != daterange${AttrName}) {
                    queryParams.value.params["begin${AttrName}"] = daterange${AttrName}.value[0];
                    queryParams.value.params["end${AttrName}"] = daterange${AttrName}.value[1];
                }
            #end
        #end
        list${BusinessName}(queryParams.value).then(response => {
                ${businessName}List.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }

    /** 搜索按钮操作 */
    function handleQuery() {
        queryParams.value.pageNum = 1;
        getList();
    }

    /** 重置按钮操作 */
    function resetQuery() {
        #foreach ($column in $columns)
            #if($column.htmlType == "datetime" && $column.queryType == "BETWEEN")
                #set($AttrName=$column.javaField.substring(0,1).toUpperCase() + ${column.javaField.substring(1)})
                daterange${AttrName}.value = [];
            #end
        #end
        proxy.resetForm("queryRef");
        handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.${pkColumn.javaField});
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    /** 新增按钮操作 */
    function handleAdd() {
         proxy.$refs.${businessName}Ref.open()
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
        const id = row.id || ids.value
        proxy.$refs.${businessName}Ref.open(id)
    }


    /** 删除按钮操作 */
    function handleDelete(row) {
        const _${pkColumn.javaField}s = row.${pkColumn.javaField} || ids.value;
        proxy.#[[$modal]]#.confirm('是否确认删除选中的数据项？').then(function() {
            return del${BusinessName}(_${pkColumn.javaField}s);
        }).then(() => {
            getList();
            proxy.#[[$modal]]#.msgSuccess("删除成功！");
        }).catch(() => {});
    }

    /** 导出按钮操作 */
    function handleExport() {
        proxy.download('${moduleName}/${businessName}/export', {
            ...queryParams.value
        }, `${businessName}_#[[${new Date().getTime()}]]#.xlsx`)
    }

   getList();
</script>
