<template>
  <div class="procurement-contract-management">
    <template>
      <div class="tempalte-content">
        <div class="search">
          <div class="left" />
          <div class="right">
            <div>
              <el-input v-model="search.businessNumber" style="width: 100%" placeholder="采购合同编号/业务编号" clearable />
            </div>
            <div>
              <el-select v-model="search.status" style="width: 100%" placeholder="请选择状态" clearable>
                <el-option
                  v-for="item in constant.projectInitiationStatus"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </div>
            <div>
              <el-input v-model="search.fastSearch" style="width: 100%" placeholder="项目名称/主企业名称/核心企业名称" clearable />
            </div>
            <div class="right-btn">
              <el-button type="primary" @click="fetchData">查询</el-button>
            <!-- </div>
            <div class="leftBtn"> -->
              <el-button type="primary" icon="el-icon-plus" @click="createContract">创建采购合同</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"
          />
        </div>
      </div>
    </template>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import request from '@/utils/request'
import indexTable from './indexTable'
import { exportFile } from '@/utils/index'
import { getPurchManagementList } from '@/api/myTrade/purchaseContractManagement/purchaseContractManagement'
export default {
  name: 'ProcurementContractManagement',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      statusList: [],
      tableData: [],
      page: 1,
      size: 10,
      total: 0,
      search: {
        businessNumber: '',
        status: '',
        fastSearch: ''
      }
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'constant'
    ])
  },
  created() {
    this.$store.dispatch('getConstant', ['projectInitiationStatus'])
  },
  mounted() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.loading = true
      const params = {
        ...this.search,
        page: this.page,
        rows: this.size
      }
      getPurchManagementList(params).then(({ data }) => {
        this.tableData = data.rows
        this.total = data.records
        this.loading = false
      })
    },
    // 创建采购合同
    createContract() {
      this.$router.push({
        name: 'PurchaseContractAdd',
        params: { id: 'child' }
      })
    },
    handlerOperate(type, row) {
      if(type == 'edit') {
        this.$router.push({
          name: !row.stage && row.status == '已完成' || row.stage == '采购合同变更录入' ? 'PurchaseContractChange' : row.stage && (row.stage.includes('采购合同签署') || row.stage.includes('采购合同变更签署')) ? 'PurchaseContractSign' : 'PurchaseContractAdd',
          params: { id: 'child', infoId: row.id, businessNumber: row.businessNumber, isChange: row.stage && row.stage.includes('采购合同变更签署') },
          query: {
            infoId: row.id,
            businessNumber: row.businessNumber,
            isChange: row.stage && row.stage.includes('采购合同变更签署')
          }
        })
      } else if(type == 'download') {
        request({
          url: '/purchManagement/downloadPurchFile',
          method: 'get',
          responseType: 'arraybuffer',
          params: { id: row.id }
        }).then(data => {
          exportFile(data)
        })
      } else {
        this.$router.push({
          name: 'PurchaseContractDetail',
          params: { id: 'child', infoId: row.id, businessNumber: row.businessNumber },
          query: {
            infoId: row.id,
            businessNumber: row.businessNumber
          }
        })
      }
    }
  }
}
</script>

  <style lang="sass" scoped>

  </style>
