<!--
@name: 首营质检报告打印
@author: 瞿涛
@time: 2024-04-22
-->
<template>
  <p-spin :spinning="spinning">
    <p-card title="首营质检报告打印">
      <div slot='extra'>
        <p-button icon='filter' @click='toggleFormTag = !toggleFormTag'>{{ toggleFormTag ? '隐藏' : '显示' }}</p-button>
        <p-button class='ml-10' type='primary' @click="onReset">重置</p-button>
        <p-button class='ml-10' type='primary' @click='onSubmit'>查询</p-button>
        <p-button class='ml-10' type='primary' @click='onPrint'>打印</p-button>
      </div>
      <p-tabs v-model='activeKey' @change="changeKey">
        <p-tab-pane key="1" :forceRender="false" tab="首营">
          <tab1 ref="tab1" :toggleFormTag="toggleFormTag"></tab1>
        </p-tab-pane>
        <p-tab-pane key="2" :forceRender="false" tab="质检报告">
          <tab2 ref="tab2" :toggleFormTag="toggleFormTag"></tab2>
        </p-tab-pane>
      </p-tabs>
    </p-card>
  </p-spin>
</template>

<script>
import { print1, print2 } from './api'
import tab1 from './modules/tab1.vue'
import tab2 from './modules/tab2.vue'
export default {
  components:{ tab1, tab2 },
  data() {
    return {
      spinning: false,
      toggleFormTag: true,
      activeKey:'1',
    }
  },
  methods: {
    onPrint() {
      console.log(this.$refs.tab1.selectedRow,'row');
      if (this.activeKey === '1') {
        const list1 = this.$refs.tab1.selectedRow
        if (list1.length === 0) return this.$message.warn('请选择打印数据！')
        const flag = list1.every(item => item.upFlag === '已上传')
        if(!flag) return this.$message.warn('请先上传首营资料再打印')
        this.spinning = true
        print1(list1).then(res => {
          this.spinning = false
          if (res.success) {
            this.$message.success(res.message)
            this.onSubmit()
          } else {
            this.$message.error(res.message)
          }
        })
      }
      if (this.activeKey === '2') {
        const list2 = this.$refs.tab2.selectedRow
        if (list2.length === 0) return this.$message.warn('请选择打印数据！')
        const flag = list2.every(item => item.upFlag === '已上传')
        if(!flag) return this.$message.warn('请先上传质检报告再打印')
        this.spinning = true
        print2(list2).then(res => {
          this.spinning = false
          if (res.success) {
            this.$message.success(res.message)
            this.onSubmit()
          } else {
            this.$message.error(res.message)
          }
        })
      }
    },
    onReset() {
      console.log(this.$refs.tab1,'this.$refs.tab1');
      if (this.activeKey === '1') {
        this.$refs.tab1.form = {}
        this.$set(this.$refs.tab1.form,'startDate',this.$moment().format('YYYY-MM-DD 00:00:00'))
      }
      if (this.activeKey === '2') {
        this.$refs.tab2.form = {}
        this.$set(this.$refs.tab2.form,'startDate',this.$moment().format('YYYY-MM-DD 23:59:59'))
      }
      
    },
    onSubmit() {
      if (this.activeKey === '1') {
        this.$refs.tab1.listParams = {
          num: "syzlbill",
          params: {
            ...this.$refs.tab1.form
          }
        }
      }
      if (this.activeKey === '2') {
        this.$refs.tab2.listParams = {
          num: "zjbgbill",
          params: {
            ...this.$refs.tab2.form
          }
        }
      }
    },
    changeKey(e) {
      console.log(e,'e');
      this.activeKey = e
    },
  }
}
</script>

<style>

</style>