<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.planMaintCode" 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>
<!--    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">-->
<!--&lt;!&ndash;      <el-form-item label="计划ID，关联dms_plan_maint的plan_maint_id" prop="planMaintId">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-input&ndash;&gt;-->
<!--&lt;!&ndash;          v-model="queryParams.planMaintId"&ndash;&gt;-->
<!--&lt;!&ndash;          placeholder="请输入计划ID，关联dms_plan_maint的plan_maint_id"&ndash;&gt;-->
<!--&lt;!&ndash;          clearable&ndash;&gt;-->
<!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
<!--&lt;!&ndash;        />&ndash;&gt;-->
<!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;      <el-form-item label="设备ID，关联dms_base_device_ledger的device_id" prop="deviceId">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-input&ndash;&gt;-->
<!--&lt;!&ndash;          v-model="queryParams.deviceId"&ndash;&gt;-->
<!--&lt;!&ndash;          placeholder="请输入设备ID，关联dms_base_device_ledger的device_id"&ndash;&gt;-->
<!--&lt;!&ndash;          clearable&ndash;&gt;-->
<!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
<!--&lt;!&ndash;        />&ndash;&gt;-->
<!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--&lt;!&ndash;      <el-form-item label="保养部位,关联dms_base_maint_station的maint_station_id" prop="maintStationCode">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-input&ndash;&gt;-->
<!--&lt;!&ndash;          v-model="queryParams.maintStationCode"&ndash;&gt;-->
<!--&lt;!&ndash;          placeholder="请输入保养部位,关联dms_base_maint_station的maint_station_id"&ndash;&gt;-->
<!--&lt;!&ndash;          clearable&ndash;&gt;-->
<!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
<!--&lt;!&ndash;        />&ndash;&gt;-->
<!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--      <el-form-item label="保养标准" prop="maintProtocol">-->
<!--        <el-input-->
<!--          v-model="queryParams.maintProtocol"-->
<!--          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>-->
<!--&lt;!&ndash;      <el-form-item label="是否标识：1-是；2-否" prop="isFlag">&ndash;&gt;-->
<!--&lt;!&ndash;        <el-input&ndash;&gt;-->
<!--&lt;!&ndash;          v-model="queryParams.isFlag"&ndash;&gt;-->
<!--&lt;!&ndash;          placeholder="请输入是否标识：1-是；2-否"&ndash;&gt;-->
<!--&lt;!&ndash;          clearable&ndash;&gt;-->
<!--&lt;!&ndash;          @keyup.enter.native="handleQuery"&ndash;&gt;-->
<!--&lt;!&ndash;        />&ndash;&gt;-->
<!--&lt;!&ndash;      </el-form-item>&ndash;&gt;-->
<!--      <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>-->


<!--    <h4 class="form-header h4">设备保养信息</h4>-->
<!--    <el-table v-loading="loading"  :data="maintList" @selection-change="handleSelectionChange">-->
<!--&lt;!&ndash;      <el-table-column type="selection" width="55" align="center" />&ndash;&gt;-->
<!--      &lt;!&ndash;      <el-table-column label="主键标识" align="center" prop="planMaintId" />&ndash;&gt;-->
<!--&lt;!&ndash;      <el-table-column label="主键标识" align="center" prop="planMaintId" >&ndash;&gt;-->
<!--&lt;!&ndash;        <template slot-scope="scope">&ndash;&gt;-->
<!--&lt;!&ndash;          <router-link :to="'/dms/maintDetail/index/' + scope.row.planMaintId" class="link-type">&ndash;&gt;-->
<!--&lt;!&ndash;            <span>{{ scope.row.planMaintId }}</span>&ndash;&gt;-->
<!--&lt;!&ndash;          </router-link>&ndash;&gt;-->
<!--&lt;!&ndash;        </template>&ndash;&gt;-->
<!--&lt;!&ndash;      </el-table-column>&ndash;&gt;-->
<!--&lt;!&ndash;      <el-table-column label="计划编号" align="center" prop="planMaintCode" />&ndash;&gt;-->
<!--      &lt;!&ndash;      <el-table-column label="保养级别：1-日常保养；2-月度保养；3-年度保养" align="center" prop="maintLevel" />&ndash;&gt;-->
<!--      <el-table-column label="保养级别" align="center" prop="maintLevel">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_maint_level" :value="scope.row.maintLevel"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="保养组别" align="center" prop="maintGroup" />-->
<!--      <el-table-column label="保养负责人" align="center" prop="maintSupervisor" />-->
<!--      <el-table-column label="本次保养时间" align="center" prop="maintTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.maintTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="循环周期" align="center" prop="cyclePeriod" />-->
<!--      &lt;!&ndash;      <el-table-column label="保养状态：1-待保养；2-保养中；3-已完成" align="center" prop="maintStatus" />&ndash;&gt;-->
<!--      <el-table-column label="保养状态" align="center" prop="maintStatus" key="maintStatus1">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_maint_status" :value="scope.row.maintStatus"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      &lt;!&ndash;      <el-table-column label="创建方式：1-人工创建；2-自动创建" align="center" prop="createMethod" />&ndash;&gt;-->
<!--      <el-table-column label="创建方式" align="center" prop="createMethod" key="createMethod1">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.dms_create_method" :value="scope.row.createMethod"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <!--      <el-table-column label="是否标识：1-是；2-否" align="center" prop="isFlag" />-->
<!--      <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="handleUpdateCheck(scope.row)"-->
<!--            v-hasPermi="['dms:maint:edit']"-->
<!--          >修改</el-button>-->
<!--&lt;!&ndash;          <el-button&ndash;&gt;-->
<!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
<!--&lt;!&ndash;            type="text"&ndash;&gt;-->
<!--&lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
<!--&lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
<!--&lt;!&ndash;            v-hasPermi="['dms:maint:remove']"&ndash;&gt;-->
<!--&lt;!&ndash;          >删除</el-button>&ndash;&gt;-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->
    <h4 class="form-header h4">详细信息</h4>
    <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:maintDetail:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="maintDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键标识" align="center" prop="planMaintDetailId" />-->
