<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="功能模块id" prop="moduleId">
        <el-input
          v-model="queryParams.moduleId"
          placeholder="请输入功能模块id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="插入点" prop="insertPoint">
        <el-input
          v-model="queryParams.insertPoint"
          placeholder="请输入插入点"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联表名称" prop="relationTableName">
        <el-input
          v-model="queryParams.relationTableName"
          placeholder="请输入关联表名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['generator:snippet:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['generator:snippet:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['generator:snippet:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['generator:snippet:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="snippetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代码片段id" align="center" prop="snippetId" />
      <el-table-column label="功能模块id" align="center" prop="moduleId" />
      <el-table-column label="代码片段内容" align="center" prop="snippetContent" :show-overflow-tooltip="true" />
      <el-table-column label="插入点" align="center" prop="insertPoint" />
      <el-table-column label="关联表名称" align="center" prop="relationTableName" />
      <!-- <el-table-column label="模板文件名称" align="center" prop="templateFileName" /> -->
      <el-table-column label="参数1" align="center" prop="param1" />
      <el-table-column label="参数2" align="center" prop="param2" />
      <el-table-column label="参数3" align="center" prop="param3" />
      <el-table-column label="参数4" align="center" prop="param4" />
      <el-table-column label="参数5" align="center" prop="param5" />
      <!-- <el-table-column label="状态" align="center" prop="status" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['generator:snippet:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['generator:snippet:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改代码片段管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="功能模块id" prop="moduleId">
          <el-select v-model="form.moduleId" placeholder="请选择功能模块" clearable style="width:100%">
            <el-option
              v-for="module in moduleList"
              :key="module.moduleId"
              :label="module.moduleName"
              :value="module.moduleId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="代码片段内容" prop="snippetContent">
          <el-input v-model="form.snippetContent" type="textarea" :rows="6" placeholder="请输入代码片段内容" />
        </el-form-item>
        <el-form-item label="插入点" prop="insertPoint">
          <el-input v-model="form.insertPoint" placeholder="请输入插入点" />
        </el-form-item>
        <el-form-item label="关联表名称" prop="relationTableName">
          <el-input v-model="form.relationTableName" placeholder="请输入关联表名称" />
        </el-form-item>
        <!-- <el-form-item label="模板文件名称" prop="templateFileName">
          <el-input v-model="form.templateFileName" placeholder="请输入模板文件名称" />
        </el-form-item> -->
        <el-form-item label="参数1" prop="param1">
          <el-input v-model="form.param1" placeholder="请输入参数1" />
        </el-form-item>
        <el-form-item label="参数2" prop="param2">
          <el-input v-model="form.param2" placeholder="请输入参数2" />
        </el-form-item>
        <el-form-item label="参数3" prop="param3">
          <el-input v-model="form.param3" placeholder="请输入参数3" />
        </el-form-item>
        <el-form-item label="参数4" prop="param4">
          <el-input v-model="form.param4" placeholder="请输入参数4" />
        </el-form-item>
        <el-form-item label="参数5" prop="param5">
          <el-input v-model="form.param5" placeholder="请输入参数5" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSnippet, getSnippet, delSnippet, addSnippet, updateSnippet } from "@/api/generator/snippet";
import { listModule } from "@/api/generator/module";

export default {
  name: "Snippet",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 代码片段管理表格数据
      snippetList: [],
      // 功能模块列表
      moduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moduleId: null,
        snippetContent: null,
        insertPoint: null,
        param1: null,
        param2: null,
        param3: null,
        param4: null,
        param5: null,
        status: null,
        templateFileName: null,
        relationTableName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moduleId: [
          { required: true, message: "功能模块id不能为空", trigger: "blur" }
        ],
        snippetContent: [
          { required: true, message: "代码片段内容不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询代码片段管理列表 */
    getList() {
      this.loading = true;
      listSnippet(this.queryParams).then(response => {
        this.snippetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 获取功能模块列表 */
    getModuleList() {
      listModule().then(response => {
        // Assuming listModule returns a TableDataInfo object with modules in 'rows'
        // If it directly returns an array or an object with modules in 'data', adjust accordingly.
        this.moduleList = response.rows || response.data || []; 
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        snippetId: null,
        moduleId: null,
        snippetContent: null,
        insertPoint: null,
        param1: null,
        param2: null,
        param3: null,
        param4: null,
        param5: null,
        templateFileName: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        relationTableName: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.snippetId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getModuleList(); // 获取功能模块列表
      this.open = true;
      this.title = "添加代码片段管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const snippetId = row.snippetId || this.ids
      getSnippet(snippetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改代码片段管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.snippetId != null) {
            updateSnippet(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSnippet(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const snippetIds = row.snippetId || this.ids;
      this.$modal.confirm('是否确认删除代码片段管理编号为"' + snippetIds + '"的数据项？').then(function() {
        return delSnippet(snippetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('generator/snippet/export', {
        ...this.queryParams
      }, `snippet_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
