<template>
  <div style="width: 30vw; height: 50vh">
    <el-input v-model="input" />
    <el-button
      @click="handlePrint1"
    >按工序打印
    </el-button>
    <el-button
      @click="handlePrint2"
    >按班组打印
    </el-button>
  </div>
</template>

<script>
export default {
  name: 'Gdcjstj',
}
// 2工单超节损统计(按工序)
import service from '@/utils/request'
const getOrderSuperLossCount = (params) => {
  return service({
    url: '/mes_server/order_super_loss/getOrderSuperLossCount',
    method: 'get',
    params
  })
}

</script>

<script setup>
import { ref, defineProps } from 'vue'
import { hiprint } from 'vue-plugin-hiprint'
import { ElMessage } from 'element-plus'
import template1 from './template-gx.json'
import template2 from './template-bz.json'

// const props = defineProps(['gdbh'])
const props = defineProps({
  gdbh: {
    type: String,
    default: '',
  }
})
const input = ref(props['gdbh'])

const handlePrint1 = async() => {
  // 引入后初始化
  hiprint.init()
  const template = JSON.parse(JSON.stringify(template1))
  const hiprintTemplate = new hiprint.PrintTemplate({ template })
  const params = {
    order: input.value,
    type: '1',
  }
  const res = await getOrderSuperLossCount(params)
  if (res['code'] === 0) {
    console.log(res['data'])
    const { data } = res
    if (!data) {
      ElMessage({
        type: 'info',
        message: res['msg']
      })
      return
    }
    const tableData = {
      'gdbh': data['Gd_gdbh'],
      'cpdh': data['成品代号'],
      'cpmc': data['成品名称'],
      'xsddh': data['销售订单号'],
      'ddsl': `${parseFloat(data['订单数量']).toFixed(4)} 万张`,
      'sjtl': `${parseFloat(data['实际投料']).toFixed(4)} 万张`,
      'rcrq': data['warehousing_date'],
      'rchj': data['warehousing_num'],
      'clf': data['material_waste'],
      'wff': data['waste_out'],
      'ltcl': data['minor_processing'],
      'zcf': data['zcfp'],
      'zjf': data['waste_quality'],
      'wxs': data['intangible_loss'],
      'mbhgl': data['target_rate'],
      'sjhgl': data['real_rate'],
      'total1': data['total']['plan_loss'],
      'total2': data['total']['total_fp'],
      'total3': data['total']['waste_quality'],
      'total4': data['total']['waste_intangible'],
      'total5': data['total']['total_waste'],
      'total6': data['total']['loss'],
      // 'total7': data['total'][''],
      'table': data['gy_data'].map(item => ({
        'gxmc': `${item.Gy0_yjno}${item.Gy0_gxh === '' ? '' : '-' + item.Gy0_gxh}${item.Gy0_gxmc === '' ? '' : '-' + item.Gy0_gxmc}${item.Add_gxmc === '' ? '' : '-' + item.Add_gxmc}`.substring(0, 20),
        'jhcl': item['Gy0_计划接货数'],
        'jhsh': item['Gy0_计划损耗'],
        'zcf': item['total_fp'],
        'zjf': item['waste_quality'],
        'wxs': item['intangible_loss'],
        'bfhj': item['total_waste'],
        'jss': item['loss'],
        'tlbfl': item['loss_rate'],
        'mbbfl': item['target_loss_rate'],
        'sjbfl': item['actual_loss_rate'],
        'cdbl': item['超节损承担比例'],
        'cjsje': item[''],
        'zgqr': item[''],
      })),
    }
    console.log(tableData)
    hiprintTemplate.print(tableData)
  }
}

const handlePrint2 = async() => {
  // 引入后初始化
  hiprint.init()
  const template = JSON.parse(JSON.stringify(template2))
  const hiprintTemplate = new hiprint.PrintTemplate({ template })
  const params = {
    order: input.value,
    type: '2',
  }
  const res = await getOrderSuperLossCount(params)
  if (res['code'] === 0) {
    console.log(res['data'])
    const { data } = res
    if (!data) {
      ElMessage({
        type: 'info',
        message: res['msg']
      })
      return
    }
    const tableData = {
      'gdbh': data['Gd_gdbh'],
      'cpmc': data['成品名称'],
      'sjtl': parseFloat(data['实际投料']).toFixed(2) + '万张',
      'rcs': data['warehousing_num'],
      'wg': data['warehousing_date'],
      'clf': data['材料废'],
      'wff': data['外发废'],
      'ltcl': data['零头处理'],
      'zcf': data[''],
      'zjf': data['工单质检废'],
      'wxs': data[''],
      'mbhgl': data['target_rate'],
      'sjhgl': data['real_rate'],
      'plan_loss': data['total']['plan_loss'],
      'total_fp': data['total']['total_fp'],
      'waste_quality': data['total']['waste_quality'],
      'waste_intangible': data['total']['waste_intangible'],
      'total_jsj': '',
      'table': data['gy_data'].map(item => ({
        'bzcy': item['member'],
        'yjgx': item['sczl_gxmc'].substring(0, 20),
        'cl': item['total_cl'],
        'jhsh': item['计划损耗'],
        'zcfp': item['sczl_zcfp'],
        'zjf': item['waste_quality'],
        'wxsft': item['intangible_loss'],
        'jssl': item['loss'],
        'cdbl': item['超节损承担比例'],
        'jsj': `<span style="color: red;">${item['loss'] ? item['loss'] : ''}</span>`,
      })),
    }
    console.log(tableData)
    hiprintTemplate.print(tableData)
  }
}
</script>

<style scoped>

</style>
