<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" style="width: 150px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <!--        <span style="margin-left: 10px">保养类型：</span>-->
        <!--        <el-select v-model="maintenanceTypeValue" placeholder="请选择保养类型" size="small" style="width: 150px">-->
        <!--          <el-option-->
        <!--            v-for="item in maintenanceType"-->
        <!--            :key="item.value"-->
        <!--            :label="item.label"-->
        <!--            :value="item.value"-->
        <!--            size="mini"/>-->
        <!--        </el-select>-->
        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="searchOrder();searchTable()">查询</el-button>
        <el-button type="primary" size="small" icon="el-icon-circle-plus" @click="creatOrder('form')">增加</el-button>
        <el-button type="danger" plain size="small" icon="el-icon-upload2" @click="uploadPictures()">图片</el-button>
      </div>
      <el-col :span="12">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="tableData"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          :cell-class-name="tableRowClassName"
          element-loading-text="数据加载中"
          element-loading-spinner="el-icon-loading"
          element-loading-background="rgba(0, 0, 0, 0.2)"
          stripe
          class="main"
          tooltip-effect="dark"
          style="width: 100%;max-height: 700px"
          border
          size="mini">
          <el-table-column width="60" label="序号" align="center">
            <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">
              {{ 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="操作" width="150">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleEdit(scope.row)">修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="deleteOrder(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="11" style="margin-left: 20px;margin-top: 5px">
        <div v-for="(url, key) in urls" :key="key" style="width: 600px;height: 600px;">
          <el-image :key="url.url" :src="url.url" :preview-src-list="urls1" lazy style="width: 895px; height: 595px"/>
        </div>
      </el-col>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" :title="title" center style="min-height: 200px;" width="450px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="110px">
        <el-form-item v-show="title === '编辑'" label="工位号" prop="serialNumber" style="display: inline-block">
          <el-select v-model="form.stationNumberValue" placeholder="请选择工位号" size="small">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item v-show="title === '编辑'" label="保养类型" prop="serialNumber" style="display: inline-block">
          <el-select v-model="form.maintenanceTypeValue" placeholder="请选择保养类型" size="small">
            <el-option
              v-for="item in maintenanceType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
        </el-form-item>
        <el-form-item label="序号" prop="serialNumber" style="display: inline-block">
          <el-input
            v-model="form.serialNumber"
            clearable
            size="small"
            placeholder="序号"/>
        </el-form-item>
        <el-form-item label="设备编号" prop="orderNumber" style="display: inline-block">
          <el-input
            v-model="form.orderNumber"
            disabled
            clearable
            size="small"
            placeholder="设备编号"/>
          <el-button type="info" plain size="mini" style="margin-left: 10px" @click="searchEquipment()">...</el-button>
        </el-form-item>
        <el-form-item label="设备名称" prop="partName" style="display: inline-block">
          <el-input
            v-model="form.partName"
            disabled
            clearable
            size="small"
            placeholder="设备名称"/>
        </el-form-item>
        <el-form-item label="保养内容" prop="maintenanceContents" style="display: inline-block">
          <el-input
            v-model="form.maintenanceContents"
            :row="2"
            type="textarea"
            clearable
            size="small"
            style="width: 200px;"
            placeholder="保养内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button v-if="title === '增加'" type="primary" size="small" @click="submitAdd('form')">确定</el-button>
        <el-button v-if="title === '编辑'" type="primary" size="small" @click="submitAdd1('form')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogFormVisible1" title="设备基础信息查询" center style="min-height: 200px;" width="800px">
      <div>
        <span style="margin-left: 10px">工位号：</span>
        <el-select v-model="stationNumberValue1" placeholder="请选择工位号" size="small" style="width: 150px">
          <el-option
            v-for="item in stationNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            size="mini"/>
        </el-select>
        <span style="margin-left: 10px">设备编号：</span>
        <el-input v-model="engineTypeNumber1" style="width:150px;" placeholder="设备编号" size="small"/>
        <el-button type="primary" plain size="small" icon="el-icon-search" style="margin-left: 10px" @click="searchOrder1()">搜索</el-button>
        <el-button type="info" plain size="small" icon="el-icon-close" style="margin-left: 10px" @click="dialogFormVisible1 = false">取消</el-button>
        <el-button type="warning" plain size="small" icon="el-icon-check" style="margin-left: 10px" @click="selectSubmit()">提交</el-button>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData1"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        class="main"
        tooltip-effect="dark"
        style="width: 100%;max-height: 700px"
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column :selectable="selectable" type="selection" width="45" align="center"/>
        <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">
            {{ 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">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent1"
          :page-sizes="[15, 30, 40]"
          :page-size="pageSize1"
          :total="total1"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange1"
          @current-change="handleCurrentChange1"/>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible2" title="上传图片" center style="height: 800px" width="17%">
      <el-upload
        id="invoiceScan"
        ref="upload"
        :on-remove="handelLimitStyle"
        :on-change="handelLimit"
        :multiple="true"
        :auto-upload="false"
        :on-success="uploadSuccess"
        :on-preview="handlePictureCardPreview"
        :before-upload="beforeUpload"
        :on-exceed="warningExceed"
        :limit="limit"
        :data="Data"
        :action="upload"
        style="margin-left: 60px"
        list-type="picture-card">
        <i class="el-icon-plus"/>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2=false">取消</el-button>
        <el-button type="primary" style="width: 70px" @click="submitPicture()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { url } from '@/utils/request'
export default {
  data() {
    return {
      urls: [], // 图片数组
      urls1: [], // 图片数组
      limit: 1,
      Data: {},
      upload: url,
      maintenanceType: '', // 保养类型
      maintenanceTypeValue: '', // 保养类型
      dateTime: '',
      stationNumber: [],
      stationNumberValue: '',
      stationNumberValue1: '',
      stationName: '',
      orderNumber: [],
      orderNumberValue: '',
      engineTypeNumber: '',
      engineTypeNumber1: '',
      repairPersonnel: [], // 维修人员
      title: '',
      form: { // 创建订单
        partName: '', // 设备名称
        orderNumber: '', // 设备编号
        serialNumber: '', // 序号
        maintenanceContents: '', // 保养内容
        stationNumberValue: '', // 工位号
        maintenanceTypeValue: '', // 保养类型
        MID: '', // 设备流水号
        ID: ''
      },
      rules: { // 创建订单验证
        partName: [{ required: true, message: '请输入', trigger: 'blur' }],
        orderNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        serialNumber: [{ required: true, message: '请输入', trigger: 'blur' }],
        maintenanceContents: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      tableData: [],
      tableData1: [],
      mid: [],
      maintenanceNumber: '',
      maintenanceName: '',
      dialogFormVisible: false, // 创建订单
      dialogFormVisible1: false, // 查询设备
      dialogFormVisible2: false, // 上传图片
      total1: 0, // 总条数
      pageList1: 15, // 每页显示条数
      pageSize1: 15, // 每页显示条数
      pageCurrent1: 1, // 后台获取页
      loading: false
    }
  },
  watch: {
    stationNumberValue() {
      for (let i = 0; i < this.stationNumber.length; i++) {
        if (this.stationNumber[i].value === this.stationNumberValue) {
          console.log(this.stationNumber[i].label, 'this.stationNumber[i].label')
          this.stationName = this.stationNumber[i].labelName
        }
      }
      if (!this.stationNumberValue) {
        this.stationName = ''
      }
    }
  },
  created() {
    var year, month, day, audiTime
    this.date = new Date() // 修改数据date
    year = new Date().getFullYear()
    month = new Date().getMonth() + 1 < 10 ? '0' + (new Date().getMonth() + 1) : new Date().getMonth() + 1
    day = new Date().getDate() < 10 ? '0' + new Date().getDate() : new Date().getDate()
    audiTime = year + '-' + month + '-' + day
    console.log(audiTime, 'audiTime')
    this.dateTime = [audiTime, audiTime]
    this.getStationNumber()
    this.getMaintenanceType()
  },
  methods: {
    handleSelectionChange(val) {
      if (val.length > 0) {
        if (val.length > 1) {
          this.$refs.multipleTable.clearSelection()
          this.$refs.multipleTable.toggleRowSelection(val.pop())
        } else {
          this.mid = val.map((item) => item.设备流水号)
          this.maintenanceNumber = val.map((item) => item.设备编号)
          this.maintenanceName = val.map((item) => item.设备名称)
        }
      } else {
        this.mid = []
        this.maintenanceNumber = ''
        this.maintenanceName = ''
      }
      console.log(this.mid, 'this.mid')
      console.log(this.maintenanceNumber, 'maintenanceNumber')
      console.log(this.maintenanceName, 'maintenanceName')
    },
    // @select 事件方法
    selectable(selection, row) {
      return true
    },
    // 初始化获取订单号
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', 'MES_计划BOM_工位与名称_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data.length) {
            this.stationNumberValue = response.data[0].工位号
            this.stationName = response.data[0].工位名称
          }
          this.stationNumber.push({
            label: response.data[i].工位名称,
            value: response.data[i].工位号
          })
        }
      })
    },
    // 初始化获取订单号
    getMaintenanceType() {
      this.maintenanceType = []
      var param = []
      var Data = this.CreateData('11', '设备管理_保养类型_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          if (response.data.length) {
            this.maintenanceTypeValue = response.data[0].保养类型代码
          }
          this.maintenanceType.push({
            label: response.data[i].保养类型名称,
            value: response.data[i].保养类型代码
          })
        }
      })
    },
    // 查询订单
    searchOrder() {
      this.tableData = []
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['保养类型代码', this.maintenanceTypeValue]
      var Data = this.CreateData('11', '设备管理_设备维护内容_查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
      })
    },
    searchTable() {
      this.urls = []
      this.urls1 = []
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['保养类型代码', this.maintenanceTypeValue]
      var Data = this.CreateData('11', '设备管理_设备维护图片_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          console.log(response.data, 'response.data')
          if (response.data[i].保养图片) {
            this.urls.push({
              url: 'data:image/png;base64,' + response.data[i].保养图片,
              id: response.data[i].id
            })
            this.urls1.push('data:image/png;base64,' + response.data[i].保养图片)
          }
        }
      })
    },
    searchOrder1() {
      this.tableData1 = []
      let stationNumberValue_check, engineTypeNumber_check, engineTypeValue_check
      this.stationNumberValue1 ? (stationNumberValue_check = 1) : (stationNumberValue_check = 0)
      this.engineTypeNumber1 ? (engineTypeNumber_check = 1) : (engineTypeNumber_check = 0)
      this.engineTypeValue ? (engineTypeValue_check = 1) : (engineTypeValue_check = 0)
      var param = []
      param[0] = ['工位号_ischeck', stationNumberValue_check]
      param[1] = ['工位号', this.stationNumberValue1]
      param[2] = ['设备编号_ischeck', engineTypeNumber_check]
      param[3] = ['设备编号', this.engineTypeNumber1]
      param[4] = ['设备类型_ischeck', engineTypeValue_check]
      param[5] = ['设备类型', this.engineTypeValue]
      param[6] = ['PageCurrent', this.pageCurrent1]
      param[7] = ['PageSize', this.pageSize1]
      param[8] = ['PageCount', '1111', 'int', '1']
      param[9] = ['ItemCount', '1111', 'int', '1']
      var Data = this.CreateData('11', '设备管理_基础表_设备台账_综合查询', param)
      this.ExecDatabase(Data).then(response => {
        console.log(response, 66)
        if (response.data.result.length !== 0) {
          this.tableData1 = response.data.result
        }
        this.total1 = parseInt(response.data.output[0].ItemCount)
      })
    },
    searchEquipment() {
      this.searchOrder1()
      this.dialogFormVisible1 = true
    },
    selectSubmit() {
      if (this.maintenanceName && this.maintenanceNumber) {
        this.form.partName = this.maintenanceName[0]
        this.form.orderNumber = this.maintenanceNumber[0]
        this.form.MID = this.mid[0]
        this.dialogFormVisible1 = false
      } else {
        this.$message.error('请选择设备后提交！')
      }
    },
    // 创建订单
    creatOrder(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.title = '增加'
      this.form.partName = ''
      this.form.orderNumber = ''
      this.form.serialNumber = ''
      this.maintenanceContents = ''
      this.dialogFormVisible = true
    },
    // 提交新增订单
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['工位号', this.stationNumberValue]
          param[1] = ['保养类型代码', this.maintenanceTypeValue]
          param[2] = ['序号', this.form.serialNumber]
          param[3] = ['设备编号', this.form.orderNumber]
          param[4] = ['设备维护内容', this.form.maintenanceContents]
          // console.log(param, 'param')
          var Data = this.CreateData('12', '设备管理_设备维护内容_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogFormVisible = false
              this.searchOrder()
            } else {
              this.$message.error('增加失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    // 取消新增订单
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 删除订单
    deleteOrder(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['维护计划流水号', row.维护计划流水号]
        var Data = this.CreateData('12', '设备管理_设备维护内容_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchOrder()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 修改订单
    handleEdit(row) {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.title = '编辑'
      this.form.partName = row.机床名称
      this.form.orderNumber = row.设备编号
      this.form.stationNumberValue = row.工位号
      this.form.maintenanceTypeValue = Number(row.保养类型代码)
      this.form.serialNumber = Number(row.序号) // 序号
      this.form.maintenanceContents = row.设备维护内容 // 维修内容
      this.form.ID = row.维护计划流水号
      // this.form.MID = row.设备流水号
      this.dialogFormVisible = true
    },
    // 提交修改订单
    submitAdd1() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['维护计划流水号', this.form.ID]
          param[1] = ['工位号', this.form.stationNumberValue]
          param[2] = ['保养类型代码', this.form.maintenanceTypeValue]
          param[3] = ['序号', this.form.serialNumber]
          param[4] = ['设备维护内容', this.form.maintenanceContents]
          param[5] = ['设备编号', this.form.orderNumber]
          var Data = this.CreateData('12', '设备管理_设备维护内容_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.searchOrder()
            } else {
              this.$message.error('修改失败！')
            }
          })
          this.dialogFormVisible = false
        } else {
          return false
        }
      })
    },
    uploadPictures() {
      this.dialogFormVisible2 = true
    },
    handelLimit() {
      console.log(12345678)
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === '') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'none'
        }
      }
    },
    handelLimitStyle() {
      console.log(12345678)
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === 'none') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'block'
        }
      }
    },
    warningExceed() {
      this.$message.error('仅可上传一张照片')
    },
    beforeUpload(file) { // 上传前检测
      const isJPG = /^image\/.*/.test(file.type)
      if (!isJPG) {
        this.$message.error('只能上传图片。。')
      }
      return isJPG
    },
    uploadSuccess(res) { // 上传成功回调
      this.dialogFormVisible = false
      this.$refs.upload.clearFiles()
      var obj = document.getElementsByClassName('el-upload--picture-card')
      if (obj[0].style.display === 'none') {
        for (var i = 0; i < obj.length; i++) {
          obj[i].style.display = 'block'
        }
      }
    },
    updatePicture(row) {
      this.dialogFormVisible = true
      // this.purchaseContractID = row.采购合同流水号
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisiblePictureCard = true
    },
    submitPicture() {
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['保养类型代码', this.maintenanceTypeValue]
      param[2] = ['文件名称', '']
      param[3] = ['文件后缀', '']
      param[4] = ['文件内容', null]
      console.log(param)
      var Data1 = this.CreateData('15', '设备管理_设备维护图片_增加新', param)
      this.Data.param = Data1
      this.$refs.upload.submit()
      this.dialogFormVisible2 = false
      setTimeout(() => { this.searchTable() }, 2000)
    },
    // 表格颜色变化
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, rowIndex)
      if (rowIndex % 2 === 0) {
        return 'black'
      } else {
        return 'red'
      }
    },
    // 分页
    handleSizeChange1(val) {
      this.pageCurrent1 = 1
      this.pageSize1 = val
      this.searchOrder1()
    },
    handleCurrentChange1(val) {
      this.pageCurrent1 = val
      this.searchOrder1()
    }
  }
}
</script>

<style scoped>
  .topCard{
    margin: 5px;
  }
  .topDiv{
    margin-bottom: 10px;
  }
  .el-select{
    width:200px
  }
  .el-input{
    width:200px
  }
  .text {
    font-size: 14px;
  }

  .item {
    padding: 18px 0;
  }

  .box-card {
    position: absolute;
    right: 6px;
    top: 80px;
    width: 300px;
  }

  ::v-deep.el-input.is-disabled .el-input__inner{
    color: #606266!important;
  }
  ::v-deep.el-input.is-disabled .el-input__inner{
    background-color: white!important;
  }
</style>

