<template>
  <div class="app-container power-examine">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入设备名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="故障类型" prop="faultType">
        <el-select
          v-model="queryParams.faultType"
          placeholder="请选择故障类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in faultTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="上报人" prop="reporterName">
        <el-input
          v-model="queryParams.reporterName"
          placeholder="请输入上报人名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="处理人" prop="handlerName">
        <el-input
          v-model="queryParams.handlerName"
          placeholder="请输入处理人名称"
          clearable
          size="small"
          @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-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iot:node:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->

    <el-table
      v-loading="loading"
      :data="nodeList"
      :default-sort="{ prop: 'handleTime', order: 'descending' }"
      @sort-change="sortChange"
    >
      <el-table-column
        type="index"
        label="序号"
        align="center"
        :index="indexFormatter"
        width="80px"
      ></el-table-column>
      <!-- <el-table-column label="审核节点ID" align="center" prop="nodeId" /> -->
      <el-table-column label="项目名称" align="left" prop="projectName" />
      <el-table-column label="项目地址" align="left" prop="projectAddress" />
      <el-table-column label="设备名称" align="left" prop="deviceName" />
      <el-table-column
        sortable="custom"
        label="故障类型"
        align="center"
        width="150"
        prop="faultType"
        :formatter="statusFormat"
      />
      <el-table-column label="上报人" align="left" prop="reporterName" />
      <el-table-column label="处理人" align="left" prop="handlerName" />
      <el-table-column label="工作内容" align="left" prop="maintenanceContext">
        <template slot-scope="scope">
          <span
            class="lay-table-textarea"
            :title="scope.row.maintenanceContext"
          >
            {{ scope.row.maintenanceContext }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="处理时间"
        align="center"
        sortable="custom"
        prop="handleTime"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column label="工单状态" align="center" width="120" prop="maintenanceStatus">
        <template slot-scope="scope">
          <span>{{ workStateList[scope.row.workOrderStatus] }}</span>
        </template>
      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        width="200"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handelDetailExamine(scope.row)"
            >工单审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(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
      class="eldialog-wrap"
      :title="title"
      :visible.sync="open"
      width="450px"
      append-to-body
    >
      <el-form>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="审核结果:" prop="auditResult">
            <el-radio-group v-model="form.auditResult" disabled>
              <el-radio label="通过">通过</el-radio>
              <el-radio label="不通过">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核意见:" prop="auditOpinion">
            <el-input
              v-model="form.auditOpinion"
              type="textarea"
              maxlength="150"
              :rows="2"
              placeholder="请输入审核意见"
            />
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      class="eldialog-wrap details-w"
      title="工单审核"
      :visible.sync="openDetails"
      width="700px"
    >
      <power-details-wrap
        :maintenanceInfo="workDetails"
        tempType="examine"
        :maintenanceId="workDetails.maintenanceId"
      ></power-details-wrap>
      <el-divider content-position="left">审核信息</el-divider>
      <el-form>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="审核结果:" prop="auditResult">
            <el-radio-group
              v-model="form.auditResult"
              @change="auditResultChange"
            >
              <el-radio label="通过">通过</el-radio>
              <el-radio label="不通过">不通过</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item
            label="退回节点:"
            v-if="form.auditResult === '不通过'"
            prop="maintenanceStatus"
          >
            <el-radio-group v-model="form.maintenanceStatus">
              <el-radio label="4">退回处理</el-radio>
              <el-radio label="1">退回上报</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="审核意见:" prop="auditOpinion">
            <el-input
              v-model="form.auditOpinion"
              type="textarea"
              :rows="2"
              placeholder="请输入审核意见"
            />
          </el-form-item>
        </el-form>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button size="mini" @click="openDetails = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addNode } from "@/api/power/examine";
import { listMaintenance, updateMaintenance } from "@/api/power/maintenance";
import PowerDetailsWrap from "../mywork/details";

export default {
  name: "Node",
  components: {
    PowerDetailsWrap,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工单审核表格数据
      nodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceId: null,
        auditorId: null,
        auditorName: null,
        auditTime: null,
        auditResult: null,
        auditOpinion: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        auditResult: [
          { required: true, message: "审核结果不能为空", trigger: "blur" },
        ],
        maintenanceStatus: [
          { required: true, message: "退回节点不能为空", trigger: "blur" },
        ],
      },
      workDetails: {},
      openDetails: false,
      faultTypeOptions: [],
    };
  },
  created() {
    // 获取故障类型（字典）
    this.getDicts("fault_type").then((response) => {
      this.faultTypeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    sortChange(column) {
      const sort = {
        isAsc: column.order === "descending" ? "desc" : "asc",
        orderByColumn: column.prop,
      };
      this.queryParams = Object.assign(this.queryParams, sort);
      this.handleQuery();
    },
    auditResultChange(val) {
      if (val === "通过") {
        this.form.maintenanceStatus = "5";
      } else {
        this.form.maintenanceStatus = "4";
      }
    },
    indexFormatter(val) {
      return (
        val + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
      );
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.faultTypeOptions, row.faultType);
    },
    /** 查询工单审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.maintenanceStatus = "3";
      listMaintenance(this.queryParams).then((response) => {
        this.nodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        maintenanceId: null,
        auditTime: null,
        auditResult: "通过",
        auditOpinion: null,
        remark: null,
        maintenanceStatus: "5",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.nodeId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    // 查看详情 做出审核
    handelDetailExamine(row) {
      this.workDetails = row;
      this.reset();
      this.openDetails = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.workDetails = row;
      this.reset();
      this.title = "工单审核";
      this.form.auditResult = "通过";
      this.open = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.maintenanceId = this.workDetails.maintenanceId;
          addNode(this.form).then((response) => {
            updateMaintenance({
              maintenanceId: this.workDetails.maintenanceId,
              maintenanceStatus: this.form.maintenanceStatus,
            }).then((response) => {
              this.msgSuccess("请求成功");
              this.openDetails = false;
              this.open = false;
              this.getList();
            });
          });
        }
      });
    },
  },
};
</script>
<style lang="scss">
.power-examine {
  .eldialog-wrap {
    .el-dialog__header {
      border-bottom: 1px solid #747373;
    }
    .el-dialog__body {
      padding: 0px;
    }
    .el-form {
      padding: 20px;
      padding-right: 40px;
    }
    .el-dialog__footer {
      height: 60px;
      border-top: 1px solid #747373;
      text-align: right;
      width: 100%;
      padding: 0px;
      padding-top: 15px;
      .el-button + .el-button {
        margin-right: 10px;
      }
      .el-button {
        padding-top: 8px;
      }
    }
    .table-avatar {
      .el-form-item__content {
        margin-left: 0px !important;
      }
    }
  }
  .details-w {
    .el-dialog__body {
      padding: 5px 0;
    }
  }
}
</style>
