<template>
  <div class="customer-disburse-info">
    <router-view />
    <template v-if="!$route.query.id">
      <el-tabs v-model="activeName" type="border-card" @tab-click="handleTabClick">
        <el-tab-pane label="待处理" name="pending"></el-tab-pane>
        <el-tab-pane label="全部" name="all"></el-tab-pane>
        <div>
          <div class="search">
            <div class="left"></div>
            <div class="right">
              <template>
                <div v-if="activeName === 'all'">
                  <el-date-picker
                    v-model="search.date"
                    type="daterange"
                    align="right"
                    unlink-panels
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                  >
                  </el-date-picker>
                </div>
                <div>
                  <el-input v-model="search.fastSearch" style="width: 250px" placeholder="请输入资方名称/客户名称" clearable />
                </div>
                <div class="leftBtn">
                  <el-button type="primary" @click="fetchData('search')">查询</el-button>
                </div>
              </template>
            </div>
          </div>
          <cy-comm-table
            ref="commTable"
            v-loading="loading"
            center
            :columns="columns"
            :table-options="getTableOptions"
            :data="tableData"
            :count="total"
            parent-component="CustomerDisburseInfoPage"
          />
        </div>
      </el-tabs>
    </template>
    <cy-dialog ref="timeAxisDialog" title="进度时间轴" top="15vh">
      <cy-time-axis ref="timeAxis" :time-axis-params="timeAxisParams"></cy-time-axis>
    </cy-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import indexTable from './indexTable'
import { getcustomerDisburseInfoList, customerDisburseInfoDataDownload } from '@/api/capitalManage/customerDisburseInfo/index'
import { exportFile } from '@/utils/index'

export default {
  name: 'CustomerDisburseInfoPage',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      activeName: 'pending',
      getStatusList: [],
      page: 1,
      size: 10,
      search: {
        date: '',
        fastSearch: '',
        difference: 'pending',
        simpleClassName: 'FinancialCustomerExpenditure'
      },
      deepSearch: {},
      timeAxisParams: {
        businessKey: '',
        businessType: 'FinancialCustomerExpenditure'
      },
      btnPermission: JSON.parse(sessionStorage.getItem('buttons')) || []
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  mounted() {
    if (!this.$route.params.id) {
      this.fetchData()
    }
  },
  methods: {
    fetchData(type) {
      if (type === 'search') {
        this.page = 1
        this.size = 10
        this.deepSearch = JSON.parse(JSON.stringify(this.search))
      }
      if (Object.keys(this.deepSearch).length !== 0) {
        this.search = JSON.parse(JSON.stringify(this.deepSearch))
      }
      const { date } = this.search
      this.loading = true
      const params = {
        ...this.search,
        startDate: date ? date[0] : '',
        endDate: date ? date[1] : '',
        page: this.page,
        rows: this.size
      }
      delete params.date
      getcustomerDisburseInfoList(params).then((data) => {
        const { rows, records } = data.data
        console.log('getCustomerDisburseInfoList', data)
        this.loading = false
        this.tableData = rows
        this.total = records
      })
    },
    handlerOperate(type, row) {
      if (type === 'timeAxis') {
        this.timeAxisClick(row)
      }

      if (type === 'edit') {
        this.$router.push({
          name: 'CustomerDisburseInfoEdit',
          query: {
            id: row.id,
            taskId: row.taskId
          }
        })
      }

      if (type === 'approval') {
        this.$router.push({
          name: 'CustomerDisburseInfoApproval',
          query: {
            businessKey: row.id,
            taskId: row.taskId,
            id: row.id
          }
        })
      }

      if (type === 'see') {
        this.$router.push({
          name: 'CustomerDisburseInfoSee',
          query: {
            businessKey: row.id,
            type,
            taskId: row.taskId,
            id: row.id
          }
        })
      }

      if (type === 'download') {
        customerDisburseInfoDataDownload({ id: row.id }).then((res) => {
          exportFile(res)
        })
      }

      if (type === 'contract') {
        this.$router.push({
          name: 'CustomerDisburseInfoContract',
          query: {
            businessKey: row.id,
            taskId: row.taskId,
            id: row.id
          }
        })
      }
    },
    handleTabClick(tab) {
      this.search.fastSearch = ''
      this.search.difference = tab.name
      this.activeName = tab.name
      this.fetchData()
    },
    timeAxisClick(row) {
      this.timeAxisParams.businessKey = row.id
      this.$refs.timeAxisDialog.show = true
      this.$nextTick(() => {
        this.$refs.timeAxis.fetchData()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.customer-disburse-info {

}
</style>
