<template>
  <BasicDrawer
    v-bind="$attrs"
    @register="registerDrawer"
    :title="getTitle"
    :width="adaptiveWidth"
    @ok="handleSubmit"
    :showFooter="showFooter"
    destroyOnClose
  >
    <BasicForm @register="registerForm" >
      <template #customInput="{ model, field }">
        <a-button preIcon="ant-design:poweroff-outlined" @click="restartNow(model)">立即重启</a-button>
      </template>
    </BasicForm>
  </BasicDrawer>
</template>

<script lang="ts" setup>
import {defineComponent, ref, computed, unref, useAttrs, createVNode, h} from 'vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { formSchema } from "@/views/iot/tplink/camera/camera.data";
import { update,rebootDevice } from '@/views/iot/tplink/camera/camera.api';
import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
import { useDrawerAdaptiveWidth } from '/@/hooks/jeecg/useAdaptiveWidth';
import { getTenantId } from "/@/utils/auth";
import {Modal} from "ant-design-vue";
import {ExclamationCircleOutlined} from "@ant-design/icons-vue";

// 声明Emits
const emit = defineEmits(['success', 'register']);
const attrs = useAttrs();
const isUpdate = ref(true);
const rowId = ref('');
const departOptions = ref([]);
let isFormDepartUser = false;
//表单配置
const [registerForm, { setProps, resetFields, setFieldsValue, validate, updateSchema }] = useForm({
  labelWidth: 90,
  schemas: formSchema,
  showActionButtonGroup: false,
});
const showFooter = ref(true);
//表单赋值
const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
  await resetFields();
  showFooter.value = data?.showFooter ?? true;
  setDrawerProps({ confirmLoading: false, showFooter: showFooter.value });
  isUpdate.value = !!data?.isUpdate;
  // 无论新增还是编辑，都可以设置表单值
  if (typeof data.record === 'object') {
    setFieldsValue({
      ...data.record,
    });
  }
  // 隐藏底部时禁用整个表单
  setProps({ disabled: !showFooter.value });
});
//获取标题
const getTitle = computed(() => {
  if (!unref(isUpdate)) {
    return '新增摄像头';
  } else {
    return unref(showFooter) ? '编辑摄像头' : '摄像头详情';
  }
});
const { adaptiveWidth } = useDrawerAdaptiveWidth();

/**
 * 提交事件
 */
async function handleSubmit() {
  try {
    let values = await validate();
    setDrawerProps({ confirmLoading: true });
    let params = values;
    await update(params);
    //关闭弹窗
    closeDrawer();
    //刷新列表
    emit('success');
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}

/**
 * 设备重启
 * @param record
 */
function restartNow(record){
  Modal.confirm({
    title: '设备重启',
    width: '500px',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode('div', { style: 'color:red;' }, '重启设备过程中，请勿插拔电源，以免损坏设备。确定要重启吗？'),
    okText: '重启',
    onOk() {
      let params = {
        deviceIndex: record.deviceIndex,
        projectId: record.projectId,
      };
      rebootDevice(params);
      Modal.success({
        title: '重启IPC',
        okText: '确定',
        content: h('div', {}, [
          h('p', '正在重启IPC，重启过程大约需要60秒钟'),
          h('p', '重启后，请刷新表格以获取最新设备状态'),
        ]),
      });
    },
    onCancel() {
      // console.log('Cancel');
    },
    class: 'test',
  });
}

</script>

<style scoped>

</style>
