<template>
  <!-- 按钮 -->
  <div style="margin: 50px 50px">
    <el-space :size="30">
      <el-button type="primary" icon="SwitchButton" @click="handleOpen"
        >启用同步</el-button
      >
      <el-button type="info" icon="CircleClose" @click="handleClose"
        >关闭同步</el-button
      >
      <el-button type="primary" icon="Refresh" @click="handleManual"
        >手动同步</el-button
      >
    </el-space>
  </div>

  <!-- 表单区域 -->
  <div style="height: 500px; width: 500px; margin: auto 25%">
    <el-form
      ref="ruleFormRef"
      :rules="rules"
      :model="sybchronizationForm"
      label-position="right"
      label-width="100px"
      class="demo-ruleForm"
      style="margin-top: 50px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="金蝶地址：" prop="k3CloudUrl">
            <el-input
              v-model="sybchronizationForm.k3CloudUrl"
              placeholder="请输入金蝶地址："
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24" prop="dbid">
          <el-form-item label="数据库ID：">
            <el-input
              v-model="sybchronizationForm.dbid"
              placeholder="请输入数据库ID："
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="语言：" prop="lang">
            <el-input
              v-model="sybchronizationForm.lang"
              placeholder="请输入语言："
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="账号：" prop="uid">
            <el-input
              v-model="sybchronizationForm.uid"
              placeholder="请输入账号："
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="密码：" prop="pwd">
            <el-input
              v-model="sybchronizationForm.pwd"
              type="password"
              placeholder="请输入密码："
              clearable
              show-password
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item style="margin-left: 20%">
            <el-button
              type="primary"
              icon="Download"
              @click="handleSave(ruleFormRef)"
              >保存配置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script lang="ts" setup>
import {
  getSynchronization,
  putSynchronization,
} from "@/services/api/synchronization/synchronizationAPI";
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";

const sybchronizationForm = ref({
  k3CloudUrl: "",
  dbid: "",
  lang: "",
  uid: "",
  pwd: "",
});
// 校验规则
const rules = ref<FormRules>({
  k3CloudUrl: [{ required: true, message: "金蝶地址不为空", trigger: "blur" }],
  dbid: [{ required: true, message: "数据库ID不为空", trigger: "blur" }],
  lang: [{ required: true, message: "语言不为空", trigger: "blur" }],
  uid: [{ required: true, message: "账号不为空", trigger: "blur" }],
  pwd: [{ required: true, message: "密码不为空", trigger: "blur" }]
  });
const ruleFormRef = ref<FormInstance>();
onMounted(async () => {
  const { data: res } = await getSynchronization();
  sybchronizationForm.value = res;
  console.log(res);
});
const isDisabled = ref(false);
const handleOpen = () => {
  if (isDisabled.value === true) return;
  isDisabled.value = true;
  console.log("启动同步");
};
const handleClose = () => {
  if (isDisabled.value === false) return;
  isDisabled.value = false;
  console.log("关闭同步");
};
const handleManual = () => {
  if (isDisabled.value === true) return;
  isDisabled.value = true;
  console.log("手动同步");
};
const handleSave = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  formEl.validate(async (valid, fields) => {
    if (valid) {
      const res: any = await putSynchronization(sybchronizationForm.value);
      if (res.code === 200)
        return ElMessage({ message: "保存参数成功！", type: "success" });
      ElMessage({ message: "保存参数失败！", type: "error" });
    } else {
      console.log("校验失败", fields);
      return false;
    }
  });
};
</script>

<style lang="scss" scoped>
.el-form-item {
  margin-bottom: 40px;
}
</style>
