<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="responsibleId">
        </ElFormItem>

        <ElFormItem label="仓库" prop="warehouseId">
        </ElFormItem>
        <ElFormItem label="存放位置" prop="location">
          <ElInput v-model="formData.location" placeholder="存放位置" readonly/>
        </ElFormItem>
        <ElFormItem label="入库时间" prop="inTime">
          <ElDatePicker v-model="formData.inTime" format="YYYY-MM-DD" placeholder="请选择入库日期" type="date" value-format="YYYY-MM-DD"/>
          <!-- <ElInput v-model="formData.inTime"  placeholder="入库时间" /> -->
        </ElFormItem>
        <ElFormItem label="采购单" prop="orderId">
        </ElFormItem>
        <ElFormItem label="采购单单号" prop="orderSn">
          <ElInput v-model="formData.orderSn" placeholder="采购单单号；wh_purchase_order.sn" readonly/>
        </ElFormItem>
        <!-- <ElFormItem  label="产品分类 Id" prop="goodsCategoryId">
          <ElInput v-model="formData.goodsCategoryId"  placeholder="产品分类 Id" />
        </ElFormItem> -->
        <ElFormItem label="分类名称" prop="goodsCategoryName">
          <ElInput v-model="formData.goodsCategoryName" placeholder="分类名称" readonly/>
        </ElFormItem>
        <ElFormItem label="产品名称" prop="goodsName">
          <ElInput v-model="formData.goodsName" placeholder="产品名称" readonly/>
        </ElFormItem>
        <!-- <ElFormItem  label="产品编码" prop="goodSn">
          <ElInput v-model="formData.goodSn" readonly  placeholder="产品编码" />
        </ElFormItem> -->
        <ElFormItem label="数量" prop="quantity">
          <ElInput v-model="formData.quantity" placeholder="数量" readonly/>
        </ElFormItem>
        <!-- <ElFormItem  label="计量单位；字典代码：unit" prop="unit">
          <ElInput v-model="formData.unit"  placeholder="计量单位；字典代码：unit" />
        </ElFormItem> -->
        <ElFormItem label="备注" prop="memo">
          <ElInput v-model="formData.memo" placeholder="备注" readonly/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import InOrderApi from '@/pages/wh/in-order/in-order-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 rules = reactive<FormRules<InOrderTypes.SearchInOrderResult>>({
  id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
  sn: [ {required: true, message: '请填写编号', trigger: 'blur'} ],
  responsibleId: [ {required: true, message: '请填写责任人', trigger: 'blur'} ],
  warehouseId: [ {required: true, message: '请填写仓库', trigger: 'blur'} ],
  location: [ {required: true, message: '请填写存放位置', trigger: 'blur'} ],
  inTime: [ {required: true, message: '请填写入库时间', trigger: 'blur'} ],
  orderId: [ {required: true, message: '请填写采购单', trigger: 'blur'} ],
  orderSn: [ {required: true, message: '请填写采购单单号', trigger: 'blur'} ],
  goodsCategoryId: [ {required: true, message: '请填写产品分类 Id', trigger: 'blur'} ],
  goodsCategoryName: [ {required: true, message: '请填写分类名称', trigger: 'blur'} ],
  goodsId: [ {required: true, message: '请填写产品 Id', trigger: 'blur'} ],
  goodsName: [ {required: true, message: '请填写产品名称', trigger: 'blur'} ],
  goodSn: [ {required: true, message: '请填写产品编码', trigger: 'blur'} ],
  quantity: [ {required: true, message: '请填写数量', trigger: 'blur'} ],
  unit: [ {required: true, message: '请填写计量单位；字典代码：unit', trigger: 'blur'} ],
  memo: [ {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'} ],
})
const formPanelIns = useTemplateRef<ComponentExposed<typeof AFormPanel>>('formPanel')

const status = ref<'add' | 'modify'>('add')

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

function doSubmit(data: InOrderTypes.SearchInOrderResult) {
  if (status.value === 'add') {
    return InOrderApi.add(data)
      .then(props.research)
  } else {
    return InOrderApi.modify(data)
      .then(props.research)
  }
}

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

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

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