<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="faultType">
        <el-input
          v-model="queryParams.faultType"
          placeholder="请输入车辆故障类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障VIN编码" prop="carVin">
        <el-input
          v-model="queryParams.carVin"
          placeholder="请输入故障VIN编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障级别" prop="faultWarn">
        <el-input
          v-model="queryParams.faultWarn"
          placeholder="请输入故障级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障处理状态" prop="state">
        <el-select v-model="queryParams.state" placeholder="请选择故障处理状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_fault_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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 @click="drawer = true" class="share-button" style="margin-top: 10px" icon="el-icon-s-comment" type="primary"></el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['fault:faultInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fault:faultInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fault:faultInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['fault:faultInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="车辆故障编码;" align="center" prop="faultCode" />
      <el-table-column label="车辆故障类型" align="center" prop="faultTypeId"/>
      <el-table-column label="车辆故障规则" align="center" prop="faultRuleId"/>
      <el-table-column label="车辆故障位" align="center" prop="faultBit" />
      <el-table-column label="车辆故障值" align="center" prop="faultValue" />
      <el-table-column label="故障描述信息" align="center" prop="faultDesc" />
      <el-table-column label="是否报警" align="center" prop="faultWarn" >
        <template slot-scope="scope">
          <el-col style="font-size: 1px" v-if="(scope.row.faultWarn==1)">
            <el-result icon="error" />
          </el-col>
        </template>
      </el-table-column>
      <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="handleUpdate(scope.row)"
            v-hasPermi="['fault:faultInfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fault:faultInfo:remove']"
          >删除</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="车辆故障编码;" prop="faultCode">
          <el-input v-model="form.faultCode" placeholder="请输入车辆故障编码;" />
        </el-form-item>
        <el-form-item label="车辆故障类型" prop="faultType">
          <el-input v-model="form.faultCode" placeholder="请输入车辆类型;" />
        </el-form-item>
        <el-form-item label="故障VIN编码" prop="carVin">
          <el-input v-model="form.carVin" placeholder="请输入故障VIN编码" />
        </el-form-item>
        <el-form-item label="车辆故障标签" prop="faultLabel">
          <el-input v-model="form.faultLabel" placeholder="请输入车辆故障标签" />
        </el-form-item>
        <el-form-item label="车辆故障位" prop="faultBit">
          <el-input v-model="form.faultBit" placeholder="请输入车辆故障位" />
        </el-form-item>
        <el-form-item label="车辆故障值" prop="faultValue">
          <el-input v-model="form.faultValue" placeholder="请输入车辆故障值" />
        </el-form-item>
        <el-form-item label="故障级别" prop="faultWarn">
          <el-input v-model="form.faultWarn" placeholder="请输入故障级别" />
        </el-form-item>
        <el-form-item label="报警状态" prop="warnStatus">
          <el-select v-model="form.warnStatus" placeholder="请选择报警状态">
            <el-option
              v-for="dict in dict.type.sys_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="故障描述信息" prop="faultDesc">
          <el-input v-model="form.faultDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="故障处理状态" prop="state">
          <el-select v-model="form.state" placeholder="请选择故障处理状态">
            <el-option
              v-for="dict in dict.type.sys_fault_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

<!--    消息-->
    <el-drawer
      title="最近消息"
      :visible.sync="drawer"
      :with-header="false">
        <el-button :plain="true" @click="open1" v-if="buttonVisible" style="background-color: skyblue;color: white;border: none">全部</el-button>
        <el-button :plain="true" @click="open2" style="background-color: skyblue;color: white;border: none">未读</el-button>
        <el-button :plain="true" @click="open3" style="background-color: skyblue;color: white;border: none">已读</el-button>
      <el-table
        :data="tableData"
        style="width: 100%;">
        <el-table-column label="ID" width="180" v-if="showField"><template slot-scope="scope" ><span style="margin-left: 10px"  v-if="showField">{{ scope.row.faultMessageId }}</span></template></el-table-column>
        <el-table-column label="发送人" width="180"><template slot-scope="scope"><span style="margin-left: 10px">{{ scope.row.faultMessageSendName }}</span></template></el-table-column>
        <el-table-column label="内容" width="180"><template slot-scope="scope"><span style="margin-left: 10px">{{ scope.row.faultMessageContent }}</span></template></el-table-column>
        <el-table-column label="日期" width="180"><template slot-scope="scope"><span style="margin-left: 10px">{{ scope.row.faultMessageSendTime }}</span></template></el-table-column>
        <el-table-column label="状态" width="180" v-if="showField"><template slot-scope="scope"><span style="margin-left: 10px" v-if="showField">{{ scope.row.faultMessageState }}</span></template></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              style="background-color: yellowgreen;color: white;border: none"
              @click="handleEdit(scope.row)" v-if="scope.row.faultMessageState==1">已读</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDel(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
<!--    消息-->
  </div>
</template>

<script>
import {
  listFaultInfo,
  getFaultInfo,
  delFaultInfo,
  addFaultInfo,
  updateFaultInfo,
  faultTypeDown
} from '../../../../api/fault/fault'
import {
  deleteMessageId,
  faultMessageList,
  faultMessageOne,
  faultMessageTwo,
  MessageList,
  updateTwo
} from '../../../../api/fault/message'

export default {
  name: "FaultInfo",
  dicts: ['sys_fault_status', 'sys_common_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 车辆故障表格数据
      faultInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //站内信
      faultMessage:{},
      //故障类型
      faultType:{},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faultName: null,
        faultType: null,
        carVin: null,
        faultWarn: null,
        state: null,
      },
      tableData:[],
      drawer: false,
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        faultCode: [
          { required: true, message: "车辆故障编码;不能为空", trigger: "blur" }
        ],
      },
      buttonVisible: true // 控制按钮的显示
    };
  },
  created() {
    this.getList();
    this.open1();
  },
  methods: {
    // typeDown(){
    //   faultTypeDown().then(response=>{
    //     this.faultType=response.data
    //   })
    // },
    open1() {
      faultMessageList(this.faultMessage).then(response=>{
        this.tableData=response.data
      })
    },
    open2() {
      faultMessageOne().then(response => {
        console.log(response);
        this.tableData = response.data;
        this.$message({
          message: '未读消息',
          type: 'success'
        })
      });
    },
    open3() {
      faultMessageTwo().then(response=>{
        console.log(response);
        this.tableData = response.data;
        this.$message({
          message: '已读消息',
          type: 'warning'
        })
      })
    },
    handleDel(row){
      deleteMessageId(row.faultMessageId).then(response=>{
        this.$message({
          message: '删除成功',
          type: 'warning'
        })
      })
    },
    handleEdit(row){
      updateTwo(row.faultMessageId).then(response=>{
          if(res.data.code==200){
            this.$message.success("已读")
          }
      })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    /** 查询车辆故障列表 */
    getList() {
      this.loading = true;
      listFaultInfo(this.queryParams).then(response => {
        alert(1)
        debugger
        console.log(response)
        debugger
        this.faultInfoList = response.data;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        faultCode: null,
        faultName: null,
        faultType: null,
        carVin: null,
        faultLabel: null,
        faultBit: null,
        faultValue: null,
        faultWarn: null,
        warnStatus: null,
        faultDesc: null,
        state: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加车辆故障";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getFaultInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改车辆故障";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFaultInfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFaultInfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除车辆故障编号为"' + ids + '"的数据项？').then(function() {
        return delFaultInfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fault/faultInfo/export', {
        ...this.queryParams
      }, `faultInfo_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
