<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"
      :disabled="isDisabled"
      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,
  getStart,
  getManual,
  getOff,
} from "@/services/api/systemApi/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 = async () => {
  // if (isDisabled.value === true) return;
  const res: any = await getStart();
  if (res.code === 200) {
    isDisabled.value = !isDisabled.value;
    return ElMessage({ type: "success", message: "自动启动成功" });
  }
};
const handleClose = async () => {
  // if (isDisabled.value === false) return;
  const res: any = await getOff();
  if (res.code === 200) {
    isDisabled.value = !isDisabled.value;
    return ElMessage({ type: "success", message: "关闭成功" });
  }
};
const handleManual = async () => {
  if (isDisabled.value === true) return;
  const res: any = await getManual();
  if (res.code === 200) {
    isDisabled.value = !isDisabled.value;
    return ElMessage({ type: "success", message: "手动启动成功" });
  }
};
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" });
    } else {
      console.log("校验失败", fields);
      return false;
    }
  });
};
</script>

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