<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="产线" prop="PRODUCT_LINE_NAME">
        <el-select v-model="queryParams.productLineCode" placeholder="请选择产线" clearable>
          <el-option
            v-for="item in productLineList"
            :key="item.productLineCode"
            :label="item.productLineName"
            :value="item.productLineCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="产品编号" prop="PRODUCT_CODE">
        <el-input
          v-model="queryParams.PRODUCT_CODE"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="PRODUCT_NAME">
        <el-input
          v-model="queryParams.PRODUCT_NAME"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SAP订单编号" prop="ORDER_CODE">
        <el-input
          v-model="queryParams.ORDER_CODE"
          placeholder="请输入SAP计划编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="（工位）区间">
        <el-select v-model="queryParams.beginStationCode" placeholder="请选择起始工位" clearable>
          <el-option
            v-for="item in findStationList"
            :key="item.executionSort"
            :label="item.productLineName"
            :value="item.executionSort"
          ></el-option>
        </el-select>
        <span style="margin: 5px;">-</span>
        <el-select v-model="queryParams.endStationCode" placeholder="请选择结束工位" clearable>
          <el-option
            v-for="item in findStationList"
            :key="item.executionSort"
            :label="item.productLineName"
            :value="item.executionSort"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="info"
        >合计数量：{{ totalSum }}
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleDetailExport"
        >明细导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reportList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键标识" align="center" prop="objId" v-if="columns[0].visible"/>
      <el-table-column label="产线" align="center" prop="PRODUCT_LINE_NAME" width="80" v-if="columns[1].visible"/>
      <el-table-column label="订单编号" align="center" prop="ORDER_CODE" width="100" v-if="columns[3].visible"/>
      <el-table-column label="物料编号" align="center" prop="MATERIAL_CODE" width="130" v-if="columns[4].visible"/>
      <el-table-column label="物料名称" align="center" prop="MATERIAL_NAME" width="430" v-if="columns[5].visible"/>
      <el-table-column label="产品编号" align="center" prop="PRODUCT_CODE" width="130" v-if="columns[6].visible"/>
      <el-table-column label="产品名称" align="center" prop="PRODUCT_NAME" width="230" v-if="columns[7].visible"/>
      <el-table-column label="在制数量" align="center" prop="COMP_AMOUNT" width="120" v-if="columns[8].visible">
        <template slot-scope="scope">
          <el-button type="text" @click="openDifference(scope.row)">{{ scope.row.COMP_AMOUNT }}</el-button>
        </template>
      </el-table-column>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleOrderDetails(scope.row)"-->
      <!--            v-hasPermi="['report:pointDnb:edit']"-->
      <!--          >查看订单明细-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 详情信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-table :data="reportDetailList" :row-class-name="rowDetailIndex"
                ref="reportDetailRef"
      >
        <el-table-column label="序号" align="center" prop="index">
        </el-table-column>
        <el-table-column label="箱体码" align="center" prop="MATERIAL_BARCODE" width="190">
        </el-table-column>
        <el-table-column label="物料编号" align="center" prop="MATERIAL_CODE" width="110">
        </el-table-column>
        <el-table-column label="物料名称" align="center" prop="MATERIAL_NAME" width="280">
        </el-table-column>
        <el-table-column label="最后扫描工位" align="center" prop="STATION_NAME" width="150">
        </el-table-column>
        <el-table-column label="最后扫描时间" align="center" prop="COMPLETE_DATE" width="150">
        </el-table-column>
      </el-table>
      <pagination
        v-show="detailTotal>0"
        :total="detailTotal"
        :page.sync="queryDetailParams.pageNum"
        :limit.sync="queryDetailParams.pageSize"
        @pagination="getDetailList"
      />
      <!--      </el-form>-->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {
  WIPInquiryDetailReportList,
  WIPInquiryReportList
} from '@/api/report/reportAPI'
import { findProductLineList } from '@//api/base/productLine'
import { parseTime } from '@//utils/ruoyi'

