<template>
  <div class="app-container">
    <el-card class="topCard">
      <div class="topDiv">
        <el-col :span="8">
          <el-button type="warning" icon="el-icon-circle-plus-outline" size="small" @click="addToScheme">新增方案</el-button>
        </el-col>
        <el-col :span="16">
          <el-button :disabled="currentRow === null" type="primary" icon="el-icon-circle-plus-outline" size="small" style="margin-left: 10px" @click="addToSchemeItem">增加项目</el-button>
        </el-col>
      </div>
      <div>
        <el-col :span="8">
          <el-table
            :data="tableDataScheme"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            height="680px"
            class="main"
            tooltip-effect="dark"
            style="width: 100%"
            border
            size="mini">
            <el-table-column width="40">
              <template slot-scope="scope">
                <el-radio
                  :label="scope.row"
                  v-model="currentRow"
                  class="radio"
                  @input="changeValue"
                >&emsp;&emsp;&emsp;</el-radio>
              </template>
            </el-table-column>
            <el-table-column type="index" label="序号" align="center" width="60"/>
            <el-table-column label="方案名称" align="center" width="100px">
              <template slot-scope="scope">
                {{ scope.row.方案名称 }}
              </template>
            </el-table-column>
            <el-table-column label="方案版本" align="center">
              <template slot-scope="scope">
                {{ scope.row.方案版本 }}
              </template>
            </el-table-column>
            <el-table-column label="备注" align="center" width="120px">
              <template slot-scope="scope">
                <div style="white-space: pre-line !important;">{{ scope.row.备注 }}</div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" @click="editToScheme(scope.row)"><i class="el-icon-edit"/></el-button>
                <el-button type="text" style="color: #ff0000" @click="deleteSchemeInformation(scope.row)"><i class="el-icon-delete"/></el-button>
              </template>
            </el-table-column>
          </el-table>

        </el-col>
        <el-col :span="16">
          <el-table
            :data="tableDataSchemeItems"
            :header-cell-style="{background:'#eef1f6',color:'#606266'}"
            class="main"
            tooltip-effect="dark"
            style="margin-left: 10px"
            border
            height="680px"
            size="mini">
            <el-table-column label="序号" align="center" width="100px">
              <template slot-scope="scope">
                {{ scope.row.序号 }}
              </template>
            </el-table-column>
            <el-table-column label="工位号" align="center" width="100px">
              <template slot-scope="scope">
                {{ scope.row.工位号 }}
              </template>
            </el-table-column>
            <el-table-column label="项目类型" align="center" width="100px">
              <template slot-scope="scope">
                <el-tag v-if="scope.row.项目类型 === 0" size="mini" type="success">基础项目</el-tag>
                <el-tag v-if="scope.row.项目类型 === 1" size="mini" type="warning">测试项目</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="类别" align="center">
              <template slot-scope="scope">
                {{ scope.row.类别 }}
              </template>
            </el-table-column>
            <el-table-column label="测试方法" align="center">
              <template slot-scope="scope">
                {{ scope.row.测试方法 }}
              </template>
            </el-table-column>
            <el-table-column label="检测标准" align="center">
              <template slot-scope="scope">
                {{ scope.row.检测标准 }}
              </template>
            </el-table-column>
            <el-table-column label="实测值" align="center">
              <template slot-scope="scope">
                {{ scope.row.实测值 }}
              </template>
            </el-table-column>
            <el-table-column label="判定" align="center" width="150px">
              <template slot-scope="scope">
                {{ scope.row.判定 }}
              </template>
            </el-table-column>
            <el-table-column align="center" label="操作" width="100">
              <template slot-scope="scope">
                <el-button type="text" @click="editInformation(scope.row)"><i class="el-icon-edit"/></el-button>
                <el-button type="text" style="color: #ff0000" @click="deleteInformation(scope.row)"><i class="el-icon-delete"/></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </div>
    </el-card>
    <el-dialog :visible.sync="dialogSchemeFormVisible" title="新增出厂报告方案" center style="min-height: 200px;" width="400px">
      <el-form ref="formScheme" :model="formScheme" :rules="rulesScheme" label-position="right" label-width="110px">
        <el-form-item label="方案名称" prop="方案名称" style="display: inline-block">
          <el-input v-model="formScheme.方案名称" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="方案版本" prop="方案版本" style="display: inline-block">
          <el-input v-model="formScheme.方案版本" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="备注" prop="备注" style="display: inline-block">
          <el-input v-model="formScheme.备注" :rows="2" type="textarea" style="width:200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOffScheme('formScheme')">取消</el-button>
        <el-button type="primary" size="small" @click="submitAddScheme('formScheme')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogSchemeFormEditVisible" title="编辑出厂报告方案" center style="min-height: 200px;" width="400px">
      <el-form ref="formSchemeEdit" :model="formSchemeEdit" :rules="rulesSchemeEdit" label-position="right" label-width="110px">
        <el-form-item label="方案名称" prop="方案名称" style="display: inline-block">
          <el-input v-model="formSchemeEdit.方案名称" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="方案版本" prop="方案版本" style="display: inline-block">
          <el-input v-model="formSchemeEdit.方案版本" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="备注" prop="备注" style="display: inline-block">
          <el-input v-model="formSchemeEdit.备注" :rows="2" type="textarea" style="width:200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOffScheme('formSchemeEdit')">取消</el-button>
        <el-button type="primary" size="small" @click="submitEditScheme('formSchemeEdit')">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogSchemeItemFormVisible" title="新增出厂报告方案项目" center style="min-height: 200px;" width="800px">
      <el-row>
        <div class="topDiv">
          <span style="margin-left: 10px">工位号：</span>
          <el-select v-model="stationNumberValue" placeholder="请选择工位号" size="small" style="width: 150px" @change="searchTableSchemeItem">
            <el-option
              v-for="item in stationNumber"
              :key="item.value"
              :label="item.label"
              :value="item.value"
              size="mini"/>
          </el-select>
          <el-radio-group v-model="dataType" style="margin-left: 10px" @change="searchTableSchemeItem">
            <el-radio :label="0">基础项目</el-radio>
            <el-radio :label="1">测试项目</el-radio>
          </el-radio-group>
        </div>
        <el-table
          v-if="dataType === 0"
          ref="multipleTable1"
          :data="tableDataBase"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          height="380px"
          highlight-current-row
          class="main"
          tooltip-effect="dark"
          style="width: 100%"
          border
          size="mini"
          @current-change="handleCurrentChange">
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column align="center" label="工位号" width="80">
            <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="100">
            <template slot-scope="scope">
              {{ scope.row.测试方法 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="检测标准" width="100">
            <template slot-scope="scope">
              {{ scope.row.检测要求 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="实测值" width="100">
            <template slot-scope="scope">
              {{ scope.row.实测值 }}
            </template>
          </el-table-column>
        </el-table>
        <el-table
          v-if="dataType === 1"
          ref="multipleTable2"
          :data="tableDataBase"
          :header-cell-style="{background:'#eef1f6',color:'#606266'}"
          height="380px"
          highlight-current-row
          class="main"
          tooltip-effect="dark"
          style="width: 100%"
          border
          size="mini"
          @current-change="handleCurrentChange">
          <el-table-column type="index" label="序号" width="50" />
          <el-table-column align="center" label="工位号" width="80">
            <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="100">
            <template slot-scope="scope">
              {{ scope.row.测试方法 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="检测标准" width="100">
            <template slot-scope="scope">
              {{ scope.row.检测要求 }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="实测值" width="100">
            <template slot-scope="scope">
              <span>******</span>
              <!--              {{ scope.row.实测值 }}-->
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOffScheme()">关闭</el-button>
        <el-button :disabled="currentRowSelectItem === null" type="primary" size="small" @click="addSchemeItem()">增加</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogSchemeItemEditFormVisible" title="编辑出厂报告方案项目" center style="min-height: 200px;" width="400px">
      <el-form ref="formSchemeItemEdit" :model="formSchemeItemEdit" :rules="rulesSchemeItemEdit" label-position="right" label-width="110px">
        <el-form-item label="工位号" prop="工位号" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.工位号" disabled="disabled" size="small" readonly style="width:200px;"/>
        </el-form-item>
        <el-form-item label="类别" prop="类别" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.类别" disabled="disabled" size="small" readonly style="width:200px;"/>
        </el-form-item>
        <el-form-item label="序号" prop="序号" style="display: inline-block">
          <el-input-number v-model="formSchemeItemEdit.序号" :min="1" :step="1" size="small" style="width:200px;"/>
        </el-form-item>
        <el-form-item label="测试方法" prop="测试方法" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.测试方法" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="检测标准" prop="检测标准" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.检测标准" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="实测值" prop="实测值" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.实测值" :disabled="formSchemeItemEdit.项目类型 === 1" size="small" clearable style="width:200px;"/>
        </el-form-item>
        <el-form-item label="判定" prop="判定" style="display: inline-block">
          <el-input v-model="formSchemeItemEdit.判定" :disabled="formSchemeItemEdit.项目类型 === 1" size="small" clearable style="width:200px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOffScheme('formSchemeItemEdit')">取消</el-button>
        <el-button type="primary" size="small" @click="submitUpdate('formSchemeItemEdit')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      dataType: 0,
      stationNumberValue: '',
      stationNumber: [],
      currentRow: null,
      currentRowSelectItem: null,
      tableDataBase: [],
      tableDataScheme: [],
      tableDataSchemeItems: [],
      activeName: 'first',
      dialogSchemeItemEditFormVisible: false,
      dialogSchemeItemFormVisible: false,
      dialogSchemeFormVisible: false,
      dialogSchemeFormEditVisible: false,
      formScheme: {
        方案名称: '',
        方案版本: '',
        备注: ''
      },
      rulesScheme: {
        方案名称: [{ required: true, message: '请输入方案名称', trigger: 'blur' }],
        方案版本: [{ required: true, message: '请输入方案版本', trigger: 'blur' }]
      },
      formSchemeEdit: {
        ID: '',
        方案名称: '',
        方案版本: '',
        备注: ''
      },
      rulesSchemeEdit: {
        方案名称: [{ required: true, message: '请输入方案名称', trigger: 'blur' }],
        方案版本: [{ required: true, message: '请输入方案版本', trigger: 'blur' }]
      },
      formSchemeItemEdit: {
        ID: '',
        序号: '',
        项目类型: 0,
        工位号: '',
        类别: '',
        测试方法: '',
        判定: '',
        实测值: '',
        检测标准: ''
      },
      rulesSchemeItemEdit: {
        序号: [{ required: true, message: '请输入序号', trigger: 'blur' }],
        类别: [{ required: true, message: '请输入类别', trigger: 'blur' }],
        测试方法: [{ required: true, message: '请输入测试方法', trigger: 'blur' }],
        判定: [{ required: true, message: '请输入判定', trigger: 'blur' }],
        实测值: [{ required: true, message: '请输入实测值', trigger: 'blur' }],
        检测标准: [{ required: true, message: '请输入检测标准', trigger: 'blur' }]
      }
    }
  },
  computed: {
    ...mapGetters([
      'id',
      'token',
      'departmentId'
    ])
  },
  watch: {
  },
  created() {
    this.getStationNumber()
  },
  mounted() {
    this.searchTableScheme()
  },
  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].工位号,
            value: response.data[i].工位号
          })
        }
        this.stationNumberValue = response.data[0].工位号
      })
    },
    changeValue(value) {
      this.currentRow = value
      this.searchSchemeItem(this.currentRow)
    },
    searchTableScheme() {
      this.tableDataScheme = []
      var param = []
      var Data = this.CreateData('11', 'CATL_出厂报告_方案_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableDataScheme = response.data
        }
      })
    },
    addToScheme() {
      this.dialogSchemeFormVisible = true
    },
    submitAddScheme(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['方案名称', this.formScheme.方案名称]
          param[1] = ['方案版本', this.formScheme.方案版本]
          param[2] = ['备注', this.formScheme.备注]
          var Data = this.CreateData('12', 'CATL_出厂报告_方案_新增', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '增加成功',
                type: 'success'
              })
              this.dialogSchemeFormVisible = false
              this.searchTableScheme()
            } else {
              this.$message.error('增加失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    editToScheme(row) {
      this.formSchemeEdit.方案名称 = row['方案名称']
      this.formSchemeEdit.方案版本 = row['方案版本']
      this.formSchemeEdit.备注 = row['备注']
      this.formSchemeEdit.ID = row['ID']
      this.dialogSchemeFormEditVisible = true
    },
    submitEditScheme(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['方案名称', this.formSchemeEdit.方案名称]
          param[1] = ['方案版本', this.formSchemeEdit.方案版本]
          param[2] = ['备注', this.formSchemeEdit.备注]
          param[3] = ['id', this.formSchemeEdit.ID]
          var Data = this.CreateData('12', 'CATL_出厂报告_方案_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '编辑成功',
                type: 'success'
              })
              this.dialogSchemeFormEditVisible = false
              this.searchTableScheme()
            } else {
              this.$message.error('编辑失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    deleteSchemeInformation(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.ID]
        var Data = this.CreateData('12', 'CATL_出厂报告_方案_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchTableScheme()
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    callOffScheme(formName) {
      this.dialogSchemeFormVisible = false
      this.dialogSchemeFormEditVisible = false
      this.dialogSchemeItemFormVisible = false
      this.dialogSchemeItemEditFormVisible = false
      if (formName) {
        this.$refs[formName].resetFields()
      }
    },
    searchSchemeItem(row) {
      this.tableDataSchemeItems = []
      var param = []
      param[0] = ['方案id', row['ID']]
      var Data = this.CreateData('11', 'CATL_出厂报告_方案_项目_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableDataSchemeItems = response.data
        }
      })
    },
    addToSchemeItem() {
      this.dataType = 0
      this.stationNumberValue = this.stationNumber[0].value
      this.searchTableSchemeItem()
      this.dialogSchemeItemFormVisible = true
    },
    // 查询信息
    searchTableSchemeItem() {
      this.currentRowSelectItem = null
      this.tableDataBase = []
      var param = []
      param[0] = ['工位号', this.stationNumberValue]
      param[1] = ['查询类型', this.dataType]
      var Data = this.CreateData('11', 'CATL_出厂报告_方案_基础项目_综合_查询', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data.length !== 0) {
          this.tableDataBase = response.data
        }
      })
    },
    handleCurrentChange(val) {
      this.currentRowSelectItem = val
    },
    addSchemeItem() {
      var param = []
      param[0] = ['方案id', this.currentRow['ID']]
      param[1] = ['项目类型', this.dataType]
      param[2] = ['类别', this.currentRowSelectItem['类别']]
      param[3] = ['测试方法', this.currentRowSelectItem['测试方法']]
      param[4] = ['检测标准', this.currentRowSelectItem['检测要求']]
      param[5] = ['实测值', this.currentRowSelectItem['实测值']]
      param[6] = ['判定', this.currentRowSelectItem['判定']]
      param[7] = ['工位号', this.currentRowSelectItem['工位号']]
      var Data = this.CreateData('12', 'CATL_出厂报告_方案_项目_新增', param)
      this.ExecDatabase(Data).then(response => {
        if (response.data[0].result === '1') {
          this.$message({
            message: '编辑成功',
            type: 'success'
          })
        }
        this.searchSchemeItem(this.currentRow)
      })
    },
    editInformation(row) {
      this.formSchemeItemEdit.ID = row.ID
      this.formSchemeItemEdit.序号 = row.序号
      this.formSchemeItemEdit.类别 = row.类别
      this.formSchemeItemEdit.工位号 = row.工位号
      this.formSchemeItemEdit.项目类型 = row.项目类型
      this.formSchemeItemEdit.测试方法 = row.测试方法
      this.formSchemeItemEdit.检测标准 = row.检测标准
      this.formSchemeItemEdit.判定 = row.判定
      this.formSchemeItemEdit.实测值 = row.实测值
      this.dialogSchemeItemEditFormVisible = true
    },
    // 编辑消息
    submitUpdate(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var param = []
          param[0] = ['类别', this.formSchemeItemEdit.类别]
          param[1] = ['测试方法', this.formSchemeItemEdit.测试方法]
          param[2] = ['检测标准', this.formSchemeItemEdit.检测标准]
          param[3] = ['实测值', this.formSchemeItemEdit.实测值]
          param[4] = ['id', this.formSchemeItemEdit.ID]
          param[5] = ['序号', this.formSchemeItemEdit.序号]
          param[6] = ['判定', this.formSchemeItemEdit.判定]
          var Data = this.CreateData('12', 'CATL_出厂报告_方案_项目_编辑', param)
          this.ExecDatabase(Data).then(response => {
            if (response.data[0].result === '1') {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.dialogSchemeItemEditFormVisible = false
              this.searchSchemeItem(this.currentRow)
            } else {
              this.$message.error('修改失败！')
            }
          })
        } else {
          return false
        }
      })
    },
    deleteInformation(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var param = []
        param[0] = ['id', row.ID]
        var Data = this.CreateData('12', 'CATL_出厂报告_方案_项目_删除', param)
        this.ExecDatabase(Data).then(response => {
          if (response.data[0].result === '1') {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.searchSchemeItem(this.currentRow)
          } else {
            this.$message.error('删除失败！')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</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;
}

.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 textarea {
  height: 100px;
}
</style>

