<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: 250px" clearable @change="searchData">
          <el-option
            v-for="item in stationNumber"
            :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="searchData()">查询</el-button>
        <el-button type="primary" size="small" icon="el-icon-circle-plus" @click="createData('form')">创建</el-button>
      </div>
      <el-table
        v-loading="loading"
        ref="multipleTable"
        :data="tableData"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        element-loading-text="数据加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(0, 0, 0, 0.2)"
        class="main"
        tooltip-effect="dark"
        height="640px"
        style="width: 100%"
        border
        size="mini">
        <el-table-column type="index" width="60" label="序号" align="center"/>
        <el-table-column align="center" label="工位号" width="150px">
          <template slot-scope="scope">
            {{ scope.row.工位号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="排序" width="100px">
          <template slot-scope="scope">
            {{ scope.row.序号 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="内容" min-width="200px">
          <template slot-scope="scope">
            <span @click="showContentPreview(scope.row.内容)">{{ scope.row.内容 }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作时间" width="180px">
          <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"
              style="color:#ff0000;"
              @click="deleteData(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 创建对话框 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible" title="创建4M信息" center style="min-height: 200px;" width="900px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
              <el-select v-model="form.stationNumberValue" size="small" placeholder="请选择工位号" style="width: 400px">
                <el-option
                  v-for="item in stationNumber"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序" prop="orderNum" style="display: inline-block">
              <el-input-number
                v-model="form.orderNum"
                :min="1"
                :step="1"
                size="small"
                style="width: 400px"
                placeholder="请输入排序号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content" style="display: inline-block">
              <el-input
                v-model="form.content"
                type="textarea"
                :rows="12"
                clearable
                size="small"
                style="width: 700px; font-family: Consolas, monospace;"
                placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff('form')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAdd('form')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog v-el-drag-dialog :visible.sync="dialogFormVisible1" title="编辑4M信息" center style="min-height: 200px;" width="900px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-position="right" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="工位号" prop="stationNumberValue" style="display: inline-block">
              <el-input
                v-model="form1.stationNumberValue"
                readonly
                size="small"
                style="width: 400px"
                placeholder="工位号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="排序" prop="orderNum" style="display: inline-block">
              <el-input-number
                v-model="form1.orderNum"
                :min="1"
                :step="1"
                size="small"
                style="width: 400px"
                placeholder="请输入排序号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="内容" prop="content" style="display: inline-block">
              <el-input
                v-model="form1.content"
                type="textarea"
                :rows="12"
                clearable
                size="small"
                style="width: 700px; font-family: Consolas, monospace;"
                placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff1('form1')">取消</el-button>
        <el-button type="primary" size="small" @click="submitEdit('form1')">确定</el-button>
      </div>
    </el-dialog>
    <!-- 内容预览弹窗 -->
    <el-dialog :visible.sync="previewDialogVisible" title="内容预览" fullscreen>
      <div class="preview-container">
        <pre class="preview-content">{{ previewContent }}</pre>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" @click="previewDialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'FourMTextManagement',
  data() {
    return {
      stationNumber: [],
      stationNumberValue: '',
      tableData: [],
      dialogFormVisible: false,
      dialogFormVisible1: false,
      previewDialogVisible: false,
      previewContent: '',
      loading: false,
      form: {
        stationNumberValue: '',
        orderNum: 1,
        content: ''
      },
      rules: {
        stationNumberValue: [{ required: true, message: '请选择工位号', trigger: 'change' }],
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      form1: {
        ID: '',
        stationNumberValue: '',
        orderNum: 1,
        content: ''
      },
      rules1: {
        orderNum: [{ required: true, message: '请输入排序号', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getStationNumber()
    setTimeout(() => {
      this.searchData()
    }, 1000)
  },
  methods: {
    // 获取工位号列表
    getStationNumber() {
      this.stationNumber = []
      var param = []
      var Data = this.CreateData('11', '工位与名称_视图_查询', param)
      this.ExecDatabase(Data).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.stationNumber.push({
            label: `【${response.data[i].工位号}】${response.data[i].工位名称}`,
            value: response.data[i].工位号,
            labelName: response.data[i].工位名称
          })
        }
      })
    },
    // 查询数据
    searchData() {
      this.loading = true
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      var Data = this.CreateData('11', 'MES_4M管理_查询', param)
      this.ExecDatabase(Data).then(response => {
        this.tableData = []
        if (response.data.length !== 0) {
          this.tableData = response.data
        }
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    // 创建数据
    createData(formName) {
      if (this.$refs[formName] !== undefined) {
        this.$refs[formName].resetFields()
      }
      this.form.stationNumberValue = ''
      this.form.orderNum = 1
      this.form.content = ''
      this.dialogFormVisible = true
    },
    // 提交新增
    submitAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['序号', this.form.orderNum]
          param[1] = ['工位号', this.form.stationNumberValue]
          param[2] = ['内容', this.form.content]
          var Data = this.CreateData('12', 'MES_4M管理_增加', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0]['result'] === '1') {
              this.$message({
                message: '4M信息增加成功!',
                type: 'success'
              })
              // 弹窗不关闭,序号+1,内容清空,工位号保留
              this.form.orderNum = this.form.orderNum + 1
              this.form.content = ''
              // 自动切换查询工位号到提交的工位并查询
              this.stationNumberValue = this.form.stationNumberValue
              this.searchData()
            } else {
              this.$message.error('4M信息增加失败!')
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消新增
    callOff(formName) {
      this.dialogFormVisible = false
      this.$refs[formName].resetFields()
    },
    // 修改数据
    handleEdit(row) {
      if (this.$refs['form1'] !== undefined) {
        this.$refs['form1'].resetFields()
      }
      this.form1.ID = row.ID
      this.form1.stationNumberValue = row.工位号
      this.form1.orderNum = row.序号
      this.form1.content = row.内容
      this.dialogFormVisible1 = true
    },
    // 提交修改
    submitEdit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['ID', this.form1.ID]
          param[1] = ['序号', this.form1.orderNum]
          param[2] = ['工位号', this.form1.stationNumberValue]
          param[3] = ['内容', this.form1.content]
          var Data = this.CreateData('12', 'MES_4M管理_修改', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0]['result'] === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogFormVisible1 = false
              this.searchData()
            } else {
              this.$message.error('修改失败!')
            }
          })
        } else {
          return false
        }
      })
    },
    // 取消修改
    callOff1(formName) {
      this.$refs[formName].resetFields()
      this.dialogFormVisible1 = false
    },
    // 删除数据
    deleteData(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['ID', row.ID]
        var Data = this.CreateData('12', 'MES_4M管理_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0]['result'] === '1') {
            this.$message.success('删除成功')
            this.searchData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 显示内容预览
    showContentPreview(content) {
      this.previewContent = content
      this.previewDialogVisible = true
    }
  }
}
</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;
  }

  .el-table .warning-row {
    background: oldlace;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  ::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;
  }

  /* 预览弹窗样式 */
  .preview-container {
    height: calc(100vh - 160px);
    overflow: auto;
    background-color: #1e1e1e;
    border-radius: 4px;
    padding: 20px;
  }
  .preview-content {
    font-family: Consolas, 'Courier New', monospace;
    font-size: 14px;
    line-height: 1.6;
    color: #d4d4d4;
    white-space: pre-wrap;
    word-wrap: break-word;
    margin: 0;
  }
</style>
