<template>
  <div class="system-{{.BusinessName}}-container">
    <el-dialog v-model="state.isShowDialog" width="769px" center>
      <template #title>
        <div style="font-size: large" v-drag="['.system-{{.BusinessName}}-container .el-dialog', '.system-{{.BusinessName}}-container .el-dialog__header']">{{"{{"}}title{{"}}"}}</div>
      </template>
      <el-form
        :model="state.ruleForm"
        :rules="state.ruleRules"
        ref="ruleFormRef"
        label-width="80px"
      >
      {{- range $index, $column := .Columns -}}
      {{- if and (eq $column.IsInsert "1") (ne $column.IsPk "1")  (ne $column.JsonField "createTime") (ne $column.JsonField "updateTime") (ne $column.JsonField "deleteTime") -}}
      {{- if eq $column.HtmlType "input" }}
      <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
          <el-input v-model="state.ruleForm.{{$column.JsonField}}" placeholder="请输入{{$column.ColumnComment}}" />
      </el-form-item>
      {{- else if eq $column.HtmlType "switch" }}
      <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
          <el-switch v-model="state.ruleForm.{{$column.HtmlField}}" />
      </el-form-item>
      {{- else if eq $column.HtmlType "select" }}
      {{if ne $column.DictType ""}}
       <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
          <el-select v-model="state.ruleForm.{{$column.JsonField}}" placeholder="请选择{{$column.ColumnComment}}">
              <el-option
                  v-for="dict in state.{{$column.JsonField}}Options"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
              >{{"{{"}} dict.dictLabel {{"}}"}}</el-option>
          </el-select>
       </el-form-item>
      {{ end }}
      {{- else if eq $column.HtmlType "radio" }}
      {{if ne $column.DictType ""}}
      <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
          <el-radio-group v-model="state.ruleForm.{{$column.JsonField}}">
              <el-radio
               v-for="dict in state.{{$column.JsonField}}Options"
               :key="dict.dictValue"
               :label="dict.dictLabel"
              >{{"{{"}} dict.dictLabel {{"}}"}}</el-radio>
          </el-radio-group>
      </el-form-item>
      {{- end }}
      {{- 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.ruleForm.{{$column.JsonField}}"
                 type="datetime"
                 placeholder="选择{{$column.ColumnComment}}">
          </el-date-picker>
      </el-form-item>
      {{- else if eq $column.HtmlType "textarea" -}}
      <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.JsonField}}">
          <el-input v-model="state.ruleForm.{{$column.HtmlField}}" type="textarea" placeholder="请输入{{$column.ColumnComment}}" />
      </el-form-item>
      {{- else if eq $column.HtmlType "checkbox" }}
       <el-form-item label="{{$column.ColumnComment}}" prop="{{$column.ruleForm}}">
          <el-checkbox-group v-model="state.ruleForm.{{$column.ruleForm}}">
             <el-checkbox
               v-for="dict in state.{{$column.ruleForm}}Options"
               :key="dict.dictValue"
               :label="dict.dictLabel"
             >{{"{{"}} dict.dictLabel {{"}}"}}</el-checkbox>
          </el-checkbox-group>
       </el-form-item>
      {{- end -}}
      {{- end -}}
      {{- end }}
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="onCancel">取 消</el-button>
          <el-button type="primary" @click="onSubmit" :loading="state.loading">编 辑</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="edit{{.FunctionName}}">
import { reactive, ref, unref, getCurrentInstance } from "vue";
import { update{{.FunctionName}}, add{{.FunctionName}} } from "@/api/{{.PackageName}}/{{.BusinessName}}";
import { ElMessage } from "element-plus";

const props = defineProps({
  title: {
    type: String,
    default: () => "",
  },
})

const { proxy } = getCurrentInstance() as any;
const ruleFormRef = ref<HTMLElement | null>(null);
const state = reactive({
  // 是否显示弹出层
  isShowDialog: false,
  loading: false,
  ruleForm: {
  {{- range $index, $column := .Columns -}}
  {{- if or (eq $column.IsInsert "1") (eq $column.IsPk "1") }}
  {{- if or (eq $column.GoType "int") (eq $column.GoType "int64")}}
  {{$column.JsonField}}: 0,
  {{- else if eq $column.GoType "string"}}
  {{$column.JsonField}}: "",
  {{- else }}
  {{$column.JsonField}}: undefined,
  {{- end}}
  {{- end}}
  {{- end}}
  },
  {{- range $index, $column := .Columns -}}
  {{- if ne $column.DictType "" }}
  // {{$column.JsonField}}Options字典数据
  {{$column.JsonField}}Options: [],
  {{- end -}}
  {{- end }}
  // 表单校验
  ruleRules: {
   {{- range $index, $column := .Columns -}}
   {{- if eq $column.IsRequired "1" }}
    {{$column.JsonField}}: [
      { required: true, message: "{{$column.ColumnComment}}不能为空", trigger: "blur" }
    ],
   {{- end}}
   {{- end}}
  },
});
// 打开弹窗
const openDialog = (row: any) => {
  state.ruleForm = JSON.parse(JSON.stringify(row));

  state.isShowDialog = true;
  state.loading = false;
 {{- range $index, $column := .Columns -}}
 {{- if ne $column.DictType "" }}
 proxy.getDicts("{{$column.DictType}}").then((response: any) => {
     state.{{$column.JsonField}}Options = response.data;
 });
 {{- end -}}
 {{- end}}
}

// 关闭弹窗
const closeDialog = (row?: object) => {
  proxy.mittBus.emit("onEdit{{.FunctionName}}Module", row);
  state.isShowDialog = false;
};
// 取消
const onCancel = () => {
  closeDialog();
};

// 保存
const onSubmit = () => {
  const formWrap = unref(ruleFormRef) as any;
  if (!formWrap) return;
  formWrap.validate((valid: boolean) => {
    if (valid) {
      state.loading = true;
      if (state.ruleForm.{{.PkJsonField}} != undefined && state.ruleForm.{{.PkJsonField}} != 0) {
        update{{.FunctionName}}(state.ruleForm).then((response) => {
          if (response.code == 200){
            ElMessage.success("修改成功");
            closeDialog(state.ruleForm); // 关闭弹窗
          }
          state.loading = false;
        });
      } else {
        add{{.FunctionName}}(state.ruleForm).then((response) => {
          if (response.code == 200){
            ElMessage.success("新增成功");
            closeDialog(state.ruleForm); // 关闭弹窗
          }
        });
      }
    }
  });
};

defineExpose({
  openDialog,
});
</script>