<!--      <el-table-column label="设备ID，关联dms_base_device_ledger的device_id" align="center" prop="deviceId" />-->
      <el-table-column label="设备名称" align="center" prop="deviceId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in ledgerList"
              :key="index"
              :value="item.ledgerList"
              v-if="scope.row.deviceId == item.deviceId"
            >
              {{ item.deviceName }}
            </span>
        </template>
      </el-table-column>
<!--      <el-table-column label="保养部位,关联dms_base_maint_station的maint_station_id" align="center" prop="maintStationCode" />-->
      <el-table-column label="保养部位" align="center" prop="maintStationName" >
      </el-table-column>
      <el-table-column label="保养标准" align="center" prop="maintProtocol" />
      <el-table-column label="操作描述" align="center" prop="operationDescription" />
<!--      <el-table-column label="是否标识：1-是；2-否" align="center" prop="isFlag" />-->
      <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="handleUpdate(scope.row)"
            v-hasPermi="['dms:maintDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:maintDetail: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="titleCheck" :visible.sync="openCheck" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="计划编号" prop="planMaintCode">
          <el-input v-model="form.planMaintCode" placeholder="请输入计划编号"  :disabled="true"/>
        </el-form-item>
        <!--        <el-form-item label="保养级别：1-日常保养；2-月度保养；3-年度保养" prop="maintLevel">-->
        <!--          <el-input v-model="form.maintLevel" placeholder="请输入保养级别：1-日常保养；2-月度保养；3-年度保养" />-->
        <!--        </el-form-item>-->
        <el-form-item label="保养级别" prop="maintLevel">
          <el-radio-group v-model="form.maintLevel" :disabled="true">
            <el-radio
              v-for="dict in dict.type.dms_maint_level"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="保养组别" prop="maintGroup" >
          <el-input v-model="form.maintGroup" placeholder="请输入保养组别":disabled="true" />
        </el-form-item>

        <el-form-item label="循环周期" prop="cyclePeriod" >
          <el-input v-model="form.cyclePeriod" placeholder="请输入循环周期"  :disabled="true"/>
        </el-form-item>
        <!--        <el-form-item label="创建方式：1-人工创建；2-自动创建" prop="createMethod">-->
        <!--          <el-input v-model="form.createMethod" placeholder="请输入创建方式：1-人工创建；2-自动创建" />-->
        <!--        </el-form-item>-->

        <el-form-item label="创建方式" prop="createMethod" >
          <el-radio-group v-model="form.createMethod" :disabled="true">
            <el-radio
              v-for="dict in dict.type.dms_create_method"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="保养负责人" prop="maintSupervisor">
          <el-input v-model="form.maintSupervisor" placeholder="请输入保养负责人" />
        </el-form-item>

        <el-form-item label="本次保养时间" prop="maintTime">
          <el-date-picker clearable
                          v-model="form.maintTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择本次保养时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保养状态" prop="maintStatus">
          <el-radio-group v-model="form.maintStatus">
            <el-radio
              v-for="dict in dict.type.dms_maint_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <!--        <el-form-item label="是否标识：1-是；2-否" prop="isFlag">-->
        <!--          <el-input v-model="form.isFlag" placeholder="请输入是否标识：1-是；2-否" />-->
        <!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelCheck">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加或修改保养计划明细对话框 -->
    <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="planMaintId" v-if="false">
          <el-input v-model="form.planMaintId" placeholder="请输入计划ID"  :disabled=true  />
        </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="deviceId" >
          <el-select v-model="form.deviceId" 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="保养部位,关联dms_base_maint_station的maint_station_id" prop="maintStationCode">-->
