<template>
  <ElDialog v-model="showDialog"
    :close-on-click-modal="false"
    destroy-on-close
    width="fit-content"
    @close="dialogCloseHandler">
    <ElForm :model="formData"
      :rules="rules"
      ref="inventoryForm"
      class="form-panel"
      label-width="auto">
       <ElFormItem label="Id" prop="id">
        <ElInput
          v-model="formData.id"
          :disabled="status === 'view'"
          placeholder="Id"/>
      </ElFormItem>
       <ElFormItem label="仓库 Id" prop="warehouseId">
        <ElInput
          v-model="formData.warehouseId"
          :disabled="status === 'view'"
          placeholder="仓库 Id"/>
      </ElFormItem>
       <ElFormItem label="产品 Id" prop="goodsId">
        <ElInput
          v-model="formData.goodsId"
          :disabled="status === 'view'"
          placeholder="产品 Id"/>
      </ElFormItem>
       <ElFormItem label="存放位置" prop="location">
        <ElInput
          v-model="formData.location"
          :disabled="status === 'view'"
          placeholder="存放位置"/>
      </ElFormItem>
       <ElFormItem label="当前库存数量" prop="stockQuantity">
        <ElInput
          v-model="formData.stockQuantity"
          :disabled="status === 'view'"
          placeholder="当前库存数量"/>
      </ElFormItem>
       <ElFormItem label="锁定数量" prop="lockQuantity">
        <ElInput
          v-model="formData.lockQuantity"
          :disabled="status === 'view'"
          placeholder="锁定数量"/>
      </ElFormItem>
       <ElFormItem label="总量" prop="totalQuantity">
        <ElInput
          v-model="formData.totalQuantity"
          :disabled="status === 'view'"
          placeholder="总量"/>
      </ElFormItem>
       <ElFormItem label="剩余量" prop="remainingQuantity">
        <ElInput
          v-model="formData.remainingQuantity"
          :disabled="status === 'view'"
          placeholder="剩余量"/>
      </ElFormItem>
       <ElFormItem label="计量单位；字典代码：unit" prop="unit">
        <ElInput
          v-model="formData.unit"
          :disabled="status === 'view'"
          placeholder="计量单位；字典代码：unit"/>
      </ElFormItem>
       <ElFormItem label="创建人 Id； sys_user.id" prop="creatorId">
        <ElInput
          v-model="formData.creatorId"
          :disabled="status === 'view'"
          placeholder="创建人 Id； sys_user.id"/>
      </ElFormItem>
       <ElFormItem label="修改人 Id； sys_user.id" prop="modifierId">
        <ElInput
          v-model="formData.modifierId"
          :disabled="status === 'view'"
          placeholder="修改人 Id； sys_user.id"/>
      </ElFormItem>
       <ElFormItem label="创建时间" prop="createTime">
        <ElInput
          v-model="formData.createTime"
          :disabled="status === 'view'"
          placeholder="创建时间"/>
      </ElFormItem>
       <ElFormItem label="修改时间" prop="modifyTime">
        <ElInput
          v-model="formData.modifyTime"
          :disabled="status === 'view'"
          placeholder="修改时间"/>
      </ElFormItem>
       <ElFormItem label="是否删除； 0-->未删除、1-->已删除" prop="deleted">
        <ElInput
          v-model="formData.deleted"
          :disabled="status === 'view'"
          placeholder="是否删除； 0-->未删除、1-->已删除"/>
      </ElFormItem>
    </ElForm>
    <template #footer>
      <ElButton @click="showDialog = false">{{ status === 'view' ? '关闭' : '取消' }}</ElButton>
      <ElButton v-if="status !== 'view'" :loading="submiting" type="primary" @click="submitHandler">提交</ElButton>
    </template>
  </ElDialog>
</template>

<script lang="ts" setup>
import InventoryApi from '@/pages/wh/inventory/inventory-api.ts'
import Strings from '@/common/utils/strings.ts'
import FormUtil from '@/common/utils/formUtil.ts'
import Utils from '@/common/utils'
import {
  ElMessage,
  type FormInstance,
  type FormRules,
} from 'element-plus'

const emits = defineEmits(['editSucc'])
const showDialog = ref(false)
const submiting = ref(false)
const status = ref<'add' | 'view' | 'modify'>('add')

const inventoryFormIns = useTemplateRef<FormInstance>('inventoryForm')

const formData = Utils.resetAble(reactive<InventoryTypes.SearchInventoryResult>({}))
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: '请填写计量单位；字典代码：unit', 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 dialogCloseHandler() {
  formData.$reset()
}

function submitHandler() {
  if (status.value === 'view') return
  submiting.value = true
  if (formData.id != null) {
  FormUtil.submit(inventoryFormIns, () => InventoryApi.modify(formData))
      .then(() => {
        ElMessage.success('修改成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  } else {
  FormUtil.submit(inventoryFormIns, () => InventoryApi.add(formData))
      .then(() => {
        ElMessage.success('添加成功')
        emits('editSucc')
        showDialog.value = false
      })
      .finally(() => {
        submiting.value = false
      })
  }
}

defineExpose({
  open(data: InventoryTypes.SearchInventoryResult = {}) {
    showDialog.value = true
    if (!Strings.isBlank(data.id)) {
      status.value = 'modify'
      InventoryApi.detail(data.id!)
        .then(res => {
          formData.$reset(res.data)
        })
    } else {
      status.value = 'add'
      formData.$reset(data)
    }
  }
})
</script>

<style lang="stylus" scoped>
.form-panel {
  padding 20px
}
</style>
