<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_CODE">
        <el-select v-model="queryParams.PRODUCT_LINE_CODE" placeholder="请选择产线">
          <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="工位">
        <el-select v-model="queryParams.STATION_CODE" placeholder="请选择工位" multiple clearable>
          <el-option
            v-for="item in findStationList"
            :key="item.productLineCode"
            :label="item.productLineName"
            :value="item.productLineCode"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起止时间">
        <el-date-picker
          v-model="daterangeBeginTime"
          style="width: 340px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </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"
          plain
          size="mini"
        >合计数量：{{ totalSum }}
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出
        </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="FACTORY_NAME" v-if="columns[1].visible"/>
      <el-table-column label="产线编号" align="center" prop="PRODUCT_LINE_CODE" v-if="columns[2].visible"/>
      <el-table-column label="产线名称" align="center" prop="PRODUCT_LINE_NAME" v-if="columns[3].visible"/>
      <el-table-column label="质检工位编号" align="center" prop="STATION_CODE" v-if="columns[4].visible"/>
      <el-table-column label="质检工位名称" align="center" prop="STATION_NAME" v-if="columns[5].visible"/>
      <el-table-column label="检测数不合格数" align="center" prop="REWORK_NUMBER" v-if="columns[6].visible"/>
      <el-table-column label="合格率(%)" align="center" prop="RATE" v-if="columns[7].visible"/>
      <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)"
          >查看缺陷明细
          </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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row gutter="15">
          <el-col :span="11">
            <el-form-item label="产线" prop="PRODUCT_LINE_NAME">
              <el-input v-model="form.PRODUCT_LINE_NAME" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="质检工位" prop="STATION_NAME">
              <el-input v-model="form.STATION_NAME" :disabled="true"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider content-position="center">工位缺陷明细信息</el-divider>

        <el-table :data="qualityDefectstList" ref="detailElectricalinspection"
        >
          <el-table-column type="selection" width="50" align="center"/>
          <el-table-column label="序号" prop="SEQ" width="150">
          </el-table-column>
          <el-table-column label="缺陷名称" prop="QUALITY_DEFECT_NAME" width="150">
          </el-table-column>
          <el-table-column label="数量" prop="DEFECT_NUMBER" width="150">
          </el-table-column>
          <el-table-column label="比例" prop="RATE" width="150">
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  qualityDefectsStatisticalAnalysisList,
  qualityDefectsDetailList
} 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,
      //合计数量
      totalSum: 0,
      // 报表表格数据
      reportList: [],
      // 质量数据
      qualityDefectstList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeBeginTime: [],
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        beginBeginTime: null,
        endBeginTime: null,
        STATION_CODE: [],
        stationCodeList: null,
        PRODUCT_LINE_CODE: 'CX_02',
        ORDER_CODE: null,
        MATERIAL_CODE: null,
        MATERIAL_NAME: null,
        BEGIN_DATE: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      columns: [
        { key: 0, label: `主键标识`, visible: false },
        { key: 1, label: `工厂`, visible: true },
        { key: 2, label: `产线编号`, visible: false },
        { key: 3, label: `产线名称`, visible: true },
        { key: 4, label: `质检工位编号`, visible: false },
        { key: 5, label: `质检工位名称`, visible: true },
        { key: 6, label: `检测数不合格数`, visible: true },
        { key: 7, label: `合格率(%)`, visible: true }
      ],
      // 产线选项
      productLineList: [],
      // 工位选项
      findStationList: []
    }
  },
  created() {
    findProductLineList({ productLineType: 1 }).then(response => {
      this.productLineList = response.data
    })
    findProductLineList({ productLineType: 2, stationType: 2 }).then(response => {
      this.findStationList = response.data
    })
    const nowDate = parseTime(new Date(), '{y}-{m}-{d}')
    this.daterangeBeginTime[0] = nowDate + ' 00:00:00'
    this.daterangeBeginTime[1] = nowDate + ' 23:59:59'
    this.getList()
  },
  methods: {
    /** 查询工单信息列表 */
    getList() {
      this.loading = true
      if (null != this.daterangeBeginTime && '' != this.daterangeBeginTime) {
        this.queryParams.beginBeginTime = this.daterangeBeginTime[0]
        this.queryParams.endBeginTime = this.daterangeBeginTime[1]
      } else {
        this.queryParams.beginBeginTime = null
        this.queryParams.endBeginTime = null
      }
      if (this.queryParams.STATION_CODE.length > 0) {
        this.queryParams.stationCodeList = this.queryParams.STATION_CODE.join(',')
      }
      qualityDefectsStatisticalAnalysisList(this.queryParams).then(response => {
        this.reportList = response.data
        this.totalSum = 0;
        this.reportList.forEach(e => {
          this.totalSum += e.REWORK_NUMBER
        })
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.daterangeBeginTime = []
      this.form = {
        WORK_CENTER_CODE: null,
        PRODUCT_LINE_NAME: null,
        ORDER_CODE: null,
        STATION_CODE: [],
        stationCodeList: null,
        MATERIAL_CODE: null,
        MATERIAL_NAME: null,
        BEGIN_DATE: null
      }
      const nowDate = parseTime(new Date(), '{y}-{m}-{d}')
      this.daterangeBeginTime[0] = nowDate + ' 00:00:00'
      this.daterangeBeginTime[1] = nowDate + ' 23:59:59'
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },

    /** 查看明细 */
    handleOrderDetails(row) {
      this.reset()
      const objId = row.objId || this.ids
      this.open = true
      qualityDefectsDetailList({
        stationCode: row.STATION_CODE,
        beginBeginTime: this.queryParams.beginBeginTime,
        endBeginTime: this.queryParams.endBeginTime
      }).then(response => {
        this.qualityDefectstList = response.data
        this.form.PRODUCT_LINE_NAME = row.PRODUCT_LINE_NAME
        this.form.STATION_NAME = row.STATION_NAME
        this.open = true
        this.title = '缺陷明细'
      })
    },

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

    /** 导出按钮操作 */
    handleExport() {
      this.download('/report/qualityReport/qualityDefectsStatisticalAnalysisList/export', {
        ...this.queryParams
      }, `质量缺陷统计分析_${new Date().getTime()}.xlsx`)
    }

  }
}
</script>
