<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="资产编号" prop="assetNo">
        <el-input
          v-model="queryParams.assetNo"
          placeholder="请输入资产编号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产名称" prop="assetName">
        <el-input
          v-model="queryParams.assetName"
          placeholder="请输入资产名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产类别" prop="assetCategory">
        <el-input
          v-model="queryParams.assetCategory"
          placeholder="请输入资产类别"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:asset-depreciation:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:asset-depreciation:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @row-click="handleRowClick">
      <el-table-column label="资产编号" align="center" prop="assetNo" width="150" />
      <el-table-column label="资产名称" align="center" prop="assetName" width="180" />
      <el-table-column label="资产类别" align="center" prop="assetCategory" width="120" />
      <el-table-column label="原值" align="right" prop="originalValue" width="130">
        <template #default="scope">
          {{ formatMoney(scope.row.originalValue) }}
        </template>
      </el-table-column>
      <el-table-column label="使用年限" align="center" prop="usefulLife" width="100">
        <template #default="scope">
          {{ scope.row.usefulLife }} 年
        </template>
      </el-table-column>
      <el-table-column label="本月折旧" align="right" prop="monthlyDepreciation" width="120">
        <template #default="scope">
          {{ formatMoney(scope.row.monthlyDepreciation) }}
        </template>
      </el-table-column>
      <el-table-column label="当月费用" align="right" prop="monthlyExpense" >
        <template #default="scope">
          {{ formatMoney(scope.row.monthlyExpense) }}
        </template>
      </el-table-column>
      <el-table-column label="累计折旧" align="right" prop="accumulatedDepreciation">
        <template #default="scope">
          {{ formatMoney(scope.row.accumulatedDepreciation) }}
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
        sortable
      />
      <el-table-column label="操作" align="center" fixed="right" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:asset-depreciation:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:asset-depreciation:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <AssetDepreciationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { AssetDepreciationApi, AssetDepreciationVO } from '@/api/erp/finance-alone/assetDepreciation'
import AssetDepreciationForm from './AssetDepreciationForm.vue'

/** ERP 固定资产折旧 列表 */
defineOptions({ name: 'ErpAssetDepreciation' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<AssetDepreciationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  assetNo: undefined,
  assetName: undefined,
  assetCategory: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 格式化金额 */
const formatMoney = (value: number | undefined) => {
  if (value === undefined || value === null) {
    return '0.00'
  }
  return value.toFixed(2)
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await AssetDepreciationApi.getAssetDepreciationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await AssetDepreciationApi.deleteAssetDepreciation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 行点击操作 */
const handleRowClick = (row: AssetDepreciationVO, column: any, event: MouseEvent) => {
  // 检查是否点击了按钮、链接或其他交互元素
  const target = event.target as HTMLElement
  if (
    target.tagName === 'BUTTON' ||
    target.tagName === 'A' ||
    target.tagName === 'I' ||
    target.tagName === 'svg' ||
    target.closest('button') ||
    target.closest('a') ||
    target.closest('.el-button') ||
    target.closest('.el-checkbox')
  ) {
    return
  }
  openForm('update', row.id)
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await AssetDepreciationApi.exportAssetDepreciation(queryParams)
    download.excel(data, '固定资产折旧.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
