<template>
  <div class="expense-container">
    <el-card>
      <!-- 搜索栏 -->
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="支出编号">
          <el-input v-model="searchForm.expenseCode" placeholder="请输入支出编号" clearable />
        </el-form-item>
        <el-form-item label="支出类型">
          <el-tree-select
            v-model="searchForm.expenseTypeId"
            :data="expenseTypeOptions"
            :props="{ label: 'typeName', value: 'typeId', children: 'children' }"
            placeholder="请选择支出类型"
            clearable
          />
        </el-form-item>
        <el-form-item label="项目">
          <el-select v-model="searchForm.projectId" placeholder="请选择项目" clearable>
            <el-option
              v-for="project in projectOptions"
              :key="project.projectId"
              :label="project.projectName"
              :value="project.projectId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option label="待付款" value="pending" />
            <el-option label="已付款" value="paid" />
            <el-option label="已完成" value="completed" />
            <el-option label="已作废" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 操作栏 -->
      <el-row style="margin-bottom: 15px;">
        <el-button type="primary" @click="handleAdd">新增支出</el-button>
      </el-row>

      <!-- 表格 -->
      <el-table :data="tableData" border v-loading="loading">
        <el-table-column prop="expenseCode" label="支出编号" width="150" />
        <el-table-column prop="expenseTypeId" label="支出类型" width="120">
          <template #default="{ row }">
            {{ getExpenseTypeName(row.expenseTypeId) }}
          </template>
        </el-table-column>
        <el-table-column prop="expenseAmount" label="支出金额" width="120">
          <template #default="{ row }">
            ¥{{ row.expenseAmount }}
          </template>
        </el-table-column>
        <el-table-column prop="expenseDate" label="支出日期" width="120" />
        <el-table-column prop="expenseReason" label="支出事由" min-width="200" show-overflow-tooltip />
        <el-table-column prop="projectId" label="所属项目" width="150">
          <template #default="{ row }">
            {{ getProjectName(row.projectId) }}
          </template>
        </el-table-column>
        <el-table-column prop="paymentMethod" label="付款方式" width="100">
          <template #default="{ row }">
            {{ getPaymentMethodText(row.paymentMethod) }}
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag v-if="row.status === 'pending'" type="warning">待付款</el-tag>
            <el-tag v-else-if="row.status === 'paid'" type="primary">已付款</el-tag>
            <el-tag v-else-if="row.status === 'completed'" type="success">已完成</el-tag>
            <el-tag v-else type="info">已作废</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createdTime" label="创建时间" width="180" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="{ row }">
            <el-button type="primary" size="small" @click="handleEdit(row)">编辑</el-button>
            <el-button type="info" size="small" @click="handleUpdateStatus(row)">更新状态</el-button>
            <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        v-model:current-page="page.current"
        v-model:page-size="page.size"
        :total="page.total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="fetchData"
        @current-change="fetchData"
        style="margin-top: 20px; justify-content: flex-end;"
      />
    </el-card>

    <!-- 新增/编辑对话框 -->
    <el-dialog 
      v-model="dialogVisible" 
      :title="form.expenseId ? '编辑支出' : '新增支出'"
      width="700px"
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="支出编号" prop="expenseCode">
              <el-input v-model="form.expenseCode" placeholder="请输入支出编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支出类型" prop="expenseTypeId">
              <el-tree-select
                v-model="form.expenseTypeId"
                :data="expenseTypeOptions"
                :props="{ label: 'typeName', value: 'typeId', children: 'children' }"
                placeholder="请选择支出类型"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="支出金额" prop="expenseAmount">
              <el-input-number v-model="form.expenseAmount" :min="0" :precision="2" style="width: 100%;" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支出日期" prop="expenseDate">
              <el-date-picker 
                v-model="form.expenseDate" 
                type="date" 
                placeholder="选择日期"
                style="width: 100%;"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="支出事由" prop="expenseReason">
          <el-input 
            v-model="form.expenseReason" 
            type="textarea" 
            :rows="3" 
            placeholder="请输入支出事由" 
          />
        </el-form-item>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="所属项目" prop="projectId">
              <el-select v-model="form.projectId" placeholder="请选择项目" style="width: 100%;" clearable>
                <el-option
                  v-for="project in projectOptions"
                  :key="project.projectId"
                  :label="project.projectName"
                  :value="project.projectId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="付款方式" prop="paymentMethod">
              <el-select v-model="form.paymentMethod" placeholder="请选择付款方式" style="width: 100%;">
                <el-option label="转账" value="transfer" />
                <el-option label="现金" value="cash" />
                <el-option label="支票" value="check" />
                <el-option label="其他" value="other" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="付款账户" prop="paymentAccount">
          <el-input v-model="form.paymentAccount" placeholder="请输入付款账户" />
        </el-form-item>

        <el-form-item label="备注说明" prop="remark">
          <el-input 
            v-model="form.remark" 
            type="textarea" 
            :rows="2" 
            placeholder="请输入备注说明" 
          />
        </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>

    <!-- 更新状态对话框 -->
    <el-dialog v-model="statusDialogVisible" title="更新状态" width="400px">
      <el-form>
        <el-form-item label="状态">
          <el-select v-model="statusForm.status" placeholder="请选择状态" style="width: 100%;">
            <el-option label="待付款" value="pending" />
            <el-option label="已付款" value="paid" />
            <el-option label="已完成" value="completed" />
            <el-option label="已作废" value="cancelled" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="statusDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleSubmitStatus">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { 
  getExpenseList, 
  createExpense, 
  updateExpense, 
  deleteExpense,
  updateExpenseStatus
} from '../../api/expense'
import { getExpenseTypeTree } from '../../api/expenseType'
import { getProjectList } from '../../api/project'

