<script setup lang="ts">
import type { DeviceApi } from '#/api';

import { ref } from 'vue';

import { useVbenDrawer } from '@vben/common-ui';

import { Button, message, Switch } from 'ant-design-vue';

import { useVbenForm } from '#/adapter/form';
import { deviceControlOnline, getDevice, updateDevice } from '#/api';
import { $t } from '#/locales';

import { useEditSchema } from '../data';

const emit = defineEmits(['success']);
const formData = ref<DeviceApi.Device>();
const [Form, formApi] = useVbenForm({
  layout: 'vertical',
  schema: useEditSchema(),
  showDefaultActions: false,
});

function resetForm() {
  formApi.resetForm();
  formApi.setValues(formData.value || {});
}
const [Drawer, modalApi] = useVbenDrawer({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (valid) {
      modalApi.lock();
      const data = await formApi.getValues<DeviceApi.Device>();
      console.warn('user data', data);
      if (formData.value?.id) {
        await updateDevice(formData.value?.id, data);
      }
      try {
        const res = await getDevice(formData.value?.id);
        formApi.setValues(res);
        // console.warn('deviceDetail', res);
        message.success('更新成功');
        emit('success');
        modalApi.close();
      } catch (error) {
        console.error(error);
        modalApi.unlock();
      } finally {
        modalApi.unlock();
      }
    }
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = modalApi.getData<DeviceApi.Device>();
      if (data) {
        if (data.pid === 0) {
          data.pid = undefined;
        }
        formData.value = data;
        formApi.setValues(formData.value);
      }
    }
  },
});
</script>

<template>
  <Drawer>
    <Form :title="$t('system.user.title')">
      <template #enabled>
        <Switch
          :un-checked-children="0"
          :un-checked-value="0"
          :checked-value="1"
          :checked-children="1"
          v-model:checked="formData!.enabled"
          @change="
            async (value) => {
              try {
                if (value) {
                  await deviceControlOnline(true, {
                    devices: [
                      {
                        device_id: formData!.id as number,
                        device_key: formData!.device_key as string,
                      },
                    ],
                    org_id: formData!.org_id as string,
                  });
                } else {
                  await deviceControlOnline(false, {
                    devices: [
                      {
                        device_id: formData!.id as number,
                        device_key: formData!.device_key as string,
                      },
                    ],
                    org_id: formData!.org_id as string,
                  });
                }
                const res = await getDevice(formData!.id);
                // formApi.setValues(res);
                console.warn('设备详情', res);
              } catch (error) {
                // formApi.setValues({ ...values, online_status });
                console.error(error);
              }
            }
          "
        />
      </template>
      <template #prepend-footer>
        <div class="flex-auto">
          <Button type="primary" danger @click="resetForm">
            {{ $t('common.reset') }}
          </Button>
        </div>
      </template>
    </Form>
  </Drawer>
</template>

<style scoped></style>
