<template>
  <div class="mod-config">
    <el-form :inline="true"  label-position="top" label-width="100px" >
      <el-form-item :label="'工厂编码：'">
        <el-input v-model="searchData.site" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'日计划单号：'">
        <el-input v-model="searchData.orderNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'订单号：'">
        <el-input v-model="searchData.orderRef1" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'物料编码 ：'">
        <el-input v-model="searchData.partNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'混炼任务单 ：'">
        <el-input v-model="searchData.taskNo" style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'计划日期：'">
        <el-date-picker
          style="width: 130px"
          v-model="searchData.startDate1"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item style="margin-top: 23px;">
        <laber style="margin-left: -9px;font-size: 19px">&#10142</laber>
      </el-form-item>
      <el-form-item :label="' '">
        <el-date-picker
          style="width: 130px"
          v-model="searchData.endDate1"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="' '">
        <el-button @click="search()" style="margin-left: 0px;margin-top: 0px" type="primary">查询</el-button>
        <!-- rqrq - 与 cancelSfdc 一致：columnList + fields() 动态导出 -->
        <download-excel
          :fields="fields()"
          type="xlsx"
          :name="exportName"
          :fetch="createExportData"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          导出
        </download-excel>
      </el-form-item>
    </el-form>
    <el-table
      :height="height"
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        v-for="(item,index) in columnList"
        :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        header-align="center"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed==''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="item.slotKey === 'shiftQty'" v-show="!item.columnHidden">
            {{ shiftQtyFmt(scope.row) }}
          </span>
          <span v-else-if="!item.columnHidden && !item.columnImage">
            {{ scope.row[item.columnProp] }}
          </span>
          <span v-if="item.columnImage">
            <img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/>
          </span>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
/**
 * rqrq - 列配置常量：增删列、改中文名仅此维护；表格与导出共用
 */
const DAILY_PLAN_COLUMN_SPECS = [
  { prop: 'site', label: '工厂编码', width: 60, align: 'left', tooltip: true },
  { prop: 'orderNo', label: '日计划单号', width: 140, align: 'left', tooltip: true },
  { prop: 'orderDate', label: '计划日期', width: 70, align: 'left' },
  { prop: 'orderRef1', label: '订单号', width: 90, align: 'left', tooltip: true },
  { prop: 'partNo', label: '物料编码', width: 70, align: 'left', tooltip: true },
  { prop: 'partDescription', label: '物料名称', width: 200, align: 'left', tooltip: true },
  { prop: 'orderQty', label: '计划数量', width: 60, align: 'right' },
  { prop: 'partPlanQty', label: '班产量', width: 60, align: 'right' },
  { prop: 'shiftQty', label: '计划班数', width: 60, align: 'right', slotKey: 'shiftQty' },
  { prop: 'weight', label: '参考数量', width: 60, align: 'right' },
  { prop: 'qtyfinished', label: '混炼胶数', width: 70, align: 'right' },
  { prop: 'taskNo', label: '混炼任务单', width: 120, align: 'left', tooltip: true },
  { prop: 'userName', label: '录入人', width: 50, align: 'left' },
  { prop: 'enterDate', label: '录入时间', width: 100, align: 'left', tooltip: true },
  { prop: 'reportTime', label: '报工时间', width: 100, align: 'left', tooltip: true }
]

import {
  getDailyPlanData
} from '@/api/production.js'

export default {
  name: 'stockPrint',
  computed: {
    // rqrq - 结构与 cancelSfdcForWareHouse.columnList 一致
    columnList () {
      const vm = this
      return DAILY_PLAN_COLUMN_SPECS.map(spec => ({
        tableId: 'dailyPlanSearchList',
        tableName: vm.$route.meta.title,
        columnProp: spec.prop,
        columnLabel: spec.label,
        columnHidden: false,
        columnImage: false,
        columnSortable: false,
        columnWidth: spec.width,
        format: null,
        functionId: vm.$route.meta.menuId,
        sortLv: 0,
        status: true,
        fixed: '',
        serialNumber: null,
        columnType: null,
        align: spec.align || 'left',
        showOverflowTooltip: !!spec.tooltip,
        slotKey: spec.slotKey || ''
      }))
    }
  },
  data () {
    return {
      exportName: '日计划列表' + this.getStrDate(),
      height: 200,
      tableData: [],
      date1: '',
      searchData: {
        orderNo: '',
        orderRef1: '',
        partNo: '',
        startDate1: new Date(),
        endDate1: '',
        taskNo: '',
        status: 'all',
        site: ''
      }
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.height = window.innerHeight - 210
    })
  },
  methods: {
    shiftQtyFmt (row) {
      const pp = Number(row.partPlanQty)
      const oq = Number(row.orderQty)
      if (!(pp > 0)) {
        return 0
      }
      return (oq * 1.0 / pp).toFixed(1)
    },
    /** rqrq - 动态导出列映射（cancelSfdc 同款） */
    fields () {
      let json = '{'
      this.columnList.forEach((item, index) => {
        if (index === this.columnList.length - 1) {
          json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"'
        } else {
          json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"' + ','
        }
      })
      json += '}'
      return eval('(' + json + ')')
    },
    /** rqrq - 导出时为「计划班数」补充计算字段 shiftQty */
    createExportData () {
      return this.tableData.map(row => {
        const shiftQty = this.shiftQtyFmt(row)
        return Object.assign({}, row, { shiftQty })
      })
    },
    search () {
      getDailyPlanData(this.searchData).then(({ data }) => {
        this.tableData = data.rows
      })
    },
    getStrDate () {
      const dd = new Date()
      const Y = dd.getFullYear()
      const M = (dd.getMonth() + 1) < 10 ? '0' + (dd.getMonth() + 1) : (dd.getMonth() + 1)
      const D = dd.getDate() < 10 ? '0' + dd.getDate() : dd.getDate()
      const H = dd.getHours() < 10 ? '0' + dd.getHours() : dd.getHours()
      const MM = dd.getMinutes() < 10 ? '0' + dd.getMinutes() : dd.getMinutes()
      const S = dd.getSeconds() < 10 ? '0' + dd.getSeconds() : dd.getSeconds()
      return Y + '' + M + D + H + MM + S
    }
  },
  created () {
  }
}
</script>

<style scoped>

</style>
