<template>
  <div class="app-container">
    <el-card shadow="always">
    <!-- 查询 -->
    <el-form
      :model="state.queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
    {{- range $index, $column := .Columns -}}
    {{- if and (eq $column.IsQuery "1") (ne $column.IsPk "1") (ne $column.JsonField "updateTime") (ne $column.JsonField "deleteTime") -}}
    {{- if eq $column.HtmlType "input" "textarea" }}
    <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
        <el-input
           v-model="state.queryParams.{{$column.JsonField}}"
           placeholder="请输入{{$column.ColumnComment}}"
           clearable
           @keyup.enter.native="handleQuery"
        />
     </el-form-item>
     {{- else if and (eq $column.HtmlType "switch" "select" "radio" "checkbox") (ne $column.DictType "") }}
     <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
       <el-select v-model="state.queryParams.{{$column.JsonField}}" placeholder="请选择{{$column.ColumnComment}}" clearable>
           <el-option
               v-for="dict in state.{{$column.JsonField}}Options"
               :key="dict.dictValue"
               :label="dict.dictLabel"
               :value="dict.dictValue"
           />
       </el-select>
     </el-form-item>
     {{- else if eq $column.HtmlType "datetime" }}
     <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
       <el-date-picker clearable style="width: 200px"
             v-model="state.queryParams.{{$column.JsonField}}"
             type="datetime"
             placeholder="选择{{$column.ColumnComment}}">
       </el-date-picker>
     </el-form-item>
     {{- end -}}
     {{- end -}}
     {{- end }}
     <el-form-item>
       <el-button type="primary" @click="handleQuery">
         <SvgIcon name="elementSearch" />搜索</el-button>
       <el-button @click="resetQuery">
         <SvgIcon name="elementRefresh" />
         重置
       </el-button>
     </el-form-item>
    </el-form>
    </el-card>
    <el-card class="box-card">
        <template #header>
           <div class="card-header">
             <span class="card-header-text">{{.TableComment}}列表</span>
             <div>
               <el-button
                  type="primary"
                  plain
                  v-auth="'{{.PackageName}}:{{.BusinessName}}:add'"
                  @click="onOpenAddModule"
                  ><SvgIcon name="elementPlus" />新增</el-button>
               <el-button
                 type="danger"
                 plain
                 v-auth="'{{.PackageName}}:{{.BusinessName}}:delete'"
                 :disabled="state.multiple"
                 @click="onTabelRowDel"
                 ><SvgIcon name="elementDelete" />删除</el-button>
             </div>
           </div>
        </template>
    <!--数据表格-->
    <el-table
      v-loading="state.loading"
      :data="state.tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      {{- range $index, $column := .Columns -}}
      {{- if eq $column.IsPk "1"}}
      <el-table-column label="编号" align="center" prop="{{$column.JsonField}}" />
      {{- else if and (eq $column.IsList "1") (eq $column.HtmlType "datetime")}}
      {{- if ne $column.ColumnComment "" }}
      <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.JsonField}}" width="180">
        <template #default="scope">
             <span>{{"{{"}} dateStrFormat(scope.row.{{$column.JsonField}}) {{"}}"}}</span>
        </template>
      </el-table-column>
      {{- end -}}
      {{- else if and (eq $column.IsList "1") (ne $column.DictType "") }}
      <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.JsonField}}" >
      <template #default="scope">
          <el-tag :type="scope.row.{{$column.JsonField}} === '0' ? 'success': 'warning'" disable-transitions>{{"{{"}} {{$column.JsonField}}Format(scope.row) || '-- --' {{"}}"}}</el-tag>
      </template>
      </el-table-column>
      {{- else if and (eq $column.IsList "1") (ne $column.JsonField "") (ne $column.JsonField "createTime") (ne $column.JsonField "updateTime") (ne $column.JsonField "deleteTime") }}
      {{- if ne $column.ColumnComment "" }}
      <el-table-column label="{{$column.ColumnComment}}" align="center" prop="{{$column.JsonField}}" />
      {{- end -}}
      {{- end -}}
      {{- end }}
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-popover  placement="left">
            <template #reference>
              <el-button type="primary" circle ><SvgIcon name="elementStar"/></el-button>
            </template>
            <div>
              <el-button text type="primary" v-auth="'{{.PackageName}}:{{.BusinessName}}:edit'" @click="onOpenEditModule(scope.row)">
                <SvgIcon name="elementEdit" />修改
              </el-button>
            </div>
            <div>
              <el-button text type="primary" v-auth="'{{.PackageName}}:{{.BusinessName}}:delete'" @click="onTabelRowDel(scope.row)">
                <SvgIcon name="elementDelete" />删除
              </el-button>
            </div>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页设置-->
    <div v-show="state.total > 0">
        <el-divider></el-divider>
        <el-pagination
            background
            :total="state.total"
            :current-page="state.queryParams.pageNum"
            :page-size="state.queryParams.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
    </div>
    </el-card>
    <!-- 添加或修改岗位对话框 -->
    <EditModule ref="editModuleRef" :title="state.title" />
  </div>
