<template>
  <div class="app-container online-product-detail">
    <el-card class="topCard">
      <div class="topDiv">
        <div class="filter-line">
          <span style="margin-left: 10px">产品编号：</span>
          <el-input
            v-model="productNo"
            placeholder="产品编号"
            clearable
            size="small"
            style="width: 150px"
            @keyup.enter.native="handleSearch" />
          <span style="margin-left: 10px">产品型号：</span>
          <el-input
            v-model="productType"
            placeholder="产品型号"
            clearable
            size="small"
            style="width: 150px"
            @keyup.enter.native="handleSearch" />
          <span style="margin-left: 10px">计划开始时间：</span>
          <el-date-picker
            v-model="planStartTime"
            clearable
            size="small"
            type="daterange"
            align="center"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            format="yyyy-MM-dd"
            range-separator="～"
            start-placeholder="开始日期"
            end-placeholder="结束日期" />
        </div>

        <div class="filter-line">
          <span style="margin-left: 10px">PACK码：</span>
          <el-input
            v-model="packCode"
            placeholder="PACK码"
            clearable
            size="small"
            style="width: 150px"
            @keyup.enter.native="handleSearch" />
          <span style="margin-left: 10px">模组码：</span>
          <el-input
            v-model="moduleCode"
            placeholder="模组码"
            clearable
            size="small"
            style="width: 150px"
            @keyup.enter.native="handleSearch" />
          <span style="margin-left: 10px">箱体码：</span>
          <el-input
            v-model="boxCode"
            placeholder="箱体码"
            clearable
            size="small"
            style="width: 150px"
            @keyup.enter.native="handleSearch" />
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-search"
            style="margin-left: 5px"
            @click="pageSize = 50; pageCurrent = 1; getTable()">查询</el-button>
          <el-button
            :loading="exportLoading"
            :disabled="exportLoading"
            type="success"
            plain
            size="small"
            icon="el-icon-download"
            style="margin-left: 5px"
            @click="exportExcel">导出</el-button>
        </div>
      </div>

      <el-table
        v-loading="loading"
        :data="tableData"
        :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(255, 255, 255, 0.75)"
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        height="620px"
        border
        size="mini">
        <el-table-column align="center" type="index" width="60" label="序号" />
        <el-table-column align="center" prop="发动机号" label="产品编号" width="220" show-overflow-tooltip />
        <el-table-column align="center" prop="产品型号" label="产品型号" width="100" show-overflow-tooltip />
        <el-table-column align="center" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="getOnlineStatusType(scope.row)" size="mini">{{ getOnlineStatusText(scope.row) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="上线工位" label="上线工位" width="120" show-overflow-tooltip />
        <el-table-column align="center" prop="当前工位" label="当前工位" width="120" show-overflow-tooltip />
        <el-table-column align="center" label="上线时间" width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ formatEmpty(scope.row.上线时间) }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="下线时间" width="160" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ formatEmpty(scope.row.下线时间) }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="模组码" label="模组码" min-width="170" show-overflow-tooltip />
        <el-table-column align="center" prop="pack码" label="PACK码" min-width="170" show-overflow-tooltip />
        <el-table-column align="center" prop="箱体码" label="箱体码" min-width="170" show-overflow-tooltip />
      </el-table>

      <div class="block">
        <el-pagination
          v-if="!loading"
          :current-page="pageCurrent"
          :page-sizes="[50, 100, 200]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange" />
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      productNo: '',
      productType: '',
      planStartTime: [],
      packCode: '',
      moduleCode: '',
      boxCode: '',
      total: 0,
      pageSize: 50,
      pageCurrent: 1,
      loading: false,
      exportLoading: false
    }
  },
  created() {
    this.initPlanStartTime()
    this.getTable()
  },
  methods: {
    initPlanStartTime() {
      const today = new Date()
      const yesterday = new Date(today.getTime() - 24 * 60 * 60 * 1000)
      const formatDate = (date) => {
        const year = date.getFullYear()
        const month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
        const day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
        return year + '-' + month + '-' + day
      }
      this.planStartTime = [formatDate(yesterday), formatDate(today)]
    },
    getTable() {
      this.loading = true
      const param = []
      param[0] = ['产品编号', this.productNo || '']
      param[1] = ['产品型号', this.productType || '']
      param[2] = ['计划开始时间起', this.planStartTime && this.planStartTime[0] ? this.planStartTime[0] + ' 00:00:00' : '']
      param[3] = ['计划开始时间止', this.planStartTime && this.planStartTime[1] ? this.planStartTime[1] + ' 23:59:59' : '']
      param[4] = ['PACK码', this.packCode || '']
      param[5] = ['模组码', this.moduleCode || '']
      param[6] = ['箱体码', this.boxCode || '']
      param[7] = ['PageCurrent', this.pageCurrent]
      param[8] = ['PageSize', this.pageSize]
      param[9] = ['PageCount', '1111', 'int', '1']
      param[10] = ['ItemCount', '1111', 'int', '1']
      const Data = this.CreateData('11', '生产计划_上线产品明细_分页查询', param)
      this.ExecDatabase(Data).then(response => {
        const data = response.data || {}
        this.tableData = data.result || []
        this.total = data.output && data.output.length > 0 ? parseInt(data.output[0].ItemCount) : 0
        this.loading = false
      }).catch(() => {
        this.tableData = []
        this.total = 0
        this.loading = false
        this.$message.error('上线产品明细查询失败')
      })
    },
    handleSearch() {
      this.pageCurrent = 1
      this.getTable()
    },
    exportExcel() {
      if (this.tableData.length === 0) {
        this.$message.warning('暂无数据')
        return
      }
      const param = []
      param[0] = ['产品编号', this.productNo || '']
      param[1] = ['产品型号', this.productType || '']
      param[2] = ['计划开始时间起', this.planStartTime && this.planStartTime[0] ? this.planStartTime[0] + ' 00:00:00' : '']
      param[3] = ['计划开始时间止', this.planStartTime && this.planStartTime[1] ? this.planStartTime[1] + ' 23:59:59' : '']
      param[4] = ['PACK码', this.packCode || '']
      param[5] = ['模组码', this.moduleCode || '']
      param[6] = ['箱体码', this.boxCode || '']
      param[7] = ['PageCurrent', 1]
      param[8] = ['PageSize', 99999999]
      param[9] = ['PageCount', '1111', 'int', '1']
      param[10] = ['ItemCount', '1111', 'int', '1']
      const exportData = JSON.parse(this.CreateData('2005', '生产计划_上线产品明细_分页查询', param))
      exportData.exportFileName = '上线产品明细'
      exportData.exportColumns = '发动机号=产品编号,产品型号,上线工位,当前工位,上线时间,下线时间,模组码,pack码=PACK码,箱体码'
      const Data = JSON.stringify(exportData)
      this.runExcelExport(Data, 'exportLoading')
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.getTable()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.getTable()
    },
    formatEmpty(value) {
      return value || '-'
    },
    getOnlineStatusText(row) {
      return row.下线时间 ? '已下线' : '进行中'
    },
    getOnlineStatusType(row) {
      return row.下线时间 ? 'success' : 'warning'
    }
  }
}
</script>

<style scoped>
.topDiv {
  margin-bottom: 10px;
}

.topCard {
  margin-bottom: 20px;
}

.filter-line {
  margin-bottom: 10px;
}

.block {
  text-align: left;
}
</style>