// 搜索表单
const searchForm = reactive({
  expenseCode: '',
  expenseTypeId: null,
  projectId: null,
  status: ''
})

// 表格数据
const tableData = ref([])
const loading = ref(false)

// 分页
const page = reactive({
  current: 1,
  size: 10,
  total: 0
})

// 支出类型选项
const expenseTypeOptions = ref([])
// 项目选项
const projectOptions = ref([])

// 对话框
const dialogVisible = ref(false)
const formRef = ref(null)

// 表单数据
const form = reactive({
  expenseId: null,
  expenseCode: '',
  expenseTypeId: null,
  expenseAmount: 0,
  expenseDate: null,
  expenseReason: '',
  projectId: null,
  paymentMethod: 'transfer',
  paymentAccount: '',
  remark: ''
})

// 表单验证规则
const rules = {
  expenseCode: [
    { required: true, message: '请输入支出编号', trigger: 'blur' }
  ],
  expenseTypeId: [
    { required: true, message: '请选择支出类型', trigger: 'change' }
  ],
  expenseAmount: [
    { required: true, message: '请输入支出金额', trigger: 'blur' }
  ]
}

// 状态对话框
const statusDialogVisible = ref(false)
const statusForm = reactive({
  expenseId: null,
  status: ''
})

// 加载支出类型
const loadExpenseTypes = async () => {
  try {
    const res = await getExpenseTypeTree()
    expenseTypeOptions.value = res
  } catch (error) {
    console.error('加载支出类型失败:', error)
  }
}

// 加载项目列表
const loadProjects = async () => {
  try {
    const res = await getProjectList({ current: 1, size: 1000 })
    projectOptions.value = res.records || []
  } catch (error) {
    console.error('加载项目列表失败:', error)
  }
}

// 获取支出类型名称
const getExpenseTypeName = (typeId) => {
  const findType = (types) => {
    for (const type of types) {
      if (type.typeId === typeId) return type.typeName
      if (type.children) {
        const found = findType(type.children)
        if (found) return found
      }
    }
    return null
  }
  return findType(expenseTypeOptions.value) || '-'
}

