<template>
  <div class="p-2">
    <el-card shadow="never">
      <template #header>
        <div class="flex justify-between items-center">
          <h3 class="text-lg font-semibold">新增物料入库</h3>
          <el-button type="primary" @click="goBack">返回列表</el-button>
        </div>
      </template>

      <el-form ref="inStockBillFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <!-- 第一列 -->
          <el-col :span="12">
            <el-form-item label="入库单号" prop="inStockCode">
              <el-input v-model="form.inStockCode" placeholder="请输入入库单号" />
            </el-form-item>

            <el-form-item label="入库单类型" prop="inStockType">
              <el-select v-model="form.inStockType" placeholder="请选择入库单类型" style="width: 100%">
                <el-option v-for="dict in wms_in_stock_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="关联单号" prop="inventoryAmount">
              <el-input v-model="form.inventoryAmount" placeholder="请输入关联单号" />
            </el-form-item>

            <el-form-item label="供应商" prop="supplier">
              <el-input v-model="form.supplier" placeholder="请输入供应商" />
            </el-form-item>

            <el-form-item label="联系人" prop="contactUser">
              <el-input v-model="form.contactUser" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>

          <!-- 第二列 -->
          <el-col :span="12">
            <el-form-item label="联系电话" prop="contactNumber">
              <el-input v-model="form.contactNumber" placeholder="请输入联系电话" />
            </el-form-item>

            <el-form-item label="入库单状态" prop="inStockBillStatus">
              <el-select v-model="form.inStockBillStatus" placeholder="请选择入库单状态" style="width: 100%">
                <el-option v-for="dict in wms_in_stock_bill_status" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="流程状态" prop="flowStatus">
              <el-input v-model="form.flowStatus" placeholder="请输入流程状态" />
            </el-form-item>

            <el-form-item label="仓库ID" prop="warehouseId">
              <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
            </el-form-item>
          </el-col>
        </el-row>

        <!-- 全宽字段 -->
        <el-form-item label="入库说明" prop="directions">
          <el-input v-model="form.directions" type="textarea" :rows="3" placeholder="请输入入库说明" style="width: 100%" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注" style="width: 100%" />
        </el-form-item>
      </el-form>

      <!-- 底部操作按钮 -->
      <div class="flex justify-center mt-6 space-x-4">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm" size="large">
          <el-icon>
            <Check />
          </el-icon>
          提交
        </el-button>
        <el-button @click="resetForm" size="large">
          <el-icon>
            <Refresh />
          </el-icon>
          重置
        </el-button>
        <el-button @click="goBack" size="large">
          <el-icon>
            <ArrowLeft />
          </el-icon>
          返回
        </el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup name="InStockBillAdd" lang="ts">
import { addInStockBill } from '@/api/wms/inStockBill';
import { InStockBillForm } from '@/api/wms/inStockBill/types';
import { ref } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { wms_in_stock_type, wms_in_stock_bill_status } = toRefs<any>(proxy?.useDict('wms_in_stock_type', 'wms_in_stock_bill_status'));

const inStockBillFormRef = ref<ElFormInstance>();
const buttonLoading = ref(false);

const initFormData: InStockBillForm = {
  inStockBillId: undefined,
  inStockCode: undefined,
  inStockType: undefined,
  projectId: undefined,
  inventoryAmount: undefined,
  supplier: undefined,
  contactUser: undefined,
  contactNumber: undefined,
  directions: undefined,
  inStockBillStatus: undefined,
  flowStatus: undefined,
  warehouseId: undefined,
  remark: undefined
};

const form = ref<InStockBillForm>({ ...initFormData });

const rules = ref({
  inStockCode: [{ required: true, message: '入库单号不能为空', trigger: 'blur' }],
  inStockType: [{ required: true, message: '入库单类型不能为空', trigger: 'change' }],
  supplier: [{ required: true, message: '供应商不能为空', trigger: 'blur' }]
});

/** 提交表单 */
const submitForm = () => {
  inStockBillFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      try {
        await addInStockBill(form.value);
        proxy?.$modal.msgSuccess('新增成功');
        goBack();
      } catch (error) {
        console.error('新增失败:', error);
      } finally {
        buttonLoading.value = false;
      }
    }
  });
};

/** 重置表单 */
const resetForm = () => {
  form.value = { ...initFormData };
  inStockBillFormRef.value?.resetFields();
};

/** 返回列表页 */
const goBack = () => {
  router.push('/wms/inStockBill');
};

// 页面加载时初始化
onMounted(() => {
  resetForm();
});
</script>

<style scoped>
.el-form-item {
  margin-bottom: 20px;
}

.el-input,
.el-select,
.el-textarea {
  width: 100%;
}
</style>
