<template>
  <div class="page-container">
    <el-card class="page-search" shadow="never">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="auto">
        #foreach($fieldConfig in $fieldConfigs)
          #if($fieldConfig.isShowInQuery == 1)
            <el-form-item label="$fieldConfig.fieldComment" prop="$fieldConfig.fieldName">
                    #if($fieldConfig.formType.name() == "INPUT")
                      <el-input
                          v-model="queryParams.$fieldConfig.fieldName"
                          placeholder="$fieldConfig.fieldComment"
                          clearable
                          @keyup.enter="handleQuery()"
                      />
                    #elseif($fieldConfig.formType.name() == "SELECT")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="queryParams.$fieldConfig.fieldName" code="$fieldConfig.dictType" />
                        #else
                          <el-select v-model="queryParams.$fieldConfig.fieldName" placeholder="请选择$fieldConfig.fieldComment">
                            <el-option :key="1" :value="1" label="选项一"/>
                            <el-option :key="2" :value="2" label="选项二"/>
                          </el-select>
                        #end
                    #elseif($fieldConfig.formType.name() == "RADIO")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="queryParams.$fieldConfig.fieldName" type="radio" code="$fieldConfig.dictType" />
                        #else
                          <el-radio-group v-model="queryParams.$fieldConfig.fieldName">
                            <el-radio :key="1" :value="1">选项一</el-radio>
                            <el-radio :key="2" :value="2">选项二</el-radio>
                          </el-radio-group>
                        #end
                    #elseif($fieldConfig.formType.name() == "CHECK_BOX")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="queryParams.$fieldConfig.fieldName" type="checkbox" code="$fieldConfig.dictType" />
                        #else
                          <el-checkbox-group v-model="queryParams.$fieldConfig.fieldName">
                            <el-checkbox :key="1" :label="1">选项一</el-checkbox>
                            <el-checkbox :key="2" :label="2">选项二</el-checkbox>
                          </el-checkbox-group>
                        #end
                    #elseif($fieldConfig.formType.name() == "INPUT_NUMBER")
                      <el-input-number
                          v-model="queryParams.$fieldConfig.fieldName"
                          placeholder="$fieldConfig.fieldComment"
                      />
                    #elseif($fieldConfig.formType.name() == "SWITCH")
                      <el-switch
                          v-model="queryParams.$fieldConfig.fieldName"
                          :active-value="1"
                          :inactive-value="0"
                      />
                    #elseif($fieldConfig.formType.name() == "TEXT_AREA")
                      <el-input type="textarea"
                                v-model="queryParams.$fieldConfig.fieldName"
                                placeholder="$fieldConfig.fieldComment"
                      />
                    #elseif($fieldConfig.formType.name() == "DATE_TIME")
                      <el-date-picker
                          v-model="queryParams.$fieldConfig.fieldName"
                          #if($fieldConfig.queryType.name() == "BETWEEN")
                          type="daterange"
                          range-separator="~"
                          start-placeholder="开始时间"
                          end-placeholder="结束时间"
                          #else
                          type="datetime"
                          placeholder="$fieldConfig.fieldComment"
                          #end
                          value-format="YYYY-MM-DD HH:mm:ss"
                      />
                    #elseif($fieldConfig.formType.name() == "DATE")
                      <el-date-picker
                          class="!w-[240px]"
                          v-model="queryParams.$fieldConfig.fieldName"
                          #if($fieldConfig.queryType.name() == "BETWEEN")
                          type="daterange"
                          range-separator="~"
                          start-placeholder="开始时间"
                          end-placeholder="结束时间"
                          #else
                          type="date"
                          placeholder="$fieldConfig.fieldComment"
                          #end
                          value-format="YYYY-MM-DD"
                      />
                    #end
            </el-form-item>
          #end
        #end
        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <el-card class="page-content" shadow="never">
      <div class="page-toolbar">
        <div class="page-toolbar__left">
          <el-button
              v-hasPerm="['${moduleName}:${entityKebab}:create']"
              type="success"
              icon="plus"
              @click="handleCreateClick()"
          >新增</el-button>
          <el-button
              v-hasPerm="['${moduleName}:${entityKebab}:delete']"
              type="danger"
              :disabled="!hasSelection"
              icon="delete"
              @click="handleDelete()"
          >删除</el-button>
        </div>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        border
        stripe
        highlight-current-row
        row-key="id"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        #foreach($fieldConfig in $fieldConfigs)
          #if($fieldConfig.isShowInList == 1)
            #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
              <el-table-column label="$fieldConfig.fieldComment" width="150" align="center">
                <template #default="scope">
                  <DictTag v-model="scope.row.$fieldConfig.fieldName" code="$fieldConfig.dictType" />
                </template>
              </el-table-column>
            #else
              <el-table-column
                key="$fieldConfig.fieldName"
                label="$fieldConfig.fieldComment"
                prop="$fieldConfig.fieldName"
                min-width="150"
                align="center"
              />
            #end
          #end
        #end
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
                v-hasPerm="['${moduleName}:${entityKebab}:update']"
                type="primary"
                size="small"
                link
                icon="edit"
                @click="handleEditClick(String(scope.row.id))"
            >
              编辑
            </el-button>
            <el-button
                v-hasPerm="['${moduleName}:${entityKebab}:delete']"
                type="danger"
                size="small"
                link
                icon="delete"
                @click="handleDelete(String(scope.row.id))"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="page-pagination">
        <pagination
            v-if="total > 0"
            v-model:total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="fetchList"
        />
      </div>
    </el-card>

    <!-- $!{businessName}表单弹窗 -->
    <el-dialog
        v-model="dialog.visible"
        :title="dialog.title"
        width="500px"
        @close="closeDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
          #foreach($fieldConfig in $fieldConfigs)
              #if($fieldConfig.isShowInForm == 1 && $fieldConfig.formType.name() != "HIDDEN")
                <el-form-item label="$fieldConfig.fieldComment" prop="$fieldConfig.fieldName">
                    #if($fieldConfig.formType.name() == "INPUT")
                      <el-input
                          v-model="formData.$fieldConfig.fieldName"
                          placeholder="$fieldConfig.fieldComment"
                      />
                    #elseif($fieldConfig.formType.name() == "SELECT")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="formData.$fieldConfig.fieldName" code="$fieldConfig.dictType" />
                        #else
                          <el-select v-model="formData.$fieldConfig.fieldName" placeholder="请选择$fieldConfig.fieldComment">
                            <el-option :value="0" label="选项一"/>
                            <el-option :value="1" label="选项二"/>
                          </el-select>
                        #end
                    #elseif($fieldConfig.formType.name() == "RADIO")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="formData.$fieldConfig.fieldName" type="radio" code="$fieldConfig.dictType" />
                        #else
                          <el-radio-group v-model="formData.$fieldConfig.fieldName">
                            <el-radio :value="0">选项一</el-radio>
                            <el-radio :value="1">选项二</el-radio>
                          </el-radio-group>
                        #end
                    #elseif($fieldConfig.formType.name() == "CHECK_BOX")
                        #if($fieldConfig.dictType && $fieldConfig.dictType.trim() != "")
                          <DictSelect v-model="formData.$fieldConfig.fieldName" type="checkbox" code="$fieldConfig.dictType" />
                        #else
                          <el-checkbox-group v-model="formData.$fieldConfig.fieldName">
                            <el-checkbox :value="0">选项一</el-checkbox>
                            <el-checkbox :value="1">选项二</el-checkbox>
                          </el-checkbox-group>
                        #end
                    #elseif($fieldConfig.formType.name() == "INPUT_NUMBER")
                      <el-input-number
                          v-model="formData.$fieldConfig.fieldName"
                          placeholder="$fieldConfig.fieldComment"
                      />
                    #elseif($fieldConfig.formType.name() == "SWITCH")
                      <el-switch
                          v-model="formData.$fieldConfig.fieldName"
                          :active-value="1"
                          :inactive-value="0"
                      />
                    #elseif($fieldConfig.formType.name() == "TEXT_AREA")
                      <el-input type="textarea"
                                v-model="formData.$fieldConfig.fieldName"
                                placeholder="$fieldConfig.fieldComment"
                      />
                    #elseif($fieldConfig.formType.name() == "DATE_TIME")
                      <el-date-picker
                          v-model="formData.$fieldConfig.fieldName"
                          type="datetime"
                          placeholder="$fieldConfig.fieldComment"
                          value-format="YYYY-MM-DD HH:mm:ss"
                      />
                    #elseif($fieldConfig.formType.name() == "DATE")
                      <el-date-picker
                          class="!w-[240px]"
                          v-model="formData.$fieldConfig.fieldName"
                          type="date"
                          placeholder="$fieldConfig.fieldComment"
                          value-format="YYYY-MM-DD"
                      />
                    #end
                </el-form-item>

              #elseif($fieldConfig.formType.name() == "HIDDEN")
                <el-input type="hidden" v-model="formData.$fieldConfig.fieldName" />
              #end
          #end
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="closeDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
  import { onMounted, reactive, ref } from "vue";
  import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from "element-plus";
  import { useTableSelection } from "@/composables";
  import ${entityName}API from "@/api/${moduleName}/${entityKebab}";
  import type { ${entityName}Item, ${entityName}Form, ${entityName}QueryParams } from "@/api/${moduleName}/${entityKebab}";

  defineOptions({
    name: "${entityName}",
    inheritAttrs: false,
  });

  const queryFormRef = ref<FormInstance>();
  const dataFormRef = ref<FormInstance>();

  const loading = ref(false);
  const total = ref(0);

  const queryParams = reactive<${entityName}QueryParams>({
    pageNum: 1,
    pageSize: 10,
  } as ${entityName}QueryParams);

  // $!{businessName}表格数据
  const pageData = ref<${entityName}Item[]>([]);

  // 弹窗
  const dialog = reactive({
    title: "",
    visible: false,
  });

  // $!{businessName}表单数据
  const formData = reactive<${entityName}Form>({} as ${entityName}Form);

  // $!{businessName}表单校验规则
  const rules: FormRules = {
      #if($fieldConfigs)
          #foreach($fieldConfig in ${fieldConfigs})
              #if($fieldConfig.isShowInForm && $fieldConfig.isRequired)
                      ${fieldConfig.fieldName}: [{ required: true, message: "请输入${fieldConfig.fieldComment}", trigger: "blur" }],
              #end
          #end
      #end
  };

  const { selectedIds, hasSelection, handleSelectionChange } = useTableSelection<${entityName}Item>();

  async function fetchList(): Promise<void> {
    loading.value = true;
    try {
      const data = await ${entityName}API.getPage(queryParams);
      pageData.value = data.list;
      total.value = data.total ?? 0;
    } finally {
      loading.value = false;
    }
  }

  /** 查询$!{businessName} */
  function handleQuery(): void {
    queryParams.pageNum = 1;
    fetchList();
  }

  /** 重置$!{businessName}查询 */
  function handleResetQuery() {
    queryFormRef.value?.resetFields();
    handleQuery();
  }

  function openDialog(): void {
    dialog.visible = true;
  }

  function closeDialog(): void {
    dialog.visible = false;
    resetForm();
  }

  function resetForm(): void {
    dataFormRef.value?.resetFields();
    dataFormRef.value?.clearValidate();
    formData.id = undefined;
  }

  async function handleCreateClick(): Promise<void> {
    dialog.title = "新增$!{businessName}";
    openDialog();
  }

  async function handleEditClick(id: string): Promise<void> {
    dialog.title = "修改$!{businessName}";
          ${entityName}API.getFormData(id).then((data) => {
      Object.assign(formData, data);
      openDialog();
    });
  }

  /** 提交$!{businessName}表单 */
  function handleSubmit() {
    dataFormRef.value?.validate((valid: any) => {
      if (valid) {
        loading.value = true;
        const id = formData.id;
        if (id) {
                ${entityName}API.update(id, formData)
              .then(() => {
                ElMessage.success("修改成功");
                closeDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        } else {
                ${entityName}API.create(formData)
              .then(() => {
                ElMessage.success("新增成功");
                closeDialog();
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        }
      }
    });
  }

  /** 删除$!{businessName} */
  function handleDelete(id?: string) {
    const ids = [id || selectedIds.value].join(",");
    if (!ids) {
      ElMessage.warning("请勾选删除项");
      return;
    }

    ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }).then(
        () => {
          loading.value = true;
                ${entityName}API.deleteByIds(ids)
              .then(() => {
                ElMessage.success("删除成功");
                handleResetQuery();
              })
              .finally(() => (loading.value = false));
        },
        () => {
          ElMessage.info("已取消删除");
        }
    );
  }

  onMounted(() => {
    handleQuery();
  });
</script>