<!--          <el-input v-model="form.maintStationCode" placeholder="请输入保养部位,关联dms_base_maint_station的maint_station_id" />-->
<!--        </el-form-item>-->
        <el-form-item label="保养部位" prop="maintStationCode" >
          <el-select v-model="form.maintStationCode" placeholder="请选择设备">
            <el-option
              v-for="item in stationList"
              :key="item.maintStationCode"
              :label="item.maintStationCode"
              :value="item.maintStationCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保养标准" prop="maintStandardId" >
          <el-select v-model="form.maintStandardId" placeholder="请选择标准">
            <el-option
              v-for="item in standardList"
              :key="item.maintStandardId"
              :label="item.maintProtocol"
              :value="item.maintStandardId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作描述" prop="operationDescription">
          <el-input v-model="form.operationDescription" placeholder="请输入操作描述" />
        </el-form-item>
<!--        <el-form-item label="是否标识：1-是；2-否" prop="isFlag">-->
<!--          <el-input v-model="form.isFlag" placeholder="请输入是否标识：1-是；2-否" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listMaintDetail, getMaintDetail, delMaintDetail, addMaintDetail, updateMaintDetail } from "@/api/dms/maintDetail";
import { listLedger } from '@/api/dms/ledger'
import { listStation } from '@/api/dms/station'
import { getMaint, listMaint } from '@/api/dms/maint'
import { listStandard } from '@/api/dms/standard'

export default {
  name: "MaintDetail",
  dicts:['dms_maint_level','dms_create_method','dms_maint_status'],
  data() {
    return {
      maintList:[],
      stationList:[],
      ledgerList:[],
      standardList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保养计划明细表格数据
      maintDetailList: [],
      planMaintIdCheck:"",
      planMaintCode:"",
      // 弹出层标题
      title: "",
      titleCheck:"",
      // 是否显示弹出层
      open: false,
      openCheck:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planMaintId: null,
        deviceId: null,
        maintStationId: null,
        maintStandardId: null,
        operationDescription: null,
        isFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        planMaintId: [
          { required: true, message: "计划ID，关联dms_plan_maint的plan_maint_id不能为空", trigger: "blur" }
        ],
        deviceId: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        maintStationCode: [
          { required: true, message: "保养部位不能为空", trigger: "blur" }
        ],
        maintStandardId: [
          { required: true, message: "保养标准不能为空", trigger: "blur" }
        ],
        // deviceId: [
        //   { required: true, message: "设备ID，关联dms_base_device_ledger的device_id不能为空", trigger: "blur" }
        // ],
        // isFlag: [
        //   { required: true, message: "是否标识：1-是；2-否不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    const planMaintId = this.$route.params && this.$route.params.planMaintId;
    const planMaintCode = this.$route.params && this.$route.params.planMaintCode;
    // console.log(planMaintId)
    this.planMaintIdCheck = planMaintId;
    this.planMaintCode = planMaintCode;
    this.queryParams.planMaintId = planMaintId;
    this.getList(planMaintId);
    this.getDeviceLedger();
    this.getStation();
    this.getMaint();
    this.getStandard();
  },
  methods: {
    /** 查询保养标准信息列表 */
    getStandard() {
      listStandard(this.queryParams).then(response => {
        console.log(response)
        this.standardList = response.rows;
      });
    },
    /** 查询保养计划信息列表 */
    getMaint() {
      this.loading = true;
      listMaint(this.queryParams).then(response => {
        this.maintList = response.rows;
        this.loading = false;
      });
    },
    /** 查询保养部位信息列表 */
    getStation() {
      listStation(this.queryParams).then(response => {
        console.log(response)
        this.stationList = response.rows;

      });
    },
    /** 查询设备台账信息列表 */
    getDeviceLedger() {
      listLedger(this.queryParams).then(response => {
        this.ledgerList = response.rows;
      });
    },
    /** 查询保养计划明细列表 */
    getList() {
      this.loading = true;
      listMaintDetail(this.queryParams).then(response => {
        this.maintDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelCheck() {
      this.openCheck = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planMaintDetailId: null,
        planMaintId: null,
        deviceId: null,
        maintStationId: null,
        maintStandardId: null,
        operationDescription: null,
        isFlag: null,
        remark: 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.planMaintDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.planMaintId = this.planMaintIdCheck;
      this.open = true;
      this.title = "添加保养计划明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planMaintDetailId = row.planMaintDetailId || this.ids
      getMaintDetail(planMaintDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保养计划明细信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdateCheck(row) {
      this.reset();
      const planMaintId = row.planMaintId || this.ids
      getMaint(planMaintId).then(response => {
        this.form = response.data;
        // this.dmsPlanMaintDetailList = response.data.dmsPlanMaintDetailList;
        this.openCheck = true;
        this.titleCheck = "修改保养计划信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planMaintDetailId != null) {
            updateMaintDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMaintDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planMaintDetailIds = row.planMaintDetailId || this.ids;
      this.$modal.confirm('是否确认删除保养计划明细编号为"' + planMaintDetailIds + '"的数据项？').then(function() {
        return delMaintDetail(planMaintDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/maintDetail/export', {
        ...this.queryParams
      }, `maintDetail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
