<template>
  <AFormPanel
    ref="formPanel"
    :details-loader="detailsLoader"
    :do-submit="doSubmit"
    :rules="rules"
    :title="status === 'add' ? '新建项目' : '修改项目'"

  >
    <template #default="{formData}">
      <div class="form-items">
        <ElFormItem label="仓库" prop="warehouseId">
        </ElFormItem>
        <ElFormItem label="产品" prop="goodsId">
        </ElFormItem>
        <ElFormItem label="存放位置" prop="location">
          <ElInput
            v-model="formData.location"

            placeholder="存放位置"/>
        </ElFormItem>
        <ElFormItem label="当前库存数量" prop="stockQuantity">
          <ElInput
            v-model="formData.stockQuantity"

            placeholder="当前库存数量"/>
        </ElFormItem>
        <ElFormItem label="锁定数量" prop="lockQuantity">
          <ElInput
            v-model="formData.lockQuantity"

            placeholder="锁定数量"/>
        </ElFormItem>
        <ElFormItem label="总量" prop="totalQuantity">
          <ElInput
            v-model="formData.totalQuantity"

            placeholder="总量"/>
        </ElFormItem>
        <ElFormItem label="剩余量" prop="remainingQuantity">
          <ElInput
            v-model="formData.remainingQuantity"

            placeholder="剩余量"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import InventoryApi from '@/pages/wh/inventory/inventory-api.ts'
import Strings from '@/common/utils/strings.ts'
import { type FormRules } from 'element-plus'
import type { ComponentExposed } from 'vue-component-type-helpers'
import AFormPanel from '@/components/a-form-panel/AFormPanel.tsx'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})

const formPanelIns = useTemplateRef<ComponentExposed<typeof AFormPanel>>('formPanel')
const status = ref<'add' | 'modify'>('add')
const rules = reactive<FormRules<InventoryTypes.SearchInventoryResult>>({
  id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
  warehouseId: [ {required: true, message: '请填写仓库 Id', trigger: 'blur'} ],
  goodsId: [ {required: true, message: '请填写产品 Id', trigger: 'blur'} ],
  location: [ {required: true, message: '请填写存放位置', trigger: 'blur'} ],
  stockQuantity: [ {required: true, message: '请填写当前库存数量', trigger: 'blur'} ],
  lockQuantity: [ {required: true, message: '请填写锁定数量', trigger: 'blur'} ],
  totalQuantity: [ {required: true, message: '请填写总量', trigger: 'blur'} ],
  remainingQuantity: [ {required: true, message: '请填写剩余量', trigger: 'blur'} ],
  unit: [ {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'} ],
})

function detailsLoader(id?: string) {
  if (Strings.isBlank(id)) {
    status.value = 'add'
    return Promise.resolve()
  } else {
    status.value = 'modify'
    return InventoryApi
      .detail(id!)
      .then(res => res.data)
  }
}

function doSubmit(data: InventoryTypes.SearchInventoryResult) {
  if (status.value === 'add') {
    return InventoryApi.add(data)
      .then(props.research)
  } else {
    return InventoryApi.modify(data)
      .then(props.research)
  }
}

defineExpose({
  open(data?: InventoryTypes.SearchInventoryResult) {
    formPanelIns.value?.open(data?.id)
  },
})
</script>


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

  :deep(.el-form-item) {
  }
}
</style>
