<template>
  <div class="detail-container">
    <!-- <div class="top-container">
      <el-radio-group v-model="currentType" size="medium" @change="changeType()">
        <el-radio-button v-for="(item, index) in typeList" :key="index" :label="item.value">{{item.label}}</el-radio-button>
      </el-radio-group>
    </div> -->

    <div id="printMe">
      <template v-for="(item,i) in detailArr">
        <PrintCommon v-if="currentType === 0" :key="i" :detail-data="item" :company="company" />
        <PrintFoshan v-if="currentType === 1" :key="i" :detail-data="item" :company="company" />
        <PrintGuangzhou v-if="currentType === 2" :key="i" :detail-data="item" :company="company" />
        <PrintShaoguan v-if="currentType === 3" :key="i" :detail-data="item" :company="company" />
      </template>
    </div>

    <div class="page-footer">
      <div class="footer">
        <el-button v-print="printObj" type="primary" icon="el-icon-printer">打 印</el-button>
        <el-button @click="goBack">关 闭</el-button>
      </div>
    </div>

  </div>
</template>

<script>
import print from 'vue-print-nb'
import { getDeliverDetail } from '@/api/supply/deliver'
import { addPrints } from '@/api/supply/pickup'
import { getCompanyList } from '@/api/user'
import PrintCommon from '@/components/Common/print-common'
import PrintFoshan from '@/components/Common/print-foshan'
import PrintGuangzhou from '@/components/Common/print-guangzhou'
import PrintShaoguan from '@/components/Common/print-shaoguan'

export default {
  name: 'ReturnDetail',
  componentName: 'ReturnDetail',
  components: {
    PrintFoshan,
    PrintGuangzhou,
    PrintShaoguan,
    PrintCommon
  },
  directives: {
    print
  },
  props: ['listItem'],
  data() {
    return {
      currentType: 0,
      typeList: [
        { label: '通用', value: 0 },
        { label: '佛山', value: 1 },
        { label: '广州', value: 2 },
        { label: '韶关', value: 3 }
      ],
      printObj: {
        id: 'printMe',
        closeCallback: () => {
          this.addPrint()
        }
      },
      detailData: {},
      company: '',
      detailArr: []
    }
  },

  created() {
    console.log(this.listItem,'454')
    this.listItem.map(k => {
      this.getDetail(k.id)
    })

    this.getCompanyList()
  },

  methods: {
    // 返回列表
    goBack() {
      this.$emit('backListFormDetail')
    },
    changeType() {

    },

    // 获取详情
    getDetail(id) {
      getDeliverDetail({ id: id }).then(res => {
        this.detailArr.push(res.data)

      })
    },

    getCompanyList() {
      getCompanyList().then(res => {
        this.company = res.data ? res.data[0].companyName : ''
      })
    },

    // 添加次数
    addPrint() {
      const ids = this.detailArr.map(item => {
            if (item.invoicePickBeans && item.invoicePickBeans.length) {
              for (let index = 0; index < item.invoicePickBeans.length; index++) {
                  const element = item.invoicePickBeans[index];
                return  item.invoiceOrderId || element.invoiceOrderId || element.id
              }
            }else{
                return item.invoiceOrderId || item.id
            }

      })

      // console.log(document.execCommand('print'),'4545');
      addPrints({ ids: ids.join(',') }).then(res => {
        // this.$successMsg('提交成功');
         this.$parent.getList()
      })
    }
  }
}
</script>

<style>
 body{
    height: auto !important;
  }
</style>

<style scoped lang="scss">

@media print {

    .detail-container {
    width: 100%;
    height: auto !important;
    margin-bottom: 50px;
    font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
    // font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
    color: #000 !important;
  }
  .top-container {
    margin: 20px;
  }

  }

  .detail-container {
    width: 100%;
    height: auto !important;
    margin-bottom: 50px;
    color: #000 !important;
  }
  .top-container {
    margin-bottom: 20px;
  }

</style>
