<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="告警级别" prop="warningLevel">
        <el-select v-model="queryParams.warningLevel" placeholder="请选择告警级别" clearable @keyup.enter.native="handleQuery">
          <el-option label="警告" value="WARNING" />
          <el-option label="错误" value="ERROR" />
          <el-option label="严重错误" value="CRITICAL" />
        </el-select>
      </el-form-item>
      <el-form-item label="告警状态" prop="warningStatus">
        <el-select v-model="queryParams.warningStatus" placeholder="请选择告警状态" clearable
          @keyup.enter.native="handleQuery">
          <el-option label="未处理" value="0" />
          <el-option label="已处理" value="1" />
          <el-option label="已忽略" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="处理人" prop="handleBy">
        <el-input v-model="queryParams.handleBy" placeholder="请输入处理人" clearable @keyup.enter.native="handleQuery" />
      </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-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="materialWarningList" @selection-change="handleSelectionChange">
      <!-- <el-table-column label="钢卷ID" align="center" prop="coilId" /> -->
      <el-table-column label="告警类型" align="center" prop="warningType">
        <template slot-scope="scope">
          <span>{{ scope.row.warningType === 'LENGTH' ? '长度' : scope.row.warningType === 'THICKNESS' ? '厚度' : '宽度'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="理论值" align="center" prop="theoreticalVal" />
      <el-table-column label="实测值" align="center" prop="actualVal" />
      <el-table-column label="允许偏差" align="center" prop="allowDeviation" />
      <el-table-column label="实际偏差值" align="center" prop="deviationValue" />
      <el-table-column label="偏差率(%)" align="center" prop="deviationRate" />
      <el-table-column label="告警级别" align="center" prop="warningLevel">
        <template slot-scope="scope">
          <span>{{ scope.row.warningLevel === 'WARNING' ? '警告' : scope.row.warningLevel === 'ERROR' ? '错误' : '严重错误'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="告警说明" align="center" prop="warningMsg" />
      <el-table-column label="告警状态" align="center" prop="warningStatus">
        <template slot-scope="scope">
          <span>{{ scope.row.warningStatus == 0 ? '未处理' : scope.row.warningStatus == 1 ? '已处理' : '已忽略' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理人" align="center" prop="handleBy" />
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="处理备注" align="center" prop="handleRemark" /> -->
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleHandle(scope.row)">处理</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleIgnore(scope.row)">忽略</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改钢卷告警对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="钢卷ID" prop="coilId">
          <el-input v-model="form.coilId" placeholder="请输入钢卷ID" />
        </el-form-item>
        <el-form-item label="理论值" prop="theoreticalVal">
          <el-input v-model="form.theoreticalVal" placeholder="请输入理论值" />
        </el-form-item>
        <el-form-item label="实测值" prop="actualVal">
          <el-input v-model="form.actualVal" placeholder="请输入实测值" />
        </el-form-item>
        <el-form-item label="允许偏差" prop="allowDeviation">
          <el-input v-model="form.allowDeviation" placeholder="请输入允许偏差" />
        </el-form-item>
        <el-form-item label="实际偏差值" prop="deviationValue">
          <el-input v-model="form.deviationValue" placeholder="请输入实际偏差值" />
        </el-form-item>
        <el-form-item label="偏差率(%)" prop="deviationRate">
          <el-input v-model="form.deviationRate" placeholder="请输入偏差率(%)" />
        </el-form-item>
        <el-form-item label="告警级别(WARNING/ERROR/CRITICAL)" prop="warningLevel">
          <el-input v-model="form.warningLevel" placeholder="请输入告警级别(WARNING/ERROR/CRITICAL)" />
        </el-form-item>
        <el-form-item label="告警说明" prop="warningMsg">
          <el-input v-model="form.warningMsg" placeholder="请输入告警说明" />
        </el-form-item>
        <el-form-item label="处理人" prop="handleBy">
          <el-input v-model="form.handleBy" placeholder="请输入处理人" />
        </el-form-item>
        <el-form-item label="处理时间" prop="handleTime">
          <el-date-picker clearable
            v-model="form.handleTime"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="处理备注" prop="handleRemark">
          <el-input v-model="form.handleRemark" placeholder="请输入处理备注" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { listMaterialWarning, getMaterialWarning, delMaterialWarning, addMaterialWarning, updateMaterialWarning } from "@/api/wms/materialWarning";

export default {
  name: "MaterialWarning",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 钢卷告警表格数据
      materialWarningList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coilId: undefined,
        warningType: undefined,
        theoreticalVal: undefined,
        actualVal: undefined,
        allowDeviation: undefined,
        deviationValue: undefined,
        deviationRate: undefined,
        warningLevel: undefined,
        warningMsg: undefined,
        warningStatus: undefined,
        handleBy: undefined,
        handleTime: undefined,
        handleRemark: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        warningId: [
          { required: true, message: "告警ID不能为空", trigger: "blur" }
        ],
        coilId: [
          { required: true, message: "钢卷ID不能为空", trigger: "blur" }
        ],
        warningType: [
          { required: true, message: "告警类型(LENGTH=长度, THICKNESS=厚度, WIDTH=宽度)不能为空", trigger: "change" }
        ],
        theoreticalVal: [
          { required: true, message: "理论值不能为空", trigger: "blur" }
        ],
        actualVal: [
          { required: true, message: "实测值不能为空", trigger: "blur" }
        ],
        allowDeviation: [
          { required: true, message: "允许偏差(数值/百分比)不能为空", trigger: "blur" }
        ],
        deviationValue: [
          { required: true, message: "实际偏差值不能为空", trigger: "blur" }
        ],
        deviationRate: [
          { required: true, message: "偏差率(%)不能为空", trigger: "blur" }
        ],
        warningLevel: [
          { required: true, message: "告警级别(WARNING/ERROR/CRITICAL)不能为空", trigger: "blur" }
        ],
        warningMsg: [
          { required: true, message: "告警说明不能为空", trigger: "blur" }
        ],
        warningStatus: [
          { required: true, message: "告警状态(0=未处理,1=已处理,2=已忽略)不能为空", trigger: "change" }
        ],
        handleBy: [
          { required: true, message: "处理人不能为空", trigger: "blur" }
        ],
        handleTime: [
          { required: true, message: "处理时间不能为空", trigger: "blur" }
        ],
        handleRemark: [
          { required: true, message: "处理备注不能为空", trigger: "blur" }
        ],
        delFlag: [
          { required: true, message: "删除标志不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询钢卷告警列表 */
    getList() {
      this.loading = true;
      listMaterialWarning(this.queryParams).then(response => {
        this.materialWarningList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 忽略按钮操作 */
    handleIgnore(row) {
      this.loading = true;
      updateMaterialWarning({ ...row, warningStatus: 2 }).then(response => {
        this.loading = false;
        this.$message({
          message: "忽略成功",
          type: "success"
        });
        this.getList();
      });
    },
    /** 处理按钮操作 */
    handleHandle(row) {
      this.loading = true;
      updateMaterialWarning({ ...row, warningStatus: 1 }).then(response => {
        this.loading = false;
        this.$message({
          message: "处理成功",
          type: "success"
        });
        this.getList();
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warningId: undefined,
        coilId: undefined,
        warningType: undefined,
        theoreticalVal: undefined,
        actualVal: undefined,
        allowDeviation: undefined,
        deviationValue: undefined,
        deviationRate: undefined,
        warningLevel: undefined,
        warningMsg: undefined,
        warningStatus: undefined,
        handleBy: undefined,
        handleTime: undefined,
        handleRemark: undefined,
        delFlag: undefined,
        createTime: undefined,
        createBy: undefined,
        updateTime: undefined,
        updateBy: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warningId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加钢卷告警";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const warningId = row.warningId || this.ids
      getMaterialWarning(warningId).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改钢卷告警";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.warningId != null) {
            updateMaterialWarning(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addMaterialWarning(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warningIds = row.warningId || this.ids;
      this.$modal.confirm('是否确认删除钢卷告警编号为"' + warningIds + '"的数据项？').then(() => {
        this.loading = true;
        return delMaterialWarning(warningIds);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wms/materialWarning/export', {
        ...this.queryParams
      }, `materialWarning_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
