<template>
  <div>
    <el-card>
      <el-button :disabled="loading" icon="el-icon-search" plain type="primary" @click="searchTable()">查询</el-button>
      <div style="margin-top: 10px">
        <el-table
          v-loading="loading"
          :cell-style="set_cell_style"
          :data="tableData"
          border
          element-loading-background="rgba(0, 0, 0, 0.2)"
          element-loading-spinner="el-icon-loading"
          element-loading-text="数据加载中"
          height="707"
          highlight-current-row
          size="mini"
          stripe
          style="width: 1200px"
          tooltip-effect="dark">
          <el-table-column align="center" label=" " type="index" width="60"/>
          <el-table-column align="center" label="产线" width="150">
            <template slot-scope="scope">
              {{ scope.row.产线 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="订单号" width="150">
            <template slot-scope="scope">
              {{ scope.row.订单号 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="型号" width="150">
            <template slot-scope="scope">
              {{ scope.row.机型号 }}
            </template>
          </el-table-column>
          <el-table-column align="机型名称" label="机型名称">
            <template slot-scope="scope">
              {{ scope.row.机型名称 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="计划数量">
            <template slot-scope="scope">
              {{ scope.row.计划数量 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作">
            <template slot-scope="scope">
              <el-button @click="updateLine(scope.row)">修改</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <el-dialog
      :visible.sync="loading"
      title="修改配置"
      width="20%">
      <el-form ref="form1" label-position="right" label-width="110px">
        <el-form-item label="产线" style="display: inline-block">
          <el-input v-model="lineData.lineName" readonly/>
        </el-form-item>
        <el-form-item label="订单号" style="display: inline-block">
          <el-input v-model="lineData.order"/>
        </el-form-item>
        <el-form-item label="机型号" style="display: inline-block">
          <el-input v-model="lineData.engineType"/>
        </el-form-item>
        <el-form-item label="机型名称" style="display: inline-block">
          <el-input v-model="lineData.engineTypeName"/>
        </el-form-item>
        <el-form-item label="计划数量" style="display: inline-block">
          <el-input v-model="lineData.planCount"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="loading = false">取 消</el-button>
        <el-button type="primary" @click="updateLineSubmit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>

import axios from 'axios'

export default {
  data() {
    return {
      // pickerOptions: {
      //   disabledDate(time) {
      //     return time.getTime() > Date.now()
      //   }
      // },
      lineData: {
        lineName: '',
        order: '',
        engineType: '',
        engineTypeName: '',
        planCount: 0
      },
      TagName: '',
      OpName: '',
      Order: '',
      EngineType: '',
      OpNameList: [],
      collectionTime: [],
      loading: false,
      tableData: [],
      lineUrlList: [],
      total: 0, // 总条数
      pageSize: 50, // 每页显示条数
      pageList: 1 // 后台获取页
    }
  },
  created() {
    this.getOpName()
    var now = new Date()
    var start = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), 0, 0, 0)
    var end = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59)
    this.collectionTime = [start, end]
    this.lineUrlList = window.dt_Config.lineUrlList
    this.searchTable()
  },
  methods: {
    updateLine(item) {
      this.lineData.lineName = item.产线
      this.lineData.order = item.订单号
      this.lineData.engineType = item.机型号
      this.lineData.engineTypeName = item.机型名称
      this.lineData.planCount = item.计划数量
      this.loading = true
    },
    updateLineSubmit() {
      if (!this.lineData.lineName || !this.lineData.order || !this.lineData.engineType || !this.lineData.engineTypeName || !this.lineData.planCount) {
        this.$message.error('请将信息填写完全后提交')
        return
      }
      for (const key in this.lineUrlList) {
        const url = this.lineUrlList[key]
        if (key == this.lineData.lineName) {
          var param = []
          param[0] = { name: '产线', value: this.lineData.lineName }
          param[1] = { name: '订单号', value: this.lineData.order }
          param[2] = { name: '机型号', value: this.lineData.engineType }
          param[3] = { name: '机型名称', value: this.lineData.engineTypeName }
          param[4] = { name: '计划数量', value: this.lineData.planCount }

          // param[0] = ['产线', this.lineData.lineName]
          // param[1] = ['订单号', this.lineData.order]
          // param[2] = ['机型号', this.lineData.engineType]
          // param[3] = ['机型名称', this.lineData.engineTypeName]
          // param[4] = ['计划数量', this.lineData.planCount]
          var data = {
            name: '订单_修改',
            param: JSON.stringify(param),
            type: '11',
            ModularID: '/SystemMaintenance/TVConfigManager/index'
          }
          axios.post(url, data).then(response => {
            if (response.data.length > 0) {
              if (response.data[0].result == 1) {
                this.$message.success(response.data[0].msg)
                this.searchTable()
                this.loading = false
              } else {
                this.$message.error(response.data[0].msg)
              }
            } else {
              this.$message.error('操作失败！')
            }
          })
        }
      }
    },
    // 查询工位号
    getOpName() {
      var param = []
      var Data = this.CreateData('11', '工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.OpNameList = []
        for (const item of response.data) {
          this.OpNameList.push({ label: item.工位号, value: item.工位号 })
        }
      })
    },
    // 查询
    searchTable() {
      this.tableData = []
      for (const key in this.lineUrlList) {
        const url = this.lineUrlList[key]
        console.log(key, url)
        // var param = []
        var data = { name: '订单_查询', param: '[]', type: '11', ModularID: '/SystemMaintenance/TVConfigManager/index' }
        axios.post(url, data).then(response => {
          response.data[0].产线 = key
          this.tableData.push(...response.data)
        })
      }
    },
    // 导出数据
    searchTableALL() {
      if (this.collectionTime.length === 0) {
        this.$message.error('请选择需要查询的时间段！')
        return
      }
      var param = []
      if (this.tableData.length === 0) {
        this.$message.warning('请查询要导出的数据')
        return
      }
      param[0] = ['TagName', this.TagName]
      param[1] = ['开始时间', this.collectionTime[0]]
      param[2] = ['结束时间', this.collectionTime[1]]
      var Data = this.CreateData('2003', '测量数据_查询_导出', param)
      const blob = 'data:image/gif;base64,R0lGODlhCQACAIAAAMzMzP///yH5BAEAAAEALAAAAAAJAAIAAAIEjI9pUAA7'
      this.exportExcel_NPOI(Data, blob)
    },
    // 设置单元格颜色
    set_cell_style({ row, column, rowIndex, columnIndex }) {
      const Value = parseFloat(row.Duration)
      const LimitUp1 = parseFloat(row.LimitUp1)
      const LimitDown1 = parseFloat(row.LimitDown1)
      const LimitUp2 = parseFloat(row.LimitUp2)
      const LimitDown2 = parseFloat(row.LimitDown2)
      if (Value.toString() === 'NaN' || LimitUp1.toString() === 'NaN' || LimitDown1.toString() === 'NaN' || LimitUp2.toString() === 'NaN' || LimitDown2.toString() === 'NaN') {
        return ''
      } else {
        if (Value > LimitUp2) {
          return {
            'background': '#fcd4d4'
          }
        }
        if (Value < LimitDown2) {
          return {
            'background': '#fcd4d4'
          }
        }
        if (Value >= LimitDown2 && Value < LimitDown1) {
          return {
            'background': '#fcefb9'
          }
        }
        if (Value > LimitUp1 && Value <= LimitUp2) {
          return {
            'background': '#fcefb9'
          }
        }
      }
    },
    // 分页
    handleSizeChange(val) {
      this.pageList = 1
      this.pageSize = val
      this.searchTable()
    },
    handleCurrentChange(val) {
      this.pageList = val
      this.searchTable()
    }
  }
}
</script>
