<template>
  <div class="warehouse">
    <div class="toolbar">
      <el-button type="primary" @click="handleAdd">新增</el-button>
    </div>
    <el-table :data="warehouseList" border>
      <el-table-column prop="name" label="仓库名称" />
      <el-table-column prop="warehouseLocation" label="所在地区" />
      <el-table-column prop="warehouseAddress" label="所在地址" />
      <el-table-column prop="warehouseContacts" label="仓库联系人" />
      <el-table-column prop="phone" label="联系电话" />
      <el-table-column label="操作" width="150">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button link type="danger" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="600px">
      <el-form :model="formData" label-width="120px">
        <el-form-item label="仓库名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="仓库性质">
          <el-select v-model="formData.warehouseNature" placeholder="请选择">
            <el-option label="自有" :value="1" />
            <el-option label="租赁" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="所在地区">
          <el-input v-model="formData.warehouseLocation" />
        </el-form-item>
        <el-form-item label="所在地址">
          <el-input v-model="formData.warehouseAddress" />
        </el-form-item>
        <el-form-item label="仓库面积(m2)">
          <el-input v-model="formData.warehouseArea" />
        </el-form-item>
        <el-form-item label="仓库联系人">
          <el-input v-model="formData.warehouseContacts" />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input v-model="formData.phone" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark" type="textarea" :rows="3" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { ElMessage, ElMessageBox } from 'element-plus';
import { getCurrentPartnerInfo, getPartnerWarehouseList, addPartnerWarehouse, updatePartnerWarehouse, deletePartnerWarehouse } from '@/api/partner';

const warehouseList = ref([]);
const loading = ref(false);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const formData = ref({
  id: null,
  partnerId: null,
  name: '',
  warehouseNature: null,
  warehouseLocation: '',
  warehouseAddress: '',
  warehouseArea: '',
  warehouseContacts: '',
  phone: '',
  remark: ''
});

const fetchWarehouseList = async () => {
  loading.value = true;
  try {
    const partnerResponse = await getCurrentPartnerInfo();
    if (partnerResponse.code === 200 && partnerResponse.data) {
      const partnerId = partnerResponse.data.id;
      formData.value.partnerId = partnerId;
      const response = await getPartnerWarehouseList(partnerId);
      if (response.code === 200) {
        warehouseList.value = response.rows || response.data || [];
      }
    }
  } catch (error) {
    console.error('获取仓库列表失败', error);
    ElMessage.error('获取仓库列表失败');
  } finally {
    loading.value = false;
  }
};

const handleAdd = () => {
  dialogTitle.value = '新增仓库';
  formData.value = {
    id: null,
    partnerId: formData.value.partnerId,
    name: '',
    warehouseNature: null,
    warehouseLocation: '',
    warehouseAddress: '',
    warehouseArea: '',
    warehouseContacts: '',
    phone: '',
    remark: ''
  };
  dialogVisible.value = true;
};

const handleEdit = (row: any) => {
  dialogTitle.value = '编辑仓库';
  formData.value = { ...row };
  dialogVisible.value = true;
};

const handleDelete = (row: any) => {
  ElMessageBox.confirm('确定要删除该仓库吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    try {
      const response = await deletePartnerWarehouse([row.id]);
      if (response.code === 200) {
        ElMessage.success('删除成功');
        fetchWarehouseList();
      } else {
        ElMessage.error(response.msg || '删除失败');
      }
    } catch (error) {
      console.error('删除失败', error);
      ElMessage.error('删除失败');
    }
  });
};

const handleSubmit = async () => {
  try {
    const apiCall = formData.value.id ? updatePartnerWarehouse : addPartnerWarehouse;
    const response = await apiCall(formData.value);
    if (response.code === 200) {
      ElMessage.success(formData.value.id ? '修改成功' : '新增成功');
      dialogVisible.value = false;
      fetchWarehouseList();
    } else {
      ElMessage.error(response.msg || '操作失败');
    }
  } catch (error) {
    console.error('操作失败', error);
    ElMessage.error('操作失败');
  }
};

onMounted(() => {
  fetchWarehouseList();
});
</script>

<style scoped lang="scss">
.warehouse {
  padding: 20px;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  justify-content: flex-end;
}
</style>
