<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 { addMaterialInfo, getMaterialInfo, updateMaterialInfo } from '@/apis/material/materialInfo'
import { type ColumnItem, GiForm } from '@/components/GiForm'
import { useResetReactive } from '@/hooks'
import { materialType } from "@/hooks/app/materialType";
import {materialProcess} from "@/hooks/app/materialProcess";
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 { materialTypeList, getMaterialTypeSelect } = materialType()
const { light_level } = useDict('light_level')



const [form, resetForm] = useResetReactive({
  // todo 待补充
})

const columns: ColumnItem[] = reactive([
  {
    label: '物料名称',
    field: 'materialName',
    type: 'input',
    span: 24,
    required: true,
  },
  {
    label: '物料编码',
    field: 'encoding',
    type: 'input',
    span: 24,
    required: true,
  },
  {
    label: '物料单位重量，单位(g)',
    field: 'unitWeight',
    type: 'input-number',
    span: 24,
    required: true,
  },
  {
    label: '物料品类',
    field: 'materialTypeId',
    type: 'select',
    span: 24,
    required: true,
    props: {
      options: materialTypeList,
      allowClear: true,
      allowSearch: true,
    },
  },
  {
    label: '灯光等级',
    field: 'lightLevel',
    type: 'select',
    span: 24,
    props: {
      options: light_level,
      allowClear: true,
    },
  },
  {
    label: '物料流程编码',
    field: 'materialProcess',
    type: 'input',
    span: 24,
    required: true,
  },
  {
    label: '物料规格',
    field: 'materialSpec',
    type: 'input',
    span: 24,
  },
  {
    label: '物料照片上传',
    field: 'photoUrl',
    type: 'image',
    savePath: 'material/',
    span: 24,
    required: true,
  },
])

// 重置
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 updateMaterialInfo(form, dataId.value)
      Message.success('修改成功')
    } else {
      await addMaterialInfo(form)
      Message.success('新增成功')
    }
    emit('save-success')
    return true
  } catch (error) {
    return false
  }
}

// 新增
const onAdd = async () => {
  reset()
  dataId.value = ''
  if (!materialTypeList.value.length) {
    await getMaterialTypeSelect()
  }
  visible.value = true
}

// 修改
const onUpdate = async (id: string) => {
  reset()
  dataId.value = id
  const { data } = await getMaterialInfo(id)
  if (!materialTypeList.value.length) {
    await getMaterialTypeSelect()
  }
  Object.assign(form, data)
  form.lightLevel = data.lightLevel ? JSON.stringify(data.lightLevel) : ''
  visible.value = true
}

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

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