<template>
  <div class="settlement-management">
    <template v-if="$route.params.id != 'child'">
      <div class="tempalte-content">
        <div class="search">
          <div class="left" />
          <div class="right">
            <div>
              <el-input v-model="search.businessNo" style="width: 100%" placeholder="结算单编号/业务编号" clearable />
            </div>
            <div>
              <el-select v-model="search.status" style="width: 100%" placeholder="请选择状态" clearable>
                <el-option
                  v-for="item in statusList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </div>
            <div>
              <el-input v-model="search.customerName" style="width: 100%" placeholder="项目名称/主企业名称/核心企业名称" clearable />
            </div>
            <div class="right-btn">
              <el-button type="primary" @click="fetchData('search')">查询</el-button>
            <!-- </div>
            <div class="leftBtn"> -->
              <el-button type="primary" icon="el-icon-plus" @click="createSettle">新建结算单</el-button>
            </div>
          </div>
        </div>
        <div class="table-box">
          <cy-comm-table
            ref="commTable"
            v-loading="loading"
            :columns="columns"
            :table-options="tableOptions"
            :data="tableData"
            :count="total"
            parentComponent="SettlementDocumentManagement"
          />
        </div>
      </div>
    </template>
    
    <router-view />
  </div>
</template>

<script>
import indexTable from './indexTable'
import request from '@/utils/request'
import { getStatus } from '@/api/myTrade/biddingManagement/biddingManagement'
import { exportFile } from '@/utils/index'

export default {
  name: 'SettlementDocumentManagement',
  mixins: [indexTable],
  data() {
    return {
      search: {
        businessNo: '',
        status: '',
        customerName: ''
      },
      loading: false,
      statusList: []
    }
  },
  mounted() {
    this.fetchData()
    this.getStatusList()
  },
  methods: {
    createSettle() {
      this.$router.push({
        // name: 'AddSettlement',
        name: 'AddSettlement',
        params: { id: 'child' }
      })
    },
    fetchData() {
      this.loading = true
      request({
        url: '/bill/findAllList',
        method: 'get',
        params: {
          page: this.page || 1,
          rows: this.size || 10,
          status: this.search.status,
          businessNo: this.search.businessNo,
          customerName: this.search.customerName
        }
      }).then(({ data }) => {
        this.tableData = data.rows || []
        this.total = data.records
        this.loading = false
      })
    },
    handlerOperate(type, info) {
      if(type == 'edit') {
        this.$router.push({
          name: !info.currentStage ? 'AddSettlement' : info.currentStage.includes('签署') ? 'SignSettlement' : info.currentStage.includes('完成') ? 'EditSettlement' : 'AddSettlement',
          params: { id: 'child', taskId: info.taskId, key: info.id },
          query: {
            taskId: info.taskId, key: info.id
          }
        })
      } else if(type == 'see') {
        this.$router.push({
          name: 'SettlementDetail',
          params: { id: 'child', taskId: info.taskId, key: info.id },
          query: {
            taskId: info.taskId, key: info.id
          }
        })
      } else if(type == 'changePrice') {
        this.$router.push({
          name: 'EditSettlement',
          params: { id: 'child', taskId: info.taskId, key: info.id },
          query: {
            taskId: info.taskId, key: info.id
          }
        })
      } else if(type == 'download') {
        request({
          url: '/bill/downloadPdfSign',
          method: 'get',
          responseType: 'arraybuffer',
          params: { settlementBasicInfoId: info.id }
        }).then(data => {
          exportFile(data)
        })
      }
    },
    getStatusList() {
      getStatus({ parentCode: 'projectInitiationStatus' }).then(response => {
        this.statusList = response.data
      })
    }
  }
}
</script>

<style lang="scss">

</style>