<template>
  <div class="purchase-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: 'Demo',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      activeName: 'pending',
      statusList: [],
      timeAxisParams: {
        businessKey: '',
        businessType: 'PurchContract'
      },
      search: {
        businessNo: '',
        status: '',
        fastSearch: ''
      },
      btnPermission: JSON.parse(sessionStorage.getItem('buttons')) || []
    }
  },
  computed: {
    ...mapGetters([
      'name'
    ])
  },
  mounted() {
    this.fetchData()
    this.getStatusList()
  },
  methods: {
    fetchData() {
      request({
        url: '/businessProcessingTask/doDifferenceList',
        params: {
          page: this.page,
          rows: this.size,
          simpleClassName: 'PurchContract',
          difference: this.activeName,
          ...this.search
        },
        method: 'get'
      }).then(({ data: { rows, total }}) => {
        this.tableData = rows || []
        this.total = total
      })
    },
    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 === 'timeAxis') {
        this.timeAxisClick(row)
        return
      }
      if (type === 'see') {
        this.$router.push({
          name: 'PurchaseContractDetail',
          params: { id: 'children', infoId: row.id, taskId: row.taskId },
          query: {
            infoId: row.id, taskId: row.taskId
          }
        })
        // this.$router.push({
        //   name: 'PurchaseContractReview',
        //   params: { id: 'children', infoId: row.id, taskId: row.taskId }
        // })
        return
      }
      if (type === 'downloadContract') {
        this.downPdf(row)
        return
      }
      if (type === 'downloadFile') {
        request({
          url: '/purchContract/downloadReportUrl',
          responseType: 'arraybuffer',
          params: {
            id: row.id
          },
          method: 'get'
        }).then(res => {
          exportFile(res)
        })
        return
      }
      if (row.taskName === '采购合同审核') {
        this.$router.push({
          name: 'PurchaseContractReview',
          params: { id: 'children', infoId: row.id, taskId: row.taskId },
          query: {
            infoId: row.id, taskId: row.taskId
          }
        })
      } else if (row.taskName === '采购合同终审' || row.taskName === '采购合同变更终审') {
        this.$router.push({
          name: 'PurchaseContractSignReview',
          params: { id: 'children', infoId: row.id, taskId: row.taskId, isChange: row.taskName == '采购合同变更终审' },
          query: {
            isChange: row.taskName == '采购合同变更终审',
            infoId: row.id, taskId: row.taskId
          }
        })
      } else if (row.taskName.includes('合同变更审核')) {
        this.$router.push({
          name: 'PurchaseContractChangeReview',
          params: { id: 'children', infoId: row.id, taskId: row.taskId },
          query: {
            infoId: row.id, taskId: row.taskId
          }
        })
      }
    },
    handleTabClick() {
      this.search.businessNo = ''
      this.search.status = ''
      this.search.fastsearch = ''
      this.page = 1
      this.fetchData()
    },
    timeAxisClick(row) {
      this.timeAxisParams.businessKey = row.id
      this.$refs.timeAxisDialog.show = true
      this.$nextTick(() => {
        this.$refs.timeAxis.fetchData()
      })
    },
    downPdf(row) {
      request({
        url: '/purchContract/downloadAllPurchFile',
        responseType: 'arraybuffer',
        params: {
          id: row.id
        },
        method: 'get'
      }).then(res => {
        exportFile(res)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.purchase-contract-management {

}
</style>