export default {
  name: 'ReportInfo',
  dicts: ['is_flag'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 总条数
      detailTotal: 0,
      //合计数量
      totalSum: 0,
      // 报表表格数据
      reportList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeBeginTime: [],
      // 详情表格数据
      reportDetailList: [],
      // 查询参数
      queryDetailParams: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        beginBeginTime: null,
        endBeginTime: null,
        productLineCode: 'CX_02',
        beginStationCode: '109',
        endStationCode: '270',
        ORDER_CODE: null,
        MATERIAL_CODE: null,
        MATERIAL_NAME: null,
        PRODUCT_CODE: null,
        PRODUCT_NAME: null,
        BEGIN_DATE: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [
        { key: 0, label: `主键标识`, visible: false },
        { key: 1, label: `产线`, visible: true },
        { key: 2, label: `工位`, visible: true },
        { key: 3, label: `订单编号`, visible: true },
        { key: 4, label: `物料编号`, visible: true },
        { key: 5, label: `物料名称`, visible: true },
        { key: 6, label: `产品编号`, visible: true },
        { key: 7, label: `产品名称`, visible: true },
        { key: 8, label: `在制数量`, visible: true }
      ],
      // 产线选项
      productLineList: [],
      // 工位选项
      findStationList: []
    }
  },
  watch: {
    'queryParams.productLineCode': function(newVal, oldVal) {
      // 执行方法，可以在这里调用你的方法
      this.getStationList()
    }
  },
  created() {
    findProductLineList({ productLineType: 1 }).then(response => {
      this.productLineList = response.data
    }).then(() => {
      return findProductLineList({ productLineType: 2, parentId: this.queryParams.productLineCode, executionSort: '1' })
    }).then(response => {
      this.findStationList = response.data.filter(station => station.executionSort != null)
    })
    const nowDate = parseTime(new Date(), '{y}-{m}-{d}')
    this.daterangeBeginTime[0] = nowDate
    this.daterangeBeginTime[1] = nowDate
    this.getList()
  },
  methods: {
    /** 查询工单信息列表 */
    getList() {
      this.loading = true
      WIPInquiryReportList(this.queryParams).then(response => {
        this.reportList = response.rows
        this.total = response.total
        this.totalSum = 0
        this.reportList.forEach(e => {
          this.totalSum += e.COMP_AMOUNT
        })
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.daterangeBeginTime = []
      this.form = {
        beginStationCode: null,
        endStationCode: null,
        ORDER_CODE: null,
        MATERIAL_CODE: null,
        MATERIAL_NAME: null,
        PRODUCT_CODE: null,
        PRODUCT_NAME: null,
        BEGIN_DATE: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 查看明细 */
    // handleOrderDetails(row) {
    //   this.$tab.openPage('生产计划', '/product/planInfo', { orderCode: row.ORDER_CODE })
    // },

    /** 查看明细 */
    openDifference(row) {
      if (row.ORDER_CODE == null) {
        this.$modal.msgError('订单号不为空！')
        return
      }
      this.queryDetailParams = { ...this.queryParams };
      this.queryDetailParams.ORDER_CODE = row.ORDER_CODE

      WIPInquiryDetailReportList(this.queryDetailParams).then(response => {
        this.reportDetailList = response.rows
        this.detailTotal = response.total
        this.open = true
        this.title = '在制品明细表'
      })
    },

    getDetailList() {
      WIPInquiryDetailReportList(this.queryDetailParams).then(response => {
        this.reportDetailList = response.rows
        this.detailTotal = response.total
        this.loading = false
      })
    },
    /** 数据明细序号 */
    rowDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1 + (this.queryDetailParams.pageNum - 1) * this.queryDetailParams.pageSize
    },
    /** 获取工位方法 */
    getStationList() {
      this.queryParams.beginStationCode = null
      this.queryParams.endStationCode = null
      findProductLineList({ productLineType: 2, parentId: this.queryParams.productLineCode, executionSort: '1' })
        .then(response => {
          this.findStationList = response.data.filter(station => station.executionSort != null)
        })
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('/report/productionReport/WIPInquiryReportList/export', {
        ...this.queryParams
      }, `在制品查询报表_${new Date().getTime()}.xlsx`)
    },

    /** 导出按钮操作 */
    handleDetailExport() {
      this.download('/report/productionReport/WIPInquiryDetailReportList/export', {
        ...this.queryParams
      }, `在制品查询明细报表_${new Date().getTime()}.xlsx`)
    }

  }
}
</script>
