<script setup>
import { ref, reactive, onMounted, watch } from "vue";

import { getAminUser, addAdminUser, updateAdminUser } from "@/api/adminuser";

const props = defineProps({
  editid: {
    type: Number,
    default: 0,
  },
});

const emit = defineEmits(["close"]);

const formType = ref("insert");

const loading = ref(false);
const formRef = ref(null);
const form = ref({
  id: 0,
  username: "",
  password: "",
  cfm_password: "",
});

onMounted(async () => {
  if (props.editid) {
    let res = await getAminUser(props.editid);
    if (res.code === 200) {
      form.value = res.data;
      formType.value = "update";
    }
  }
});

watch(()=>props.editid,(nVal)=>{
}, {immediate: true})

const validatePass = (rule, value, callback) => {
  if (form.cfm_password !== '') {
    if (!formRef.value) return
    formRef.value.validateField('cfm_password', () => null)
  }
  callback()
}

const validatePass2 = (rule, value, callback) => {
  if (value && value !== form.value.password) {
      callback(new Error("二個密碼不一致!"))
  }else{
    callback()
  }
}

const rules = reactive({
  username: [{ required: true, message: "管理員帳號必填", trigger: 'blur' }],
  password: [{ validator: validatePass, trigger: 'blur' }],
  cfm_password: [{ validator: validatePass2, trigger: 'blur' }],
})

const cancelForm = () => {
  emit("close", false);
};

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate(async (valid) => {
    if (valid) {
      let res;
      let message;
      if (formType.value === "update") {
        form.value.id = props.editid;
        res = await updateAdminUser(form.value);
        message = "修改成功.";
      } else {
        res = await addAdminUser(form.value);
        message = "新增成功.";
      }

      if (res.code === 200) {
        ElMessage({
          message: message,
          type: 'success',
        });
        emit("close", true);
      }
    } else {
      ElMessage({
        message: "操作失敗.",
        type: 'error',
      });
      return false
    }
  })
};
</script>

<template>
  <div>
    <el-form 
      ref="formRef" 
      :model="form" 
      status-icon 
      :rules="rules" 
      label-width="120px">
      <el-form-item label="管理員帳號" prop="username">
        <template v-if="formType==='insert'">
          <el-input v-model="form.username" />
        </template>
        <template v-else>
          {{ form.username }}
        </template>
      </el-form-item>
      <el-form-item label="管理員密碼" prop="password">
        <el-input v-model="form.password" type="password" show-password />
      </el-form-item>
      <el-form-item label="確認密碼" prop="cfm_password">
        <el-input v-model="form.cfm_password" type="password" show-password />
      </el-form-item>
    </el-form>
    <div class="demo-drawer__footer" style="text-align: right;">
      <el-button @click="cancelForm">取消</el-button>
      <el-button type="primary" :loading="loading" @click="submitForm(formRef)">{{
        loading ? '提交中 ...' : '確認'
      }}</el-button>
    </div>
  </div>
</template>
