<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="专业" prop="specialty">
        <el-input v-model="queryParams.specialty" placeholder="请输入专业" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="故障件名称" prop="faultyPartName">
        <el-input v-model="queryParams.faultyPartName" 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="['fault:faultRecord: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="['fault:faultRecord: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="['fault:faultRecord: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="['fault:faultRecord:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultRecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="日期" align="center" prop="incidentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.incidentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="机型" align="center" prop="aircraftType" />
      <el-table-column label="专业" align="center" prop="specialty" />
      <el-table-column label="故障件名称" align="center" prop="faultyPartName" />
      <el-table-column label="故障现象" align="center" prop="faultDescription" />
      <el-table-column label="系统" align="center" prop="useSystem" />
      <el-table-column label="故障原因" align="center" prop="faultCause" />
      <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="['fault:faultRecord:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fault:faultRecord: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="70%" append-to-body @open="dialogOpen" >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top" style="max-height: 600px;overflow-y: auto;overflow-x: hidden;">
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="日期" prop="incidentDate">
              <el-date-picker clearable v-model="form.incidentDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择日期" style="width: 100%;">
              </el-date-picker>
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="战区" prop="militaryRegion">
              <el-input v-model="form.militaryRegion" placeholder="请输入战区" />
            </el-form-item>
          </el-col>
          <el-col :span="6"><el-form-item label="军" prop="army">
              <el-input v-model="form.army" placeholder="请输入军" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="师" prop="division">
              <el-input v-model="form.division" placeholder="请输入师" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6"> <el-form-item label="团" prop="regiment">
              <el-input v-model="form.regiment" placeholder="请输入团" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="中队" prop="squadron">
              <el-input v-model="form.squadron" placeholder="请输入中队" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="飞机号" prop="aircraftNo">
              <el-input v-model="form.aircraftNo" placeholder="请输入飞机号" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="机型" prop="aircraftType">
              <el-input v-model="form.aircraftType" placeholder="请输入机型" />
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6"> <el-form-item label="出厂号码" prop="factoryNo">
              <el-input v-model="form.factoryNo" placeholder="请输入出厂号码" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="机件类型" prop="partCategory">
              <el-input v-model="form.partCategory" placeholder="请输入机件类型" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="机件号" prop="partNo">
              <el-input v-model="form.partNo" placeholder="请输入机件号" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="机件型别" prop="partModel">
              <el-input v-model="form.partModel" placeholder="请输入机件型别" />
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="机件名称" prop="partName">
              <el-input v-model="form.partName" placeholder="请输入机件名称" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="起落" prop="landingCount">
              <el-input v-model="form.landingCount" placeholder="请输入起落" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="专业" prop="specialty">
              <el-input v-model="form.specialty" placeholder="请输入专业" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件名称" prop="faultyPartName">
              <el-input v-model="form.faultyPartName" placeholder="请输入故障件名称" />
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="故障件型别" prop="faultyPartModel">
              <el-input v-model="form.faultyPartModel" placeholder="请输入故障件型别" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故障件号码" prop="faultyPartNo">
              <el-input v-model="form.faultyPartNo" placeholder="请输入故障件号码" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件所属发动机型别" prop="engineType">
              <el-input v-model="form.engineType" placeholder="请输入故障件所属发动机型别" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件所属发动机号码" prop="engineNo">
              <el-input v-model="form.engineNo" placeholder="请输入故障件所属发动机号码" />
            </el-form-item></el-col>
        </el-row>


        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="故障件所属发动机序号" prop="engineSerialNo">
              <el-input v-model="form.engineSerialNo" placeholder="请输入故障件所属发动机序号" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故障件位置" prop="faultLocation">
              <el-input v-model="form.faultLocation" placeholder="请输入故障件位置" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障现象" prop="faultDescription">
              <el-input v-model="form.faultDescription" type="textarea" placeholder="请输入内容" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障发生地点" prop="faultLocationSite">
              <el-input v-model="form.faultLocationSite" placeholder="请输入故障发生地点" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="发现时机" prop="detectionTiming">
              <el-input v-model="form.detectionTiming" placeholder="请输入发现时机" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="系统" prop="useSystem">
              <el-input v-model="form.useSystem" placeholder="请输入系统" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件制造厂" prop="manufacturer">
              <el-input v-model="form.manufacturer" placeholder="请输入故障件制造厂" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件翻修厂" prop="repairFacility">
              <el-input v-model="form.repairFacility" placeholder="请输入故障件翻修厂" />
            </el-form-item></el-col>
        </el-row>


        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="故障件装本机工作时次" prop="workHoursCurrent">
              <el-time-picker clearable v-model="form.workHoursCurrent" type="time" value-format="HH:mm:ss"
                placeholder="请选择故障件装本机工作时次" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故障件总工作时次" prop="workHoursTotal">
              <el-time-picker clearable v-model="form.workHoursTotal" type="time" value-format="HH:mm:ss"
                placeholder="请选择故障件总工作时次" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件修后时次" prop="workHoursAfterRepair">
              <el-time-picker clearable v-model="form.workHoursAfterRepair" type="time" value-format="HH:mm:ss"
                placeholder="请选择故障件修后时次" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故换件型别" prop="replacementPartModel">
              <el-input v-model="form.replacementPartModel" placeholder="请输入故换件型别" />
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="故换件号码" prop="replacementPartNo">
              <el-input v-model="form.replacementPartNo" placeholder="请输入故换件号码" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故换件制造厂" prop="replacementManufacturer">
              <el-input v-model="form.replacementManufacturer" placeholder="请输入故换件制造厂" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故换件总工作时次" prop="replacementWorkHoursTotal">
              <el-time-picker clearable v-model="form.replacementWorkHoursTotal" type="time" value-format="HH:mm:ss"
                placeholder="请选择故换件总工作时次" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故换件翻修厂" prop="replacementRepairFacility">
              <el-input v-model="form.replacementRepairFacility" placeholder="请输入故换件翻修厂" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6"><el-form-item label="故换件修后时次" prop="replacementHoursAfterRepair">
              <el-time-picker clearable v-model="form.replacementHoursAfterRepair" type="time" value-format="HH:mm:ss"
                placeholder="请选择故换件修后时次" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故障失常码" prop="faultCode">
              <el-input v-model="form.faultCode" placeholder="请输入故障失常码" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障件装机日期" prop="installationDate">
              <el-time-picker clearable v-model="form.installationDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择故障件装机日期" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障原因" prop="faultCause">
              <el-input v-model="form.faultCause" type="textarea" placeholder="请输入内容" />
            </el-form-item></el-col>
        </el-row>



        <el-row :gutter="10">
          <el-col :span="6"> <el-form-item label="故障性质" prop="faultNature">
              <el-input v-model="form.faultNature" placeholder="请输入故障性质" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="故障责任" prop="faultResponsibility">
              <el-input v-model="form.faultResponsibility" placeholder="请输入故障责任" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="故障后果" prop="faultConsequence">
              <el-input v-model="form.faultConsequence" placeholder="请输入故障后果" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="处理意见" prop="handlingSuggestion">
              <el-input v-model="form.handlingSuggestion" type="textarea" placeholder="请输入内容" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6"> <el-form-item label="影响次数" prop="impactCount">
              <el-input v-model="form.impactCount" placeholder="请输入影响次数" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="误飞次数" prop="missedFlightCount">
              <el-input v-model="form.missedFlightCount" placeholder="请输入误飞次数" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="判明方法" prop="detectionMethod">
              <el-input v-model="form.detectionMethod" placeholder="请输入判明方法" />
            </el-form-item></el-col>
          <el-col :span="6">
            <el-form-item label="发现人" prop="discoverer">
              <el-input v-model="form.discoverer" placeholder="请输入发现人" />
            </el-form-item></el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6"> <el-form-item label="排除方法" prop="resolutionMethod">
              <el-input v-model="form.resolutionMethod" placeholder="请输入排除方法" />
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="排故人" prop="maintainer">
              <el-input v-model="form.maintainer" placeholder="请输入排故人" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="审核人" prop="reviewer">
              <el-input v-model="form.reviewer" placeholder="请输入审核人" />
            </el-form-item></el-col>

          <el-col :span="6">
            <el-form-item label="审核时间" prop="reviewTime">
              <el-date-picker clearable v-model="form.reviewTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择审核时间" style="width: 100%;">
              </el-date-picker>
            </el-form-item>

          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="排除日期" prop="resolutionDate">
              <el-date-picker clearable v-model="form.resolutionDate" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择排除日期" style="width: 100%;">
              </el-date-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="排故工时" prop="maintenanceHours">
              <el-time-picker clearable v-model="form.maintenanceHours" type="time" value-format="HH:mm:ss"
                placeholder="请选择排故工时" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="排故人数" prop="maintenanceStaffCount">
              <el-input v-model="form.maintenanceStaffCount" placeholder="请输入排故人数" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="排故时间" prop="maintenanceTime">
              <el-time-picker clearable v-model="form.maintenanceTime" type="time" value-format="HH:mm:ss"
                placeholder="请选择排故时间" style="width: 100%;">
              </el-time-picker>
            </el-form-item></el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="是否立功" prop="meritAwarded">
              <el-select v-model="form.meritAwarded" placeholder="请选择是否立功" style="width: 100%;">
                <el-option v-for="dict in dict.type.fault_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item></el-col>
          <el-col :span="6"> <el-form-item label="需要试飞" prop="testFlightRequired">
              <el-select v-model="form.testFlightRequired" placeholder="请选择需要试飞" style="width: 100%;">
                <el-option v-for="dict in dict.type.fault_yes_no" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="文件路径" prop="filePath">
              <file-upload v-model="form.filePath" />
            </el-form-item></el-col>
        </el-row>

      </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 {
    listFaultRecord,
    getFaultRecord,
    delFaultRecord,
    addFaultRecord,
    updateFaultRecord
  } from "@/api/fault/faultRecord";
  import {
    listSpeciality,
    getSpeciality,
    delSpeciality,
    addSpeciality,
    updateSpeciality
  } from "@/api/basic/speciality";
  import FileUpload from "@/components/FileUpload/upload.vue";
  export default {
    name: "FaultRecord",
    dicts: ['fault_yes_no', 'fault_status'],
    components: { FileUpload },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 故障记录表格数据
        faultRecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          aircraftType: null,
          specialty: null,
          faultyPartName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {},
        specialityList: [],
        belongType: 'tech',
        belongId: null
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询故障记录列表 */
      getList() {
        this.loading = true;
        listFaultRecord(this.queryParams).then(response => {
          this.faultRecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      dialogOpen() {
        this.getSpList();
      },
      getSpList() {
        const queryParams = {
          pageNum: 1,
          pageSize: 50
        };
        listSpeciality(queryParams).then(response => {
          this.specialityList = response.rows;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.belongId = null;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          incidentDate: null,
          militaryRegion: null,
          army: null,
          division: null,
          regiment: null,
          squadron: null,
          aircraftNo: null,
          aircraftType: null,
          factoryNo: null,
          partCategory: null,
          partNo: null,
          partModel: null,
          partName: null,
          landingCount: null,
          specialty: null,
          faultyPartName: null,
          faultyPartModel: null,
          faultyPartNo: null,
          engineType: null,
          engineNo: null,
          engineSerialNo: null,
          faultLocation: null,
          faultDescription: null,
          faultLocationSite: null,
          detectionTiming: null,
          useSystem: null,
          timingType: null,
          manufacturer: null,
          repairFacility: null,
          workHoursCurrent: null,
          workHoursTotal: null,
          workHoursAfterRepair: null,
          replacementPartModel: null,
          replacementPartNo: null,
          replacementManufacturer: null,
          replacementWorkHoursTotal: null,
          replacementRepairFacility: null,
          replacementHoursAfterRepair: null,
          faultCode: null,
          installationDate: null,
          faultCause: null,
          faultNature: null,
          faultResponsibility: null,
          faultConsequence: null,
          handlingSuggestion: null,
          impactCount: null,
          missedFlightCount: null,
          detectionMethod: null,
          discoverer: null,
          resolutionMethod: null,
          maintainer: null,
          reviewer: null,
          reviewTime: null,
          resolutionDate: null,
          maintenanceHours: null,
          maintenanceStaffCount: null,
          maintenanceTime: null,
          meritAwarded: null,
          testFlightRequired: null,
          remarks: null,
          filePath: 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.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加故障记录";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        this.belongId = id;
        getFaultRecord(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改故障记录";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateFaultRecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addFaultRecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除故障记录编号为"' + ids + '"的数据项？').then(function() {
          return delFaultRecord(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('fault/faultRecord/export', {
          ...this.queryParams
        }, `faultRecord_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
