<template>
  <div class="contract-create-fee-container">
    <!-- 查询条件 -->
    <el-card class="search-wrapper">
      <div slot="header" class="flex justify-between">
        <span>{{ $t('contractCreateFee.search.title') }}</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="6">
          <el-input v-model.trim="searchForm.contractCode" :placeholder="$t('contractCreateFee.search.contractCode')"
            clearable />
        </el-col>
        <el-col :span="6">
          <el-input v-model.trim="searchForm.contractNameLike"
            :placeholder="$t('contractCreateFee.search.contractNameLike')" clearable />
        </el-col>
        <el-col :span="6">
          <el-input v-model.trim="searchForm.contractType" :placeholder="$t('contractCreateFee.search.contractType')"
            clearable />
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="handleSearch">
            <i class="el-icon-search"></i>
            {{ $t('common.search') }}
          </el-button>
          <el-button @click="handleReset">
            <i class="el-icon-refresh"></i>
            {{ $t('common.reset') }}
          </el-button>
        </el-col>
      </el-row>
    </el-card>

    <!-- 合同信息 -->
    <el-card class="list-wrapper">
      <div slot="header" class="flex justify-between">
        <span>{{ $t('contractCreateFee.list.title') }}</span>
        <el-button type="primary" size="small" @click="handleBatchCreate">
          <i class="el-icon-plus"></i>
          {{ $t('contractCreateFee.list.batchCreate') }}
        </el-button>
      </div>

      <el-table v-loading="loading" :data="tableData" border style="width: 100%">
        <el-table-column prop="contractCode" :label="$t('contractCreateFee.table.contractCode')" align="center" />
        <el-table-column prop="parentContractCode" :label="$t('contractCreateFee.table.parentContractCode')"
          align="center">
          <template slot-scope="scope">
            {{ scope.row.parentContractCode || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="contractName" :label="$t('contractCreateFee.table.contractName')" align="center" />
        <el-table-column prop="contractTypeName" :label="$t('contractCreateFee.table.contractType')" align="center" />
        <el-table-column prop="bContacts" :label="$t('contractCreateFee.table.partyB')" align="center" />
        <el-table-column prop="amount" :label="$t('contractCreateFee.table.amount')" align="center" />
        <el-table-column prop="startTime" :label="$t('contractCreateFee.table.startTime')" align="center" />
        <el-table-column prop="endTime" :label="$t('contractCreateFee.table.endTime')" align="center" />
        <el-table-column :label="$t('common.operation')" align="center" width="220">
          <template slot-scope="scope">
            <el-button v-if="scope.row.state !== '2002'" size="mini" @click="handlePayFee(scope.row)">
              {{ $t('contractCreateFee.table.payFee') }}
            </el-button>
            <!-- <el-button size="mini" @click="handleViewDetail(scope.row)">
              {{ $t('contractCreateFee.table.viewDetail') }}
            </el-button> -->
            <el-button size="mini" @click="handleViewFee(scope.row)">
              {{ $t('contractCreateFee.table.viewFee') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination :current-page.sync="pagination.current" :page-sizes="[10, 20, 30, 50]" :page-size="pagination.size"
        :total="pagination.total" layout="total, sizes, prev, pager, next, jumper" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-card>

    <!-- 创建费用弹窗 -->
    <contract-create-fee-add ref="createFeeAdd" @success="handleSuccess" />
  </div>
</template>

<script>
import { queryContract } from '@/api/fee/contractCreateFeeApi'
import ContractCreateFeeAdd from '@/components/fee/contractCreateFeeAdd'
import { getCommunityId } from '@/api/community/communityApi'

export default {
  name: 'ContractCreateFeeList',
  components: {
    ContractCreateFeeAdd
  },
  data() {
    return {
      loading: false,
      searchForm: {
        contractCode: '',
        contractNameLike: '',
        contractType: ''
      },
      tableData: [],
      pagination: {
        current: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      try {
        this.loading = true
        const params = {
          page: this.pagination.current,
          row: this.pagination.size,
          ...this.searchForm,
          communityId: getCommunityId()
        }
        const { data, total } = await queryContract(params)
        this.tableData = data
        this.pagination.total = total
      } catch (error) {
        this.$message.error(this.$t('contractCreateFee.fetchError'))
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.pagination.current = 1
      this.getList()
    },
    handleReset() {
      this.searchForm = {
        contractCode: '',
        contractNameLike: '',
        contractType: ''
      }
      this.handleSearch()
    },
    handleSizeChange(val) {
      this.pagination.size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pagination.current = val
      this.getList()
    },
    handleBatchCreate() {
      this.$refs.createFeeAdd.open(null, true)
    },
    handlePayFee(row) {
      this.$router.push({
        path: '/views/fee/owePayFeeOrder',
        query: {
          payObjId: row.contractId,
          payObjType: '7777',
          contractName: row.contractName
        }
      })
    },
    handleViewDetail(row) {
      this.$router.push({
        path: '/views/contract/contractDetail',
        query: { contractId: row.contractId }
      })
    },
    handleViewFee(row) {
      this.$router.push({
        path: '/views/contract/contractDetail',
        query: {
          contractId: row.contractId,
          contractCode: row.contractCode,
          currentTab:'contractDetailFee'
        }
      })
    },
    handleSuccess() {
      this.getList()
    }
  }
}
</script>

<style lang="scss" scoped>
.contract-create-fee-container {
  padding: 20px;

  .search-wrapper {
    margin-bottom: 20px;

    .el-row {
      margin-bottom: -20px;
    }

    .el-col {
      margin-bottom: 20px;
    }
  }

  .list-wrapper {
    .el-pagination {
      margin-top: 20px;
      text-align: right;
    }
  }
}
</style>