<template>
  <div class="app-container">
  <h4 class="form-header h4">检修计划信息</h4>
  <el-form  label-width="120px">
    <el-row>
      <el-col :span="8" :offset="2">
        <el-form-item label="检修计划编号" prop="planRepairId">
          <el-input v-model="this.planMaintIdCheck" disabled />
        </el-form-item>
      </el-col>
      <el-col :span="8" :offset="2">
        <el-form-item label="检修负责人" prop="planRepairId">
          <el-input v-model="this.repairSupervisor" disabled />
        </el-form-item>
      </el-col>
<!--      <el-col :span="8" :offset="2">-->
<!--        <el-form-item label="登录账号" prop="deviceId">-->
<!--          <el-input  v-model="form.deviceId" disabled />-->
<!--        </el-form-item>-->
<!--      </el-col>-->
    </el-row>
  </el-form>
    <h4 class="form-header h4">详细信息</h4>
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--      <el-form-item label="计划ID，关联dms_plan_repair的plan_maint_id" prop="planRepairId">-->
<!--        <el-input-->
<!--          v-model="queryParams.planRepairId"-->
<!--          placeholder="请输入计划ID，关联dms_plan_repair的plan_maint_id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="设备ID，关联dms_base_device_ledger的device_id" prop="deviceId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deviceId"-->
<!--          placeholder="请输入设备ID，关联dms_base_device_ledger的device_id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检修说明" prop="repairDesc">-->
<!--        <el-input-->
<!--          v-model="queryParams.repairDesc"-->
<!--          placeholder="请输入检修说明"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="检修标准" prop="repairProtocol">-->
<!--        <el-input-->
<!--          v-model="queryParams.repairProtocol"-->
<!--          placeholder="请输入检修标准"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="操作描述" prop="operationDescription">-->
<!--        <el-input-->
<!--          v-model="queryParams.operationDescription"-->
<!--          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-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="['dms:dmsRepairDetail:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="addmembers"-->
<!--          v-hasPermi="['dms:dmsRepairDetail: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="['dms:dmsRepairDetail: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="['dms:dmsRepairDetail: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="['dms:dmsRepairDetail:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dmsRepairDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键标识" align="center" prop="planRepairDetailId" />-->
<!--      <el-table-column label="计划ID，关联dms_plan_repair的plan_maint_id" align="center"-->
<!--                       prop="planRepairId" >-->
<!--        <template slot-scope="scope">-->
<!--          <el-input v-model="scope.row.planRepairId" autocomplete="off" size="small"-->
<!--                    placeholder="请输入姓名"></el-input>-->

<!--        </template>-->
<!--      </el-table-column>-->

      <el-table-column label="设备名称" align="center" prop="repairDetailTargetId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in ledgerList"
              :key="index"
              :value="item.ledgerList"
              v-if="scope.row.repairDetailTargetId == item.deviceId"
            >
              {{ item.deviceName }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="故障类别" align="center" prop="faultType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_fault_type" :value="scope.row.faultType"/>
        </template>
      </el-table-column>
      <el-table-column label="检修说明" align="center" prop="repairDesc" />
      <el-table-column label="检修标准" align="center" prop="repairProtocol" />
      <el-table-column label="操作描述" align="center" prop="operationDescription" />

      <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="addRepairDetaile(scope.row)"-->
<!--            v-hasPermi="['dms:dmsRepairDetail:edit']"-->
<!--          >确定/修改</el-button>-->

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dms:dmsRepairDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:dmsRepairDetail: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="计划ID，关联dms_plan_repair的plan_maint_id" prop="planRepairId">-->
<!--          <el-input v-model="form.planRepairId" placeholder="请输入计划ID，关联dms_plan_repair的plan_maint_id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="计划ID" prop="planRepairId" >-->
<!--          <el-select v-model="form.deviceId" placeholder="请输入计划ID">-->
<!--            <el-option-->
<!--              v-for="item in dmsRepairList"-->
<!--              :key="item.planRepairCode"-->
<!--              :label="item.planRepairCode"-->
<!--              :value="item.planRepairCode">-->
<!--            </el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="设备ID，关联dms_base_device_ledger的device_id" prop="deviceId">-->
<!--          <el-input v-model="form.deviceId" placeholder="请输入设备ID，关联dms_base_device_ledger的device_id" />-->
<!--        </el-form-item>-->
        <el-form-item label="巡检设备" prop="repairDetailTargetId" >
          <el-select v-model="form.repairDetailTargetId" placeholder="请输入设备类型">
            <el-option
              v-for="item in ledgerList"
              :key="item.deviceId"
              :label="item.deviceName"
              :value="item.deviceId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保养等级" prop="faultType">
          <el-select v-model="form.faultType" placeholder="保养等级" >
            <el-option
              v-for="item in dict.type.dms_fault_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检修说明" prop="repairDesc">
          <el-input v-model="form.repairDesc" placeholder="请输入检修说明" />
        </el-form-item>
        <el-form-item label="检修标准" prop="repairProtocol">
          <el-input v-model="form.repairProtocol" placeholder="请输入检修标准" />
        </el-form-item>
        <el-form-item label="操作描述" prop="operationDescription">
          <el-input v-model="form.operationDescription" placeholder="请输入操作描述" />
        </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>
  </div>
</template>

<script>
import { listDmsRepairDetail, getDmsRepairDetail, delDmsRepairDetail, addDmsRepairDetail, updateDmsRepairDetail } from "@/api/dms/dmsRepairDetail";
import { listLedger } from '@/api/dms/ledger'
import { listDmsRepair } from '@/api/dms/dmsRepair'
import dict from '@/store/modules/dict'

export default {
  name: "DmsRepairDetail",
  dicts:['dms_fault_type','dms_repair_type'],
  data() {
    return {
      planMaintIdCheck:'',
      repairType:'',
      repairSupervisor:'',
      ledgerList:[],
      //检修计划列表
      dmsRepairList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检修计划明细表格数据
      dmsRepairDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planRepairId: null,
        repairDetailTargetId:null,
        repairDetailTargetType:null,
        faultType: null,
        repairDesc: null,
        repairProtocol: null,
        operationDescription: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planRepairId: [
          { required: true, message: "计划ID，关联dms_plan_repair的plan_maint_id不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备ID，关联dms_base_device_ledger的device_id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const planRepairId = this.$route.params && this.$route.params.planMaintId;
    // console.log(planRepairId);
    this.planMaintIdCheck = planRepairId;
    this.queryParams.planRepairId = planRepairId;
    this.getList();
    this.getPlanRepair();
    this.getDeviceLedger();
  },
  methods: {
    /** 查询检修计划信息列表 */
    getPlanRepair() {
      listDmsRepair(this.queryParams).then(response => {
        this.dmsRepairList = response.rows;
        this.repairType = response.rows[0].repairType;
        this.repairSupervisor = response.rows[0].repairSupervisor;
      });
    },
    /** 查询设备台账信息列表 */
    getDeviceLedger() {
      listLedger(this.queryParams).then(response => {
        this.ledgerList = response.rows;
        // console.log(this.ledgerList)
      });
    },

    /** 查询检修计划明细列表 */
    getList() {
      this.loading = true;
      listDmsRepairDetail(this.queryParams).then(response => {
        this.dmsRepairDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planRepairDetailId: null,
        planRepairId: null,
        repairDetailTargetType:null,
        repairDetailTargetId:null,
        faultType: null,
        repairDesc: null,
        repairProtocol: null,
        operationDescription: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: 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.planRepairDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检修计划明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planRepairDetailId = row.planRepairDetailId || this.ids
      getDmsRepairDetail(planRepairDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检修计划明细";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planRepairDetailId != null) {
            updateDmsRepairDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.planRepairId = this.planMaintIdCheck
            addDmsRepairDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planRepairDetailIds = row.planRepairDetailId || this.ids;
      this.$modal.confirm('是否确认删除检修计划明细编号为"' + planRepairDetailIds + '"的数据项？').then(function() {
        return delDmsRepairDetail(planRepairDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dmsRepairDetail/export', {
        ...this.queryParams
      }, `dmsRepairDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>



