<template>
  <div class="contract-list">
    <el-card>
      <template slot="header">
        <div class="card-header">
          <span>合同档案</span>
        </div>
      </template>
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value=""></el-option>
            <el-option label="有效" value="active"></el-option>
            <el-option label="过期" value="expired"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="filteredContracts" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="regionName" label="区域"></el-table-column>
        <el-table-column prop="apartmentName" label="公寓"></el-table-column>
        <el-table-column prop="roomNumber" label="房间号"></el-table-column>
        <el-table-column prop="tenantName" label="租客姓名"></el-table-column>
        <el-table-column prop="startDate" label="开始日期" width="150"></el-table-column>
        <el-table-column prop="endDate" label="结束日期" width="150"></el-table-column>
        <el-table-column prop="rent" label="租金(元/月)" width="120"></el-table-column>
        <el-table-column prop="deposit" label="押金(元)" width="120"></el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.status === 'active' ? 'success' : 'danger'">{{ scope.row.status === 'active' ? '有效' : '过期' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" width="180"></el-table-column>

      </el-table>
      <div class="pagination" style="margin-top: 20px;">
        <el-pagination
          layout="prev, pager, next"
          :total="filteredContracts.length"
          :page-size="10"
          :current-page="currentPage"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { contractApi } from '../../api/api'

export default {
  name: 'ContractList',
  data() {
    return {
      contracts: [],
      searchForm: {
        status: ''
      },
      currentPage: 1
    }
  },
  computed: {
    filteredContracts() {
      return this.contracts.filter(contract => {
        return !this.searchForm.status || contract.status == this.searchForm.status
      })
    }
  },
  mounted() {
    this.loadContracts()
  },
  methods: {
    async loadContracts() {
      try {
        // 加载合同数据
        const contractsResponse = await contractApi.getAll()
        
        // 处理合同数据，使用后端返回的关联信息
        this.contracts = contractsResponse.map(contract => {
          return {
            ...contract,
            roomNumber: contract.Room ? contract.Room.roomNumber : '',
            tenantName: contract.Tenant ? contract.Tenant.name : '',
            apartmentName: contract.Room && contract.Room.Apartment ? contract.Room.Apartment.name : '',
            regionName: contract.Room && contract.Room.Apartment && contract.Room.Apartment.Region ? contract.Room.Apartment.Region.name : ''
          }
        })
      } catch (error) {
        this.$message.error('加载数据失败')
      }
    },
    handleAdd() {
      this.$router.push('/contract/add')
    },
    handleEdit(id) {
      this.$router.push(`/contract/edit/${id}`)
    },
    async handleDelete(id) {
      this.$confirm('确定要删除这个合同吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          await contractApi.delete(id)
          this.$message.success('删除成功')
          this.loadContracts()
        } catch (error) {
          this.$message.error('删除失败')
        }
      }).catch(() => {
        // 取消删除
      })
    },
    handleSearch() {
      // 搜索逻辑
    },
    resetSearch() {
      this.searchForm = {
        status: ''
      }
    },
    handleCurrentChange(val) {
      this.currentPage = val
    }
  }
}
</script>

<style scoped>
.contract-list {
  padding: 20px 0;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  display: flex;
  justify-content: flex-end;
}
</style>