<template>
  <div class="sales-contract-management">
    <router-view />
    <template v-if="!$route.params.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 v-if="activeName === 'all'">
                <div>
                  <el-input v-model="search.businessNo" style="width: 250px" placeholder="请输入业务编号" clearable />
                </div>
                <div>
                  <el-select
                    v-model="search.status"
                    placeholder="请选择业务状态"
                    clearable
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in statusList"
                      :key="item.code"
                      :label="item.name"
                      :value="item.code"
                    >
                    </el-option>
                  </el-select>
                </div>
                <div>
                  <el-input v-model="search.fastSearch" style="width: 300px" placeholder="请输入项目名称/主企业名称/核心企业名称" clearable></el-input>
                </div>
                <div class="leftBtn">
                  <el-button type="primary" @click="fetchData">查询</el-button>
                </div>
              </template>
            </div>
          </div>
          <cy-comm-table
            ref="commTable"
            v-loading="loading"
            :columns="columns"
            :table-options="getTableOptions"
            :data="tableData"
            :count="total"
          />
        </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 request from '@/utils/request'
import { exportFile } from '@/utils/index'

export default {
  name: 'SettlementManage',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      activeName: 'pending',
      statusList: [],

      search: {
        businessNo: '',
        status: '',
        fastSearch: ''
      },
      timeAxisParams: {
        businessKey: '',
        businessType: 'BillBasicInformation'
      },
      btnPermission: JSON.parse(sessionStorage.getItem('buttons')) || []
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  mounted() {
    this.fetchData()
    this.getStatusList()
  },
  methods: {
    fetchData() {
      request({
        url: this.activeName == 'pending' ? '/businessProcessingTask/doMyPendingList' : '/businessProcessingTask/doList',
        method: this.activeName == 'pending' ? 'post' : 'get',
        params: {
          page: this.page || 1,
          rows: this.size || 10,
          status: this.search.status,
          businessNo: this.search.businessNo,
          fastSearch: this.search.fastSearch,
          simpleClassName: 'BillBasicInformation'
        },
        data: {
          page: this.page || 1,
          rows: this.size || 10,
          status: this.search.status,
          businessNo: this.search.businessNo,
          fastSearch: this.search.fastSearch,
          simpleClassName: 'BillBasicInformation'
        }
      }).then(({ records, rows }) => {
        this.tableData = rows || []
        this.total = records
        this.loading = false
      })
    },
    getStatusList() {
      request({
        url: '/dictionary/do_findByParentCode',
        method: 'get',
        params: { parentCode: 'projectInitiationStatus' }
      }).then(({ data }) => {
        this.statusList = data || []
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    handlerOperate(type, row) {
      if (type == 'edit') {
        this.$router.push({
          name: 'SettlementEdit',
          params: { id: 'child', infoId: row.id, taskId: row.taskId, type: row.taskName },
          query: {
            infoId: row.id, taskId: row.taskId, type: row.taskName
          }
        })
      } else if (type == 'timeAxis') {
        this.timeAxisClick(row)
      } else if (type == 'downloadContract') {
        request({
          url: '/bill/downloadAllSettlementFile',
          responseType: 'arraybuffer',
          params: {
            id: row.id
          },
          method: 'get'
        }).then(res => {
          exportFile(res)
        })
      } else if (type == 'downloadFile') {
        request({
          url: '/bill/download/report',
          responseType: 'arraybuffer',
          params: {
            id: row.id
          },
          method: 'get'
        }).then(res => {
          exportFile(res)
        })
      } else {
        this.$router.push({
          name: 'SettlementView',
          params: { id: 'child', infoId: row.id, taskId: row.taskId, type: row.taskName },
          query: {
            infoId: row.id, taskId: row.taskId, type: row.taskName
          }
        })
      }
    },
    handleTabClick(tab) {
      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>
.sales-contract-management {

}
</style>
