<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="customerId">
        </ElFormItem>
        <ElFormItem label="采购日期" prop="purchaseDate">
          <el-date-picker v-model="formData.purchaseDate" format="YYYY-MM-DD" placeholder="请选择采购日期" type="date" value-format="YYYY-MM-DD"/>
        </ElFormItem>
        <ElFormItem label="到货日期" prop="arrivalDate">
          <el-date-picker v-model="formData.arrivalDate" format="YYYY-MM-DD" placeholder="请选择采购日期" type="date" value-format="YYYY-MM-DD"/>

          <!-- <ElInput v-model="formData.arrivalDate"  placeholder="到货日期" /> -->
        </ElFormItem>
        <!-- <ElFormItem  label="产品分类" prop="goodsCategoryId">
          <ElInput v-model="formData.goodsCategoryId"  placeholder="产品分类" />
        </ElFormItem>
        <ElFormItem  label="分类名称" prop="goodsCategoryName">
          <ElInput v-model="formData.goodsCategoryName"  placeholder="分类名称" />
        </ElFormItem> -->

        <ElFormItem label="产品" prop="goodsId">
        </ElFormItem>

        <!-- <ElFormItem  label="产品编码" prop="goodSn">
          <ElInput v-model="formData.goodSn"  placeholder="产品编码" />
        </ElFormItem> -->
        <ElFormItem label="数量" prop="quantity">
          <ElInput v-model="formData.quantity" placeholder="数量"/>
        </ElFormItem>
        <ElFormItem label="计量单位" prop="unit">
          <ElInput v-model="formData.unit" placeholder="计量单位" readonly/>
        </ElFormItem>
        <ElFormItem label="总金额" prop="totalMoney">
          <ElInput v-model="formData.totalMoney" placeholder="总金额；单位：元"/>
        </ElFormItem>
        <ElFormItem label="备注" prop="memo">
          <ElInput v-model="formData.memo" placeholder="备注"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import PurchaseOrderApi from '@/pages/wh/purchase-order/purchase-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<PurchaseOrderTypes.SearchPurchaseOrderResult>>({
  sn: [ {required: true, message: '请填写编号', trigger: 'blur'} ],
  purchaseOrderStatus: [ {required: true, message: '请填写状态', trigger: 'blur'} ],
  customerId: [ {required: true, message: '请填写供货客户', trigger: 'blur'} ],
  purchaseDate: [ {required: true, message: '请填写采购日期', trigger: 'blur'} ],
  arrivalDate: [ {required: true, message: '请填写到货日期', trigger: 'blur'} ],
  goodsCategoryId: [ {required: true, message: '请填写产品分类', trigger: 'blur'} ],
  goodsCategoryName: [ {required: true, message: '请填写分类名称', trigger: 'blur'} ],
  goodsId: [ {required: true, message: '请填写产品', trigger: 'blur'} ],
  goodsName: [ {required: true, message: '请填写产品名称', trigger: 'blur'} ],
  goodSn: [ {required: true, message: '请填写产品编码', trigger: 'blur'} ],
  quantity: [ {required: true, message: '请填写数量', trigger: 'blur'} ],
  unit: [ {required: true, message: '请填写计量单位', trigger: 'blur'} ],
  totalMoney: [ {required: true, message: '请填写总金额；单位：元', trigger: 'blur'} ],
  memo: [ {required: true, message: '请填写备注', trigger: 'blur'} ],
  creatorId: [ {required: true, message: '请填写创建人', trigger: 'blur'} ],
  modifierId: [ {required: true, message: '请填写修改人', trigger: 'blur'} ],
  createTime: [ {required: true, message: '请填写创建时间', trigger: 'blur'} ],
  modifyTime: [ {required: true, message: '请填写修改时间', 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 PurchaseOrderApi
      .detail(id!)
      .then(res => res.data)
  }
}

function doSubmit(data: PurchaseOrderTypes.SearchPurchaseOrderResult) {
  if (status.value === 'add') {
    return PurchaseOrderApi.add(data)
      .then(props.research)
  } else {
    return PurchaseOrderApi.modify(data)
      .then(props.research)
  }
}

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

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

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