<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">
          <ASelect v-model="formData.responsibleId" :api="UserApi.paging" :tableColumn="userColumn" labelKey="nickname" placeholder="请选择负责人"/>
        </ElFormItem>

        <ElFormItem label="仓库" prop="warehouseId">
          <ASelect v-model="formData.warehouseId" :api="WarehouseApi.paging" :tableColumn="warehouseColumn" labelKey="warehouseName" placeholder="选择仓库" @change="warehouseChange"/>
        </ElFormItem>

        <ElFormItem label="存放位置" prop="location">
          <ElInput v-model="formData.location" placeholder="存放位置"/>
        </ElFormItem>
        <ElFormItem label="出库时间" prop="outTime">
          <ElDatePicker v-model="formData.outTime" format="YYYY-MM-DD" placeholder="请选择出库日期" type="date" value-format="YYYY-MM-DD"/>
        </ElFormItem>

        <ElFormItem label="销售单" prop="orderId">
          <ASelect v-model="formData.orderId" :api="SalesOrderApi.paging" :tableColumn="orderColumn" labelKey="sn" placeholder="请选择销售单" @change="orderChange"/>
          <!-- <ElInput v-model="formData.orderId"  placeholder="销售单 Id；wh_sales_order.id" /> -->
        </ElFormItem>
        <!-- <ElFormItem  label="销售单号；wh_sales_order.sn" prop="orderSn">
          <ElInput v-model="formData.orderSn"  placeholder="销售单号；wh_sales_order.sn" />
        </ElFormItem> -->
        <ElFormItem label="产品分类" prop="goodsCategoryId">
          <ElInput v-model="formData.goodsCategoryId" placeholder="产品分类 Id"/>
        </ElFormItem>
        <ElFormItem label="分类名称" prop="goodsCategoryName">
          <ElInput v-model="formData.goodsCategoryName" placeholder="分类名称"/>
        </ElFormItem>
        <ElFormItem label="产品" prop="goodsId">
          <ElInput v-model="formData.goodsId" placeholder="产品 Id"/>
        </ElFormItem>
        <ElFormItem label="产品名称" prop="goodsName">
          <ElInput v-model="formData.goodsName" placeholder="产品名称"/>
        </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="计量单位；字典代码：unit" prop="unit">
          <ElInput v-model="formData.unit"  placeholder="计量单位；字典代码：unit" />
        </ElFormItem> -->
        <ElFormItem label="备注" prop="memo">
          <ElInput v-model="formData.memo" placeholder="备注"/>
        </ElFormItem>
      </div>
    </template>
  </AFormPanel>
</template>

<script lang="ts" setup>
import OutOrderApi from '@/pages/wh/out-order/out-order-api.ts'
import Strings from '@/common/utils/strings.ts'
import { type FormRules } from 'element-plus'
import SalesOrderApi from '@/pages/wh/sales-order/sales-order-api.ts'
import WarehouseApi from '@/pages/wh/warehouse/warehouse-api.ts'
import ASelect from '@/components/a-select/ASelect.vue'
import UserApi from '@/pages/sys/user/user-api.ts'
import type AFormPanel from '@/components/a-form-panel/AFormPanel.tsx'
import type { ComponentExposed } from 'vue-component-type-helpers'

const props = withDefaults(defineProps<{
  research?: () => void
}>(), {
  research: () => {
  },
})
const rules = reactive<FormRules<OutOrderTypes.SearchOutOrderResult>>({
  id: [ {required: true, message: '请填写Id', trigger: 'blur'} ],
  sn: [ {required: true, message: '请填写编号', trigger: 'blur'} ],
  responsibleId: [ {required: true, message: '请填写责任人 Id；sys_user.id', trigger: 'blur'} ],
  warehouseId: [ {required: true, message: '请填写仓库 Id；wh_warehouse.id', trigger: 'blur'} ],
  location: [ {required: true, message: '请填写存放位置', trigger: 'blur'} ],
  outTime: [ {required: true, message: '请填写出库时间', trigger: 'blur'} ],
  orderId: [ {required: true, message: '请填写销售单 Id；wh_sales_order.id', trigger: 'blur'} ],
  orderSn: [ {required: true, message: '请填写销售单号；wh_sales_order.sn', 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 OutOrderApi
      .detail(id!)
      .then(res => res.data)
  }
}

function doSubmit(data: OutOrderTypes.SearchOutOrderResult) {
  if (status.value === 'add') {
    return OutOrderApi.add(data)
      .then(props.research)
  } else {
    return OutOrderApi.modify(data)
      .then(props.research)
  }
}

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

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

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