<template>
  <AFormPanel
    ref="formPanel"
    v-bind="formPanelProps">
    <template #default="formData">
      <div class="form-items">
        <ElFormItem label="Id" prop="id">
          <ElInput v-model="formData.id" placeholder="Id"/>
        </ElFormItem>
        <ElFormItem label="仓库名称" prop="warehouseName">
          <ElInput v-model="formData.warehouseName" placeholder="仓库名称"/>
        </ElFormItem>
        <ElFormItem label="仓库类型；raw_material-原料库，finished_product-成品库" prop="type">
          <ElInput v-model="formData.type" placeholder="仓库类型；raw_material-原料库，finished_product-成品库"/>
        </ElFormItem>
        <ElFormItem label="所属站点 Id；关联站点表主键" prop="stationId">
          <ElInput v-model="formData.stationId" placeholder="所属站点 Id；关联站点表主键"/>
        </ElFormItem>
        <ElFormItem label="位置" prop="location">
          <ElInput v-model="formData.location" placeholder="位置"/>
        </ElFormItem>
        <ElFormItem label="最大容量，单位：m³" prop="maxCapacity">
          <ElInput v-model="formData.maxCapacity" placeholder="最大容量，单位：m³"/>
        </ElFormItem>
        <ElFormItem label="备注" prop="remark">
          <ElInput v-model="formData.remark" placeholder="备注"/>
        </ElFormItem>
        <ElFormItem label="创建人 Id； sys_user.id" prop="creatorId">
          <ElInput v-model="formData.creatorId" placeholder="创建人 Id； sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="修改人 Id；sys_user.id" prop="modifierId">
          <ElInput v-model="formData.modifierId" placeholder="修改人 Id；sys_user.id"/>
        </ElFormItem>
        <ElFormItem label="创建时间" prop="createTime">
          <ElInput v-model="formData.createTime" placeholder="创建时间"/>
        </ElFormItem>
        <ElFormItem label="修改时间" prop="modifyTime">
          <ElInput v-model="formData.modifyTime" placeholder="修改时间"/>
        </ElFormItem>
        <ElFormItem label="是否删除； 0-->未删除、1-->已删除" prop="deleted">
          <ElInput v-model="formData.deleted" placeholder="是否删除； 0-->未删除、1-->已删除"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import WarehouseApi from '@/pages/wh/warehouse/warehouse-api.ts'
import AFormPanel, {
  type AFormPanelInstance,
  buildFormPanelProps,
} from '@/components/a-form-panel/AFormPanel.tsx'
import Strings from '@/common/utils/strings.ts'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const formPanelIns = useTemplateRef<AFormPanelInstance>('formPanel')
const status = ref<'add' | 'modify'>('add')
const formPanelProps = buildFormPanelProps<WarehouseTypes.SearchWarehouseResult>({
  detailsLoader(id?: string) {
    if (Strings.isBlank(id)) {
      status.value = 'add'
      return Promise.resolve()
    } else {
      status.value = 'modify'
      return WarehouseApi
        .detail(id!)
        .then(res => res.data)
    }
  },
  doSubmit(data) {
    if (status.value === 'add') {
      return WarehouseApi
        .add(data)
        .then(props.research)
    } else {
      return WarehouseApi
        .modify(data)
        .then(props.research)
    }
  },
  rules: {
    id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
    warehouseName: [ {required: true, message: '请填写仓库名称', trigger: 'blur'} ],
    type: [ {required: true, message: '请填写仓库类型；raw_material-原料库，finished_product-成品库', trigger: 'blur'} ],
    stationId: [ {required: true, message: '请填写所属站点 Id；关联站点表主键', trigger: 'blur'} ],
    location: [ {required: true, message: '请填写位置', trigger: 'blur'} ],
    maxCapacity: [ {required: true, message: '请填写最大容量，单位：m³', trigger: 'blur'} ],
    remark: [ {required: true, message: '请填写备注', trigger: 'blur'} ],
    creatorId: [ {required: true, message: '请填写创建人 Id； sys_user.id', trigger: 'blur'} ],
    modifierId: [ {required: true, message: '请填写修改人 Id；sys_user.id', trigger: 'blur'} ],
    createTime: [ {required: true, message: '请填写创建时间', trigger: 'blur'} ],
    modifyTime: [ {required: true, message: '请填写修改时间', trigger: 'blur'} ],
    deleted: [ {required: true, message: '请填写是否删除； 0-->未删除、1-->已删除', trigger: 'blur'} ],
  },
})
watchEffect(() => {
  formPanelProps.title = status.value === 'add' ? '新建仓库信息' : '修改仓库信息信息'
})
defineExpose({
  open(data?: WarehouseTypes.SearchWarehouseResult) {
    formPanelIns.value?.open(data?.id)
  },
})

</script>

<style lang="stylus" scoped>
.form-items {
  grid-template-columns: 1fr 1fr;
}
</style>
