<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 constant.projectInitiationStatus"
                      :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('search')">查询</el-button>
                </div>
              </template>
            </div>
          </div>
          <cy-comm-table
            ref="commTable"
            v-loading="loading"
            :columns="columns"
            :table-options="getTableOptions"
            :data="tableData"
            :count="total"
            parent-component="SalesContractManagement"
          />
        </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 { getList } from '@/api/dictionary'
import request from '@/utils/request'
import { exportFile } from '@/utils/index'
export default {
  name: 'SalesContractManagement',
  mixins: [indexTable],
  data() {
    return {
      loading: false,
      activeName: 'pending',
      getStatusList: [],
      page: 1,
      size: 10,
      search: {
        businessNo: '',
        status: '',
        fastSearch: '',
        difference: 'pending',
        simpleClassName: 'SalesContract'
      },
      timeAxisParams: {
        businessKey: '',
        businessType: 'SalesContract'
      },
      btnPermission: JSON.parse(sessionStorage.getItem('buttons')) || []
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'constant'
    ])
  },
  created() {
    this.$store.dispatch('getConstant', ['projectInitiationStatus'])
  },
  mounted() {
    if (!this.$route.params.id) {
      this.fetchData()
    }
  },
  methods: {
    fetchData(type) {
      // if (type === 'search') {
      //   this.deepSearch = JSON.parse(JSON.stringify(this.search))
      // }
      // if (Object.keys(this.deepSearch).length !== 0) {
      //   this.search = JSON.parse(JSON.stringify(this.deepSearch))
      // }
      this.loading = true
      const params = {
        ...this.search,
        page: this.page,
        rows: this.size
      }
      getList(params).then((data) => {
        const { rows, records } = data.data
        console.log(data)
        this.loading = false
        this.tableData = rows
        this.total = records
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          alert('submit!')
        } else {
          return false
        }
      })
    },
    handlerOperate(type, row) {
      if (type === 'edit') {
        if (row.taskName === '模板初审' || row.taskName === '模板复审' || row.taskName === '变更初审' || row.taskName === '变更复审') {
          this.$router.push({
            name: 'SalesContractManagementPrequalification',
            params: { id: 'children' },
            query: {
              taskName: row.taskName,
              id: row.id,
              taskId: row.taskId,
              type
            }
          })
        } else if (row.taskName === '信息审核') {
          this.$router.push({
            name: 'SalesContractManagementSalesContractReview',
            params: { id: 'children' },
            query: {
              taskName: row.taskName,
              id: row.id,
              taskId: row.taskId,
              type
            }
          })
        } else if (row.taskName === '签署审核' || row.taskName === '变更签署审核') {
          this.$router.push({
            name: 'SalesContractManagementSignReview',
            params: { id: 'children' },
            query: {
              taskName: row.taskName,
              id: row.id,
              taskId: row.taskId,
              type
            }
          })
        } else if (row.taskName === '变更审核') {
          this.$router.push({
            name: 'SalesContractManagementChangeContractReview',
            params: { id: 'children' },
            query: {
              taskName: row.taskName,
              id: row.id,
              taskId: row.taskId,
              type
            }
          })
        }
      }

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

      if (type === 'timeAxis') {
        this.timeAxisClick(row)
        return
      }

      if (type === 'downloadContract') {
        request({
          url: '/salesContract/downloadAllSalesFile',
          responseType: 'arraybuffer',
          params: {
            id: row.id
          },
          method: 'get'
        }).then(res => {
          exportFile(res)
        })
        return
      }

      if (type === 'downloadFile') {
        request({
          url: '/salesContract/downloadSalesReport',
          responseType: 'arraybuffer',
          params: {
            id: row.id
          },
          method: 'get'
        }).then(res => {
          exportFile(res)
        })
      }
    },
    handleTabClick(tab) {
      this.search.businessNo = ''
      this.search.status = ''
      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>
.sales-contract-management {

}
</style>
