<template>
  <a-modal
      v-model:visible="visible"
      :title="title"
      :mask-closable="false"
      :esc-to-close="false"
      :width="width >= 600 ? 600 : '100%'"
      draggable
      @before-ok="save"
      @close="reset"
  >
    <GiForm ref="formRef" v-model="form" :columns="columns" />
  </a-modal>
</template>

<script setup lang="ts">
import { Message } from '@arco-design/web-vue'
import { useWindowSize } from '@vueuse/core'
import { getProduct, addProduct, updateProduct } from '@/apis/equipment/product'
import {type ColumnItem, type ColumnItemRequest, GiForm} from '@/components/GiForm'
import { useResetReactive } from '@/hooks'
import qs from "query-string";
import {useDict} from "@/hooks/app";

const emit = defineEmits<{
  (e: 'save-success'): void
}>()

const { width } = useWindowSize()

const dataId = ref('')
const visible = ref(false)
const isUpdate = computed(() => !!dataId.value)
const title = computed(() => (isUpdate.value ? '修改产品信息' : '新增产品信息'))
const formRef = ref<InstanceType<typeof GiForm>>()
const {
  product_bigtype, product_subtype, product_genre, product_network, product_attestation
} = useDict('product_bigtype', 'product_subtype',
    'product_genre', 'product_network',
    'product_attestation');

const [form, resetForm] = useResetReactive({

})
const file = ref();
const columns: ColumnItem[] = reactive([
  {
    label: '产品图片',
    field: 'avatar',
    type: 'image',
    savePath: 'product/',
    span: 24,
  },
  {
    label: '产品名称',
    field: 'name',
    type: 'input',
    span: 24,
    props: {
      maxLength: 25,
    },
    required: true,
  },
  {
    label: '产品型号',
    field: 'version',
    type: 'input',
    span: 24,
  },
  {
    label: '产品品类（大类）',
    field: 'bigtype',
    type: 'select',
    props: {
      options: product_bigtype,
      multiple: false,
    },
    span: 24,
  },
  {
    label: '产品品类（小类）',
    field: 'subtype',
    type: 'select',
    props: {
      options: product_subtype,
      multiple: false,
    },
    span: 24,
  },
  {
    label: '节点类型',
    field: 'genre',
    type: 'select',
    props: {
      options: product_genre,
      multiple: false,
    },
    span: 24,
  },
  {
    label: '产品描述',
    field: 'describes',
    type: 'input',
    span: 24,
  },
  {
    label: '联网方式',
    field: 'network',
    type: 'select',
    props: {
      options: product_network,
      multiple: false,
    },
    span: 24,
  },
  {
    label: '数据格式',
    field: 'dataFormat',
    type: 'input',
    span: 24,
  },
  {
    label: '数据校验级别',
    field: 'scale',
    type: 'input',
    span: 24,
  },
  {
    label: '认证方式',
    field: 'attestation',
    type: 'select',
    props: {
      options: product_attestation,
      multiple: false,
    },
    span: 24,
  },
]);

// 重置
const reset = () => {
  formRef.value?.formRef?.resetFields()
  resetForm()
}

// 保存
const save = async () => {
  try {
    const isInvalid = await formRef.value?.formRef?.validate()
    if (isInvalid) return false
    if (isUpdate.value) {
      await updateProduct(form, dataId.value)
      Message.success('修改成功')
    } else {
      await addProduct(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

// 新增
const onAdd = async () => {
  reset()
  dataId.value = ''
  visible.value = true
}

// 修改
const onUpdate = async (record: object) => {
  reset()
  dataId.value = record.id;
  Object.assign(form, record)
  visible.value = true
}

// 修改
const test = async (record: object) => {
  await test(form)
}

defineExpose({ onAdd, onUpdate })
</script>

<style scoped lang="scss"></style>