</template>

<script lang="ts" setup name="{{.FunctionName}}">
import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance,
  onUnmounted,
} from "vue";
import { ElMessageBox, ElMessage } from "element-plus";
import { list{{.FunctionName}}, del{{.FunctionName}} } from "@/api/{{.PackageName}}/{{.BusinessName}}";
import EditModule from "./component/editModule.vue";

const { proxy } = getCurrentInstance() as any;
const editModuleRef = ref();
const state = reactive({
  // 遮罩层
  loading: true,
  // 选中数组
  ids: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 弹出层标题
  title: "",
  // 表格数据
  tableData: [],
  // 总条数
  total: 0,
  {{- range $index, $column := .Columns -}}
  {{- if ne $column.DictType "" }}
  // {{$column.JsonField}}Options字典数据
  {{$column.JsonField}}Options: [],
  {{- end -}}
  {{- end }}
  // 查询参数
  queryParams: {
    // 页码
    pageNum: 1,
    // 每页大小
    pageSize: 10,
    // 以下为参数
    {{- range $index, $column := .Columns -}}
    {{- if eq $column.IsQuery "1" }}
    {{$column.JsonField}}: undefined,
    {{- end }}
    {{- end }}
  },
});

/** 查询列表 */
const handleQuery = () => {
  state.loading = true;
  list{{.FunctionName}}(state.queryParams).then((response:any) => {
    state.tableData = response.data.data;
    state.total = response.data.total;
    state.loading = false;
  });
};
/** 重置按钮操作 */
const resetQuery = () => {
   {{- range $index, $column := .Columns -}}
   {{- if eq $column.IsQuery "1" }}
   state.queryParams.{{$column.JsonField}} = undefined;
   {{- end }}
   {{- end }}
  handleQuery();
};

const handleCurrentChange = (val:number) => {
  state.queryParams.pageNum = val
  handleQuery()
}
const handleSizeChange = (val:number) => {
  state.queryParams.pageSize = val
  handleQuery()
}

{{- range $index, $column := .Columns -}}
{{- if ne $column.DictType "" }}
const {{$column.JsonField}}Format = (row: any) => {
   return proxy.selectDictLabel(state.{{$column.JsonField}}Options, row.{{$column.JsonField}});
};
{{- end -}}
{{- end }}

// 打开新增弹窗
const onOpenAddModule = () => {
  state.title = "添加{{.TableComment}}";
  editModuleRef.value.openDialog({});
};
// 打开编辑弹窗
const onOpenEditModule = (row: object) => {
  state.title = "修改{{.TableComment}}";
  editModuleRef.value.openDialog(row);
};
/** 删除按钮操作 */
const onTabelRowDel = (row: any) => {
  const {{.PkJsonField}}s = row.{{.PkJsonField}} || state.ids;
  ElMessageBox({
    message: '是否确认删除编号为"' + {{.PkJsonField}}s + '"的数据项?',
    title: "警告",
    showCancelButton: true,
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(function () {
    return del{{.FunctionName}}({{.PkJsonField}}s).then(() => {
      handleQuery();
      ElMessage.success("删除成功");
    });
  });
};
// 多选框选中数据
const handleSelectionChange = (selection: any) => {
  state.ids = selection.map((item: any) => item.{{.PkJsonField}});
  state.single = selection.length != 1;
  state.multiple = !selection.length;
};
// 页面加载时
onMounted(() => {
  // 查询岗位信息
  handleQuery();
  {{- range $index, $column := .Columns -}}
  {{- if ne $column.DictType "" }}
  proxy.getDicts("{{$column.DictType}}").then((response: any) => {
      state.{{$column.JsonField}}Options = response.data;
  });
  {{- end -}}
  {{- end}}

  proxy.mittBus.on("onEdit{{.FunctionName}}Module", (res: any) => {
    handleQuery();
  });
});
// 页面卸载时
onUnmounted(() => {
  proxy.mittBus.off("onEdit{{.FunctionName}}Module");
});
</script>
