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

      <el-form-item label="故障类型" prop="faultType">
        <el-select
          v-model="queryParams.faultType"
          clearable
          placeholder="请选择故障类型"
          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"
          clearable
          placeholder="请输入上报人名称"
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

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

      <el-form-item>
        <el-button
          icon="el-icon-search"
          size="mini"
          type="primary"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <div class="main-content-card">
    <!-- <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
        :index="indexFormatter"
        align="center"
        label="序号"
        type="index"
        width="80px"
      ></el-table-column>
      <!-- <el-table-column label="审核节点ID" align="center" prop="nodeId" /> -->
      <el-table-column align="left" label="项目名称" prop="projectName" />
      <el-table-column align="left" label="项目地址" prop="projectAddress" />
      <el-table-column align="left" label="设备名称" prop="deviceName" />
      <el-table-column
        :formatter="statusFormat"
        align="center"
        label="故障类型"
        prop="faultType"
        sortable="custom"
        width="150"
      />
      <el-table-column align="left" label="上报人" prop="reporterNickName" />
      <el-table-column align="left" label="处理人" prop="handleNickName" />
      <el-table-column align="left" label="工作内容" prop="maintenanceContext">
        <template slot-scope="scope">
          <span
            :title="scope.row.maintenanceContext"
            class="lay-table-textarea"
          >
            {{ scope.row.maintenanceContext }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="处理时间"
        prop="handleTime"
        sortable="custom"
        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
        align="center"
        class-name="small-padding fixed-width"
        label="操作"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handelDetailExamine(scope.row)"
            >工单审核</el-button
          >
          <el-button
            icon="el-icon-edit"
            size="mini"
            type="text"
            @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"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
    </div>
    <!-- 添加或修改工单审核对话框 -->
    <el-dialog
      :close-on-click-modal="false"
      :title="title"
      :visible.sync="open"
      append-to-body
      class="eldialog-wrap"
      width="450px"
    >
      <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"
              :rows="2"
              maxlength="150"
              placeholder="请输入审核意见"
              type="textarea"
            />
          </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
      :close-on-click-modal="false"
      :visible.sync="openDetails"
      class="eldialog-wrap details-w"
      title="工单审核"
      width="700px"
    >
      <power-details-wrap
        :maintenanceId="workDetails.maintenanceId"
        :maintenanceInfo="workDetails"
        tempType="examine"
      ></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
            v-if="form.auditResult === '不通过'"
            label="退回节点:"
            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"
              :rows="2"
              placeholder="请输入审核意见"
              type="textarea"
            />
          </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>
