<template>
  <a-modal
    :open="visible"
    title="修改多库"
    cancelText="取消"
    okText="提交"
    @ok="submit"
    @cancel="cancel"
  >
    <a-form
       ref="formRef"
      :model="formState"
      :rules="formRules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <a-form-item ref="name" label="名称" name="name">
        <a-input v-model:value="formState.name" />
      </a-form-item>
      <a-form-item ref="username" label="账户" name="username">
        <a-input v-model:value="formState.username" />
      </a-form-item>
      <a-form-item ref="password" label="密码" name="password">
        <a-input v-model:value="formState.password" />
      </a-form-item>
      <a-form-item label="连接" name="url">
        <a-textarea v-model:value="formState.url" />
      </a-form-item>
      <a-form-item ref="driver" label="驱动" name="driver">
        <a-input v-model:value="formState.driver" />
      </a-form-item>
      <a-form-item label="状态" name="enable">
        <a-switch v-model:checked="formState.enable" />
      </a-form-item>
      <a-form-item label="备注" name="remark">
        <a-textarea v-model:value="formState.remark" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import { message } from '@hwork/ant-design-vue';
import { edit } from "@/api/module/dataSource";
import { defineComponent, reactive, ref, toRaw, watch } from "vue";

const key = "save"

export default defineComponent({
  props: {
    visible: {
      type: Boolean,
    },
    record: {
      type: Object,
    }
  },
  emit: ["close"],
  setup(props, context) {

    const formRef = ref();
    
    const formState = reactive({});

    watch(props,(props) => {
        formState.id = props.record.id
        formState.name = props.record.name
        formState.username = props.record.username
        formState.password = props.record.password
        formState.url = props.record.url
        formState.driver = props.record.driver
        formState.remark = props.record.remark
        formState.enable = props.record.enable
    })


    const formRules = {
      name: [{ required: true, message: '请输入名称', trigger: 'blur'}],
      code: [{ required: true, message: '请输入标识', trigger: 'blur'}],
      username: [{ required: true, message: '请输入账户', trigger: 'blur'}],
      password: [{ required: true, message: '请输入密码', trigger: 'blur'}],
      url: [{ required: true, message: '请输入链接', trigger: 'blur'}],
      driver: [{ required: true, message: '请输入驱动', trigger: 'blur'}],
    };

    const submit = (e) => {
      formRef.value
      .validate()
      .then(() => {
          message.loading({
            content: "提交中...",
            key,
          });
          edit(toRaw(formState)).then((response) => {
            if (response.success) {
              message.success({
                content: "保存成功",
                key,
                duration: 1,
              }).then(()=>{
                cancel();
              });
            } else {
              message.error({
                content: "保存失败",
                key,
                duration: 1,
              }).then(()=>{
                cancel();
              });
            }
          });
        })
        .catch((error) => {
          console.log("error", error);
        });
    };

    const cancel = (e) => {
      formRef.value.resetFields();
      context.emit("close", false);
    };

    return {
      submit,
      cancel,
      formRef,
      formState,
      formRules,
      
      labelCol: { span: 6 },
      wrapperCol: { span: 18 },
    };
  },
});
</script>