// 获取项目名称
const getProjectName = (projectId) => {
  const project = projectOptions.value.find(p => p.projectId === projectId)
  return project ? project.projectName : '-'
}

// 获取付款方式文本
const getPaymentMethodText = (method) => {
  const map = {
    transfer: '转账',
    cash: '现金',
    check: '支票',
    other: '其他'
  }
  return map[method] || method
}

// 加载数据
const fetchData = async () => {
  loading.value = true
  try {
    const params = {
      current: page.current,
      size: page.size,
      expenseCode: searchForm.expenseCode || undefined,
      expenseTypeId: searchForm.expenseTypeId || undefined,
      projectId: searchForm.projectId || undefined,
      status: searchForm.status || undefined
    }
    const res = await getExpenseList(params)
    tableData.value = res.records
    page.total = res.total
  } catch (error) {
    console.error('加载数据失败:', error)
    ElMessage.error(error.message || '加载数据失败')
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  page.current = 1
  fetchData()
}

// 重置
const handleReset = () => {
  searchForm.expenseCode = ''
  searchForm.expenseTypeId = null
  searchForm.projectId = null
  searchForm.status = ''
  page.current = 1
  fetchData()
}

// 新增
const handleAdd = () => {
  Object.assign(form, {
    expenseId: null,
    expenseCode: '',
    expenseTypeId: null,
    expenseAmount: 0,
    expenseDate: null,
    expenseReason: '',
    projectId: null,
    paymentMethod: 'transfer',
    paymentAccount: '',
    remark: ''
  })
  dialogVisible.value = true
}

// 编辑
const handleEdit = (row) => {
  Object.assign(form, {
    expenseId: row.expenseId,
    expenseCode: row.expenseCode,
    expenseTypeId: row.expenseTypeId,
    expenseAmount: row.expenseAmount,
    expenseDate: row.expenseDate,
    expenseReason: row.expenseReason,
    projectId: row.projectId,
    paymentMethod: row.paymentMethod,
    paymentAccount: row.paymentAccount,
    remark: row.remark
  })
  dialogVisible.value = true
}

// 提交表单
const handleSubmit = async () => {
  if (!formRef.value) return
  
  try {
    await formRef.value.validate()
  } catch (error) {
    ElMessage.warning('请检查表单填写是否完整')
    return
  }
  
  try {
    if (form.expenseId) {
      await updateExpense(form.expenseId, form)
      ElMessage.success('更新成功')
    } else {
      await createExpense(form)
      ElMessage.success('创建成功')
    }
    dialogVisible.value = false
    await fetchData()
  } catch (error) {
    console.error('保存失败:', error)
    ElMessage.error(error.message || '操作失败')
  }
}

// 更新状态
const handleUpdateStatus = (row) => {
  statusForm.expenseId = row.expenseId
  statusForm.status = row.status
  statusDialogVisible.value = true
}

// 提交状态
const handleSubmitStatus = async () => {
  try {
    await updateExpenseStatus(statusForm.expenseId, statusForm.status)
    ElMessage.success('状态更新成功')
    statusDialogVisible.value = false
    await fetchData()
  } catch (error) {
    console.error('状态更新失败:', error)
    ElMessage.error(error.message || '状态更新失败')
  }
}

// 删除
const handleDelete = async (row) => {
  try {
    await ElMessageBox.confirm('确定要删除该支出吗？', '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    
    await deleteExpense(row.expenseId)
    ElMessage.success('删除成功')
    
    // 如果是最后一条且不是第一页，返回上一页
    if (tableData.value.length === 1 && page.current > 1) {
      page.current--
    }
    
    await fetchData()
  } catch (error) {
    if (error !== 'cancel') {
      console.error('删除失败:', error)
      ElMessage.error(error.message || '删除失败')
    }
  }
}

// 初始化
onMounted(() => {
  loadExpenseTypes()
  loadProjects()
  fetchData()
})
</script>

<style scoped>
.expense-container {
  padding: 20px;
}
</style>
