<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.alarmType" placeholder="请选择异常类型" >
          <el-option
            v-for="item in alarmTypeList"
            :key="item.key"
            :label="item.label"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期类型">
        <el-select v-model="queryParams.dateType" placeholder="请选择日期类型" >
          <el-option
            v-for="item in dateTypeList"
            :key="item.key"
            :label="item.label"
            :value="item.key"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起止日期">
        <el-date-picker
          v-model="daterangeBeginTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          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>

    <div class="chart1">
      <Chart ref="chart1"></Chart>
    </div>

    <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="INSPECTOR_TIME" v-if="columns[1].visible"/>
      <el-table-column label="异常数" align="center" prop="QUALITY_SUM" v-if="columns[2].visible"/>
      <el-table-column label="产量" align="center" prop="OFF_LINE_NUM" v-if="columns[3].visible"/>
      <el-table-column label="异常率" align="center" prop="REPAIR_RATE" v-if="columns[4].visible">
        <template slot-scope="scope">
          {{ parseFloat(scope.row.REPAIR_RATE) }}%
        </template>
      </el-table-column>
    </el-table>
    <!--    <pagination-->
    <!--      v-show="total>0"-->
    <!--      :total="total"-->
    <!--      :page.sync="queryParams.pageNum"-->
    <!--      :limit.sync="queryParams.pageSize"-->
    <!--      @pagination="getList"-->
    <!--    />-->


  </div>
</template>

<script>
import {
  downgradeProportionReportList
} from '@/api/report/reportAPI'
import {findProductLineList} from '@//api/base/productLine'
import {parseTime} from '@//utils/ruoyi'
import Chart from "@/components/board/Chart";

const vw = (document.documentElement.clientWidth || document.body.clientWidth) / 100
export default {
  name: 'DowngradeProportionReport',
  dicts: ['is_flag'],
  components: {
    Chart
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //合计数量
      totalSum: 0,
      // 报表表格数据
      reportList: [],
      // 质量数据
      qualityDefectstList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 更新时间时间范围
      daterangeBeginTime: [],
      // 日期类型
      dateTypeList: [{ key: 10, label: '日' }, { key: 7, label: '月' }, { key: 4, label: '年' }],
      // 异常类型
      alarmTypeList: [{ key: 1, label: '降级品' }, { key: 2, label: '报废品' } ],
      // 查询参数
      queryParams: {
        // pageNum: 1,
        // pageSize: 10,
        beginBeginTime: null,
        endBeginTime: null,
        stationCodeList: null,
        dateType: 10,
        alarmType: 1,
        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: true},
        {key: 3, label: `产量`, visible: true},
        {key: 4, label: `异常率`, visible: true}
      ],
      // 产线选项
      productLineList: [],
    }
  },
  created() {
    findProductLineList({productLineType: 1}).then(response => {
      this.productLineList = response.data
    })
    const weekDate = parseTime(new Date(Date.now() - 6 * 24 * 60 * 60 * 1000), '{y}-{m}-{d}')
    const nowDate = parseTime(new Date(), '{y}-{m}-{d}')
    this.daterangeBeginTime[0] = weekDate
    this.daterangeBeginTime[1] = nowDate
    this.queryParams.beginBeginTime = this.daterangeBeginTime[0]
    this.queryParams.endBeginTime = this.daterangeBeginTime[1]
    this.getList()
  },
  watch: {
    'queryParams.PRODUCT_LINE_CODE': function (newVal, oldVal) {
      // 执行方法，可以在这里调用你的方法
      findProductLineList({parentId: this.queryParams.PRODUCT_LINE_CODE, stationType: 2}).then(response => {
        this.findStationList = response.data
      })
    },
  },
  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
      }

      downgradeProportionReportList(this.queryParams).then(response => {
        this.reportList = response.data
        this.totalSum = 0;
        this.reportList.forEach(e => {
          this.totalSum += e.QUALITY_SUM
        })
        this.total = response.total
        this.loading = false

        let e = response.data
        this.$refs.chart1.setData({
          grid: {
            top: "15%",
            left: "1%",
            right: "1%",
            bottom: "2%",
            containLabel: true,
          },
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              crossStyle: {
                color: '#000',
              },
            },
          },
          legend: {
            itemWidth: 3 * vw,
            data: ['异常数', '异常率(%)', '目标(%)'],
            textStyle: {
              color: "#000",
              margin: 15,
              fontSize: 0.75 * vw
            },
          },
          xAxis: [
            {
              axisLabel: {
                //坐标轴刻度标签的相关设置
                textStyle: {
                  color: "#000",
                  margin: 15,
                  fontSize: 0.75 * vw
                },
              },
              type: 'category',
              data: e.map(val => val.INSPECTOR_TIME),
              axisPointer: {
                type: 'shadow'
              }
            }
          ],
          yAxis: [
            {
              type: 'value',
              splitLine: {
                lineStyle: {
                  color: "rgba(255,255,255,0.1)",
                },
              },
              axisLabel: {
                //坐标轴刻度标签的相关设置
                textStyle: {
                  color: "#000",
                  margin: 15,
                  fontSize: 0.75 * vw
                },
                formatter: '{value}'
              }
            },
            {
              type: 'value',
              splitLine: {
                show: false,
              },
              axisLabel: {
                //坐标轴刻度标签的相关设置
                textStyle: {
                  color: "#000",
                  margin: 15,
                  fontSize: 0.75 * vw
                },
                formatter: '{value}% '
              }
            }
          ],
          series: [
            {
              name: '异常数',
              type: 'bar',
              itemStyle: {
                normal: {
                  color: function (val) {
                    if (parseFloat(e[val.dataIndex].REPAIR_RATE) < 0.2) {
                      return '#30e391'
                    } else {
                      return '#FE70A6'
                    }
                    // return "#0D81ED"
                  },
                },
              },
              data: e.map(val => val.QUALITY_SUM),
              label: {
                show: true,
                position: 'top',
                textStyle: {
                  color: "rgba(255,255,255,0.5)",
                  fontSize: 0.75 * vw
                },
              },
            },
            {
              name: '异常率(%)',
              type: 'line',
              yAxisIndex: 1,
              data: e.map(val => parseFloat(val.REPAIR_RATE)),
              itemStyle: {
                normal: {
                  color: '#F9A25B',
                },
              },
            },
            {
              name: '目标(%)',
              type: 'line',
              yAxisIndex: 1,
              data: e.map(val => parseFloat(val.TARGET)),
              itemStyle: {
                normal: {
                  color: '#0DB99D',
                },
              },
            }
          ]
        })
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.daterangeBeginTime = []
      this.form = {
        WORK_CENTER_CODE: null,
        PRODUCT_LINE_NAME: null,
        ORDER_CODE: null,
        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()
    },

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

    /** 导出按钮操作 */
    handleExport() {
      this.download('/report/qualityReport/downgradeProportionReportList/export', {
        ...this.queryParams
      }, `质量异常率报表_${new Date().getTime()}.xlsx`)
    },

  }
}
</script>
<style>
.chart1 {
  width: 100%;
  height: 20vw;
}

</style>
