<template>
  <!-- 仓库管理对话框组件 -->
  <el-dialog v-model="dialogVisible" title="仓库管理" width="600px" append-to-body @close="handleClose">
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="仓库名称" prop="name">
            <el-input v-model="formData.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库性质">
            <el-select v-model="formData.warehouseNature" placeholder="请选择" style="width: 100%">
              <el-option label="自有仓库" :value="1" />
              <el-option label="租赁仓库" :value="2" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="所在地区">
            <el-input v-model="formData.warehouseLocation" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="仓库面积">
            <el-input v-model="formData.warehouseArea" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="所在地址">
            <el-input v-model="formData.warehouseAddress" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="联系人">
            <el-input v-model="formData.warehouseContacts" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="联系电话">
            <el-input v-model="formData.phone" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="附件">
            <el-upload class="upload-demo" action="#" :auto-upload="false">
              <el-button size="small">上传</el-button>
              <template #tip>
                <div class="el-upload__tip">支持jpg/png/xlsx等文件</div>
              </template>
            </el-upload>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="formData.remark" type="textarea" :rows="2" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确认</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue';
import type { PartnerWarehouseForm } from '@/api/partner/warehouse/types';

// ========== 组件属性定义 ==========
// 这些是父组件传进来的数据
interface Props {
  visible: boolean;           // 对话框是否显示
  formData: PartnerWarehouseForm;  // 表单数据
}

// ========== 组件事件定义 ==========
// 这些是子组件要通知父组件的事件
interface Emits {
  (e: 'update:visible', value: boolean): void;  // 更新显示状态
  (e: 'update:formData', value: PartnerWarehouseForm): void;  // 更新表单数据
  (e: 'submit', value: PartnerWarehouseForm): void;  // 提交表单
}

// 接收父组件传来的属性
const props = defineProps<Props>();
// 定义可以触发的事件
const emit = defineEmits<Emits>();

// ========== 对话框显示状态（计算属性） ==========
// 使用 computed 创建一个可读写的计算属性
// 读取时返回 props.visible，写入时触发 update:visible 事件
const dialogVisible = computed({
  get: () => props.visible,
  set: (value) => emit('update:visible', value)
});

// ========== 表单验证规则 ==========
const rules = {
  name: [{ required: true, message: '仓库名称不能为空', trigger: 'blur' }]
};

// ========== 表单引用 ==========
const formRef = ref();

// ========== 关闭对话框 ==========
const handleClose = () => {
  // 通知父组件关闭对话框
  emit('update:visible', false);
  // 重置表单验证状态
  formRef.value?.resetFields();
};

// ========== 提交表单 ==========
const handleSubmit = () => {
  // 先验证表单
  formRef.value?.validate((valid: boolean) => {
    if (valid) {
      // 验证通过，通知父组件提交
      emit('submit', props.formData);
    }
  });
};
</script>

<style scoped lang="scss">
// 组件样式（如果需要的话）
</style>
