<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="模块名称" prop="moduleName">
        <el-input
          v-model="queryParams.moduleName"
          placeholder="请输入模块名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="模块别名" prop="moduleAlias">
        <el-input
          v-model="queryParams.moduleAlias"
          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:module: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:module: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:module: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:module:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="moduleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="模块id" align="center" prop="moduleId" />
      <el-table-column label="模块名称" align="center" prop="moduleName" />
      <el-table-column label="模块别名" align="center" prop="moduleAlias" />
      <el-table-column label="模块描述" align="center" prop="moduleDesc" />
      <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-s-order"
            @click="handleSnippet(scope.row)"
            v-hasPermi="['generator:snippet:list']"
          >代码片段</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['generator:module:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['generator:module: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="模块名称" prop="moduleName">
          <el-input v-model="form.moduleName" placeholder="请输入模块名称" />
        </el-form-item>
        <el-form-item label="模块别名" prop="moduleAlias">
          <el-input v-model="form.moduleAlias" placeholder="请输入模块别名" />
        </el-form-item>
        <el-form-item label="模块描述" prop="moduleDesc">
          <el-input v-model="form.moduleDesc" 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>

    <!-- 代码片段管理对话框 -->
    <el-dialog :title="'模块 「' + currentModule.moduleName + '」 的代码片段'" :visible.sync="snippetOpen" width="1200px" append-to-body>
      <el-form :model="snippetQueryParams" ref="snippetQueryForm" size="small" :inline="true" v-show="snippetShowSearch" label-width="68px">
        <el-form-item label="插入点" prop="insertPoint">
          <el-input
            v-model="snippetQueryParams.insertPoint"
            placeholder="请输入插入点"
            clearable
            @keyup.enter.native="handleSnippetQuery"
          />
        </el-form-item>
        <el-form-item label="关联表" prop="relationTableName">
          <el-input
            v-model="snippetQueryParams.relationTableName"
            placeholder="请输入关联表名称"
            clearable
            @keyup.enter.native="handleSnippetQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSnippetQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetSnippetQuery">重置</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="handleSnippetAdd"
            v-hasPermi="['generator:snippet:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="snippetShowSearch" @queryTable="getSnippetList"></right-toolbar>
      </el-row>

      <el-table v-loading="snippetLoading" :data="snippetList" @selection-change="handleSnippetSelectionChange">
        <el-table-column label="ID" align="center" prop="snippetId" />
        <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="参数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="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="handleSnippetUpdate(scope.row)"
              v-hasPermi="['generator:snippet:edit']"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleSnippetDelete(scope.row)"
              v-hasPermi="['generator:snippet:remove']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="snippetTotal>0"
        :total="snippetTotal"
        :page.sync="snippetQueryParams.pageNum"
        :limit.sync="snippetQueryParams.pageSize"
        @pagination="getSnippetList"
      />
    </el-dialog>

    <!-- 添加或修改代码片段对话框 -->
    <el-dialog :title="snippetTitle" :visible.sync="snippetFormOpen" width="800px" append-to-body>
      <el-form ref="snippetForm" :model="snippetForm" :rules="snippetRules" label-width="80px">
        <el-form-item label="代码片段内容" prop="snippetContent">
          <el-input v-model="snippetForm.snippetContent" type="textarea" :rows="6" placeholder="请输入片段内容" />
        </el-form-item>
        <el-form-item label="插入点" prop="insertPoint">
          <el-input v-model="snippetForm.insertPoint" placeholder="请输入插入点" />
        </el-form-item>
        <el-form-item label="关联表名称" prop="relationTableName">
          <el-input v-model="snippetForm.relationTableName" placeholder="请输入关联表名称" />
        </el-form-item>
        <el-form-item label="参数1" prop="param1">
          <el-input v-model="snippetForm.param1" placeholder="请输入参数1" />
        </el-form-item>
        <el-form-item label="参数2" prop="param2">
          <el-input v-model="snippetForm.param2" placeholder="请输入参数2" />
        </el-form-item>
        <el-form-item label="参数3" prop="param3">
          <el-input v-model="snippetForm.param3" placeholder="请输入参数3" />
        </el-form-item>
        <el-form-item label="参数4" prop="param4">
          <el-input v-model="snippetForm.param4" placeholder="请输入参数4" />
        </el-form-item>
        <el-form-item label="参数5" prop="param5">
          <el-input v-model="snippetForm.param5" placeholder="请输入参数5" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="snippetForm.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSnippetForm">确 定</el-button>
        <el-button @click="cancelSnippet">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

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

export default {
  name: "Module",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 功能模块表格数据
      moduleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        moduleName: null,
        moduleAlias: null,
        moduleDesc: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        moduleName: [
          { required: true, message: '模块名称不能为空', trigger: 'blur' }
        ]
      },
      // 当前选中模块
      currentModule: {},
      // 代码片段相关
      // 遮罩层
      snippetLoading: true,
      // 选中数组
      snippetIds: [],
      // 非单个禁用
      snippetSingle: true,
      // 非多个禁用
      snippetMultiple: true,
      // 显示搜索条件
      snippetShowSearch: true,
      // 总条数
      snippetTotal: 0,
      // 代码片段表格数据
      snippetList: [],
      // 是否显示代码片段弹出层
      snippetOpen: false,
      // 代码片段表单弹出层
      snippetFormOpen: false,
      // 代码片段弹出层标题
      snippetTitle: '',
      // 代码片段查询参数
      snippetQueryParams: {
        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
      },
      // 代码片段表单参数
      snippetForm: {
        snippetId: null,
        moduleId: null,
        snippetContent: null,
        insertPoint: null,
        relationTableName: null,
        param1: null,
        param2: null,
        param3: null,
        param4: null,
        param5: null,
        templateFileName: null,
        status: null,
        remark: null
      },
      // 代码片段表单校验
      snippetRules: {
        moduleId: [
          { required: true, message: '功能模块id不能为空', trigger: 'blur' }
        ],
        snippetContent: [
          { required: true, message: '片段内容不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询功能模块列表 */
    getList() {
      this.loading = true;
      listModule(this.queryParams).then(response => {
        this.moduleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        moduleId: null,
        moduleName: null,
        moduleAlias: null,
        moduleDesc: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.moduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加功能模块";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const moduleId = row.moduleId || this.ids
      getModule(moduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改功能模块";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.moduleId != null) {
            updateModule(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addModule(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const moduleIds = row.moduleId || this.ids
      this.$modal.confirm('是否确认删除功能模块编号为"' + moduleIds + '"的数据项？').then(function() {
        return delModule(moduleIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('generator/module/export', {
        ...this.queryParams
      }, `module_${new Date().getTime()}.xlsx`)
    },

    /** 代码片段管理操作 */
    handleSnippet(row) {
      this.currentModule = row
      this.snippetQueryParams.moduleId = row.moduleId
      this.snippetOpen = true
      this.getSnippetList()
    },
    /** 查询代码片段列表 */
    getSnippetList() {
      this.snippetLoading = true
      listSnippet(this.snippetQueryParams).then(response => {
        this.snippetList = response.rows
        this.snippetTotal = response.total
        this.snippetLoading = false
      })
    },
    /** 搜索代码片段按钮操作 */
    handleSnippetQuery() {
      this.snippetQueryParams.pageNum = 1
      this.getSnippetList()
    },
    /** 重置代码片段搜索操作 */
    resetSnippetQuery() {
      this.resetForm('snippetQueryForm')
      this.handleSnippetQuery()
    },
    // 代码片段多选框选中数据
    handleSnippetSelectionChange(selection) {
      this.snippetIds = selection.map(item => item.snippetId)
      this.snippetSingle = selection.length !== 1
      this.snippetMultiple = !selection.length
    },
    /** 新增代码片段操作 */
    handleSnippetAdd() {
      this.resetSnippetForm()
      this.snippetForm.moduleId = this.currentModule.moduleId
      this.snippetFormOpen = true
      this.snippetTitle = '添加代码片段'
    },
    /** 修改代码片段操作 */
    handleSnippetUpdate(row) {
      this.resetSnippetForm()
      const snippetId = row.snippetId || this.snippetIds
      getSnippet(snippetId).then(response => {
        this.snippetForm = response.data
        this.snippetFormOpen = true
        this.snippetTitle = '修改代码片段'
      })
    },
    // 取消代码片段按钮
    cancelSnippet() {
      this.snippetFormOpen = false
      this.resetSnippetForm()
    },
    // 代码片段表单重置
    resetSnippetForm() {
      this.snippetForm = {
        snippetId: null,
        moduleId: null,
        snippetContent: null,
        insertPoint: null,
        relationTableName: null,
        param1: null,
        param2: null,
        param3: null,
        param4: null,
        param5: null,
        templateFileName: null,
        status: null,
        remark: null
      }
      this.resetForm('snippetForm')
    },
    /** 提交代码片段表单 */
    submitSnippetForm() {
      this.$refs['snippetForm'].validate(valid => {
        if (valid) {
          if (this.snippetForm.snippetId != null) {
            updateSnippet(this.snippetForm).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.snippetFormOpen = false
              this.getSnippetList()
            })
          } else {
            addSnippet(this.snippetForm).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.snippetFormOpen = false
              this.getSnippetList()
            })
          }
        }
      })
    },
    /** 删除代码片段操作 */
    handleSnippetDelete(row) {
      const snippetIds = row.snippetId || this.snippetIds
      this.$modal.confirm('是否确认删除代码片段编号为"' + snippetIds + '"的数据项？').then(function() {
        return delSnippet(snippetIds)
      }).then(() => {
        this.getSnippetList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    /** 导出代码片段操作 */
    handleSnippetExport() {
      this.download('generator/snippet/export', {
        ...this.snippetQueryParams
      }, `snippet_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
