<template>
  <div class="acceptance-management">
    <template>
      <div class="search">
        <div class="left" />
        <div class="right">
          <div>
            <el-input v-model="search.businessNumber" placeholder="请输入验收单编号/业务编号" clearable />
          </div>
          <div>
            <el-select v-model="search.status" placeholder="请选择状态">
              <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" placeholder="请输入项目名称/主企业名称/核心企业名称" clearable />
          </div>
          <div class="right-btn">
            <el-button type="primary" @click="fetchData">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="modifyClick('create')">创建验收单</el-button>
          </div>
        </div>
      </div>
      <cy-comm-table
        ref="commTable"
        v-loading="loading"
        :columns="columns"
        :table-options="tableOptions"
        :data="tableData"
        :count="total"
        parent-component="AcceptanceManagement"
      />
    </template>

    <!-- 申请融资 -->
    <cy-dialog ref="receiveDialog" title="申请融资">
      <p style="text-align: center;">确认申请验收单审核，完成后续融资流程么?</p>
      <div class="submit-btn">
        <el-button @click="$refs.receiveDialog.show = false">关闭</el-button>
        <el-button type="primary" @click="applySure">确定</el-button>
      </div>
    </cy-dialog>
  </div>
</template>

<script>
import { MessageBox } from 'element-ui'
import { mapGetters } from 'vuex'
import indexTable from './indexTable'
import { exportFile } from '@/utils/index'
import { getAcceptanceFormList, applyApproval, deleteAcceptance, downloadFinishedFiles, previewFinishedFiles } from '@/api/myTrade/acceptanceManagement/index'
export default {
  name: 'AcceptanceManagement',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      getStatusList: [],
      page: 1,
      size: 10,
      search: {
        businessNumber: '',
        status: '',
        fastSearch: ''
      },
      applyRow: {}
    }
  },
  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
      }
      getAcceptanceFormList(params).then(({ data }) => {
        this.tableData = data.rows
        this.total = data.records
        this.loading = false
      })
    },
    handlerOperate(type, row) {
      if (type === 'edit') { // 录入验收单   签署确认   暂存编辑
        if (row.stage === '录入验收单' || row.submitStatus === 0) {
          this.$router.push({
            name: 'AcceptanceFormAdd',
            params: { id: 'child' },
            query: {
              type: 'update',
              id: row.id,
              taskId: row.taskId
            }
          })
        } else if (row.stage === '签署确认') {
          this.$router.push({
            name: 'AcceptanceSign',
            params: { id: 'child' },
            query: {
              id: row.id,
              taskId: row.taskId
            }
          })
        }
      } else if (type === 'apply') {
        this.apply(row)
      }

      if (type === 'change') {
        this.$router.push({
          name: 'ChangePrice',
          params: { id: 'children' },
          query: {
            taskName: row.stage,
            taskId: row.taskId,
            id: row.id
          }
        })
      }

      if (type === 'see') {
        this.$router.push({
          name: 'AcceptanceFormView',
          params: { id: 'child' },
          query: {
            type: 'see',
            id: row.id,
            taskId: row.taskId
          }
        })
      }
      if (type === 'view') { // 添加验收单-不申请审核的查看
        this.$router.push({
          name: 'AcceptanceFormViewTemp',
          params: { id: 'child' },
          query: {
            type: 'see',
            id: row.id,
            taskId: row.taskId
          }
        })
      }

      // 预览-下载 签署附件
      if (type === 'preview') {
        previewFinishedFiles(row.id).then((res) => {
          res.data.map(item => {
            window.open(`#/perview?fileId=${item.id}&name=${item.originalName}&absolutePath=${item.absolutePath}`)
          })
        })
      }
      if (type === 'download') {
        downloadFinishedFiles(row.id).then((data) => {
          exportFile(data)
        })
        return
      }

      if (type === 'delete') {
        MessageBox.confirm('确定删除此验收单？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteAcceptance({ id: row.id }).then(res => {
            if (res.success) {
              this.$message.success('删除成功')
              // this.page = 1
              this.fetchData()
            }
          })
        }).catch(() => {

        })
      }
    },
    modifyClick() {
      this.$router.push({
        name: 'AcceptanceFormAdd',
        params: { id: 'child' }

      })
    },

    // 申请
    apply(row) {
      this.applyRow = row
      this.$refs.receiveDialog.show = true
    },

    // 确认申请
    applySure() {
      const { id, taskId } = this.applyRow
      applyApproval({ Loading: true, id, taskId }).then(res => {
        if (res.success) {
          this.$message.success('申请成功')
          this.$refs.receiveDialog.show = false
          this.fetchData()
        } else {
          this.$message.warning(res.msg)
          this.$refs.receiveDialog.show = false
        }
      })
    }

  }
}
</script>

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

}
</style>
