<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.planLubeCode" disabled />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <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:lubeDetail:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="lubeDetailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <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="润滑部位" align="center" prop="lubeStationId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in stationList"
              :key="index"
              :value="item.stationList"
              v-if="scope.row.lubeStationId == item.lubeStationId"
            >
              {{ item.lubeStationName }}
            </span>
        </template>
      </el-table-column>
      <el-table-column label="润滑标准" align="center" prop="lubeStandardId" >
        <template slot-scope="scope">
            <span
              v-for="(item, index) in standardList"
              :key="index"
              :value="item.standardList"
              v-if="scope.row.lubeStandardId == item.lubeStandardId"
            >
              {{ item.lubeProtocol }}
            </span>
        </template>
      </el-table-column>
      <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:lubeDetail:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dms:lubeDetail: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="planLubeCode">
          <el-input v-model="form.planLubeCode"  :disabled="true"/>
        </el-form-item>
        <el-form-item label="润滑级别" prop="lubeLevel">
          <el-radio-group v-model="form.lubeLevel" :disabled="true">
            <el-radio
              v-for="dict in dict.type.dms_lube_level"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="润滑组别" prop="lubeGroup" >
          <el-input v-model="form.lubeGroup" 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="lubeSupervisor">
          <el-input v-model="form.lubeSupervisor" placeholder="请输入润滑负责人" />
        </el-form-item>

        <el-form-item label="本次润滑时间" prop="lubeTime">
          <el-date-picker clearable
                          v-model="form.lubeTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择本次润滑时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="润滑状态" prop="lubeStatus">
          <el-radio-group v-model="form.lubeStatus">
            <el-radio
              v-for="dict in dict.type.dms_lube_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="设备名称" 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="润滑部位" prop="lubeStationId" >
          <el-select v-model="form.lubeStationId" placeholder="请选择设备">
            <el-option
              v-for="item in stationList"
              :key="item.lubeStationId"
              :label="item.lubeStationName"
              :value="item.lubeStationId">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="润滑标准" prop="lubeStandardId" >
          <el-select v-model="form.lubeStandardId" placeholder="请选择标准">
            <el-option
              v-for="item in standardList"
              :key="item.lubeStandardId"
              :label="item.lubeProtocol"
              :value="item.lubeStandardId">
            </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 { listDmsPlanLubeDetail, getDmsPlanLubeDetail, delDmsPlanLubeDetail,
  addDmsPlanLubeDetail, updateDmsPlanLubeDetail,
  getLubeStations,getLubeStandards,getBaseDeviceLedgers
} from "@/api/dms/dmsPlanLubeDetail";import { listLedger } from '@/api/dms/ledger'

export default {
  name: "LubeDetail",
  dicts:['dms_lube_level','dms_create_method','dms_lube_status'],
  data() {
    return {
      lubeList:[],
      stationList:[],
      ledgerList:[],
      standardList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 保养计划明细表格数据
      lubeDetailList: [],
      planLubeId:"",
      planLubeCode:"",
      // 弹出层标题
      title: "",
      titleCheck:"",
      // 是否显示弹出层
      open: false,
      openCheck:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        planLubeId: null,
        deviceId: null,
        lubeStationId: null,
        lubeStandardId: null,
        operationDescription: null,
        isFlag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // deviceId: [
        //   { required: true, message: "设备ID，关联dms_base_device_ledger的device_id不能为空", trigger: "blur" }
        // ],
        // isFlag: [
        //   { required: true, message: "是否标识：1-是；2-否不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    const planLubeId = this.$route.params && this.$route.params.planLubeId;
    const planLubeCode = this.$route.params && this.$route.params.planLubeCode;
    this.planLubeId = planLubeId;
    this.planLubeCode = planLubeCode;
    this.queryParams.planLubeId = planLubeId;
    this.getList(planLubeId);
    this.getDeviceLedger();
    this.getStation();
    this.getStandard();
  },
  methods: {
    /** 查询润滑标准信息列表 */
    getStandard() {
      getLubeStandards().then(response => {
        this.standardList = response.data;
      });
    },

    /** 查询润滑部位信息列表 */
    getStation() {
      getLubeStations().then(response => {
        this.stationList = response.data;
      });
    },
    /** 查询设备台账信息列表 */
    getDeviceLedger() {
      getBaseDeviceLedgers().then(response => {
        this.ledgerList = response.data;
      });
    },
    /** 查询润滑计划明细列表 */
    getList() {
      this.loading = true;
      listDmsPlanLubeDetail({planLubeId:this.planLubeId}).then(response => {
        this.lubeDetailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelCheck() {
      this.openCheck = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        planLubeDetailId: null,
        planLubeId: null,
        deviceId: null,
        lubeStationId: null,
        lubeStandardId: 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.planLubeDetailId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.planLubeId = this.planLubeId;
      this.open = true;
      this.title = "添加润滑计划明细";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const planLubeDetailId = row.planLubeDetailId || this.ids
      getDmsPlanLubeDetail(planLubeDetailId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改润滑计划明细信息";
      });
    },
    /** 修改按钮操作 */
    handleUpdateCheck(row) {
      this.reset();
      const planLubeId = row.planLubeId || this.ids
      getLube(planLubeId).then(response => {
        this.form = response.data;
        this.openCheck = true;
        this.titleCheck = "修改润滑计划信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.planLubeDetailId != null) {
            updateDmsPlanLubeDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDmsPlanLubeDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const planLubeDetailIds = row.planLubeDetailId || this.ids;
      this.$modal.confirm('是否确认删除润滑计划明细编号为"' + planLubeDetailIds + '"的数据项？').then(function() {
        return delDmsPlanLubeDetail(planLubeDetailIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },


  }
};
</script>
