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

    <el-table v-loading="loading" :data="faultCaseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="故障名称" align="center" prop="faultName" />
      <el-table-column label="系统" align="center" prop="useSystem" />
      <el-table-column label="分系统" align="center" prop="subsystem" />
      <el-table-column label="机件名称" align="center" prop="partName" />
      <el-table-column label="专业" align="center" prop="specialty" />
      <el-table-column label="机型" align="center" prop="aircraftType" />
      <el-table-column label="故障原因" align="center" prop="faultCause" />
      <el-table-column label="案例编号" align="center" prop="caseNo" />
      <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:faultCase:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['fault:faultCase: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="60%" append-to-body @open="dialogOpen">
      <el-form ref="form" :model="form" :rules="rules"  label-width="100px" label-position="top">
        <el-row :gutter="10">
          <el-col :span="8"><el-form-item label="故障名称" prop="faultName">
              <el-input v-model="form.faultName" placeholder="请输入故障名称" />
            </el-form-item></el-col>
            <el-col :span="8"><el-form-item label="案例编号" prop="caseNo">
                <el-input v-model="form.caseNo" placeholder="请输入案例编号" />
              </el-form-item></el-col>
          <el-col :span="8"> <el-form-item label="系统" prop="useSystem">
              <el-input v-model="form.useSystem" placeholder="请输入系统" />
            </el-form-item></el-col>


        </el-row>
        <el-row :gutter="20">
          <el-col :span="8"><el-form-item label="分系统" prop="subsystem">
              <el-input v-model="form.subsystem" placeholder="请输入分系统" />
            </el-form-item></el-col>
          <el-col :span="8"><el-form-item label="机件名称" prop="partName">
              <el-input v-model="form.partName" placeholder="请输入机件名称" />
            </el-form-item></el-col>
          <el-col :span="8"><el-form-item label="专业" prop="specialty" >
            <el-input v-model="form.specialty" placeholder="请输入专业" />
            </el-form-item></el-col>


        </el-row>
        <el-row :gutter="20">

            <el-col :span="8"> <el-form-item label="故障原因" prop="faultCause">
                <el-input v-model="form.faultCause" type="textarea" placeholder="请输入内容" />
              </el-form-item></el-col>
          <el-col :span="8"><el-form-item label="故障描述" prop="faultDetail">
              <el-input v-model="form.faultDetail" type="textarea" placeholder="请输入内容"/>
            </el-form-item></el-col>
          <el-col :span="8"><el-form-item label="处置方式" prop="handlingMethod">
              <el-input v-model="form.handlingMethod" type="textarea" placeholder="请输入内容" />
            </el-form-item></el-col>
          <!--
          <el-col :span="6"> <el-form-item label="图片路径" prop="imagePath">
              <file-upload v-model="form.imagePath" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" style="width: 105%;"/>
            </el-form-item></el-col>-->
        </el-row>
        <el-row :gutter="20">
          <el-col :span="8"> <el-form-item label="文件路径" prop="filePath">
              <file-upload v-model="form.filePath" />
            </el-form-item></el-col>
          <!--
          <el-col :span="6"> <el-form-item label="图片路径" prop="imagePath">
              <file-upload v-model="form.imagePath" />
            </el-form-item></el-col>
          <el-col :span="6"><el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" style="width: 105%;"/>
            </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 { listFaultCase, getFaultCase, delFaultCase, addFaultCase, updateFaultCase } from "@/api/fault/faultCase";
import { listSpeciality, getSpeciality, delSpeciality, addSpeciality, updateSpeciality } from "@/api/basic/speciality";
import FileUpload from "@/components/FileUpload/upload.vue";
export default {
  name: "FaultCase",
  components: { FileUpload },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障案例表格数据
      faultCaseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        faultName: null,
        specialty: null,
        aircraftType: null,
        caseNo: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      specialityList:[]
    };
  },
  created() {
    this.getList();
    this.getSpList();
  },
  methods: {
    dialogOpen() {
      this.getSpList();
    },
    /** 查询故障案例列表 */
    getList() {
      this.loading = true;
      listFaultCase( this.queryParams ).then( response => {
        this.faultCaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      } );
    },
    getSpList() {
      const queryParams = {
        pageNum: 1,
        pageSize: 50
      };
      listSpeciality(queryParams).then(response => {
        this.specialityList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        faultName: null,
        useSystem: null,
        subsystem: null,
        partName: null,
        specialty: null,
        aircraftType: null,
        faultCause: null,
        faultDetail: null,
        handlingMethod: null,
        caseNo: null,
        filePath: null,
        imagePath: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remarks: 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
      getFaultCase( 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 ) {
            updateFaultCase( this.form ).then( response => {
              this.$modal.msgSuccess( "修改成功" );
              this.open = false;
              this.getList();
            } );
          } else {
            addFaultCase( 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 delFaultCase( ids );
      } ).then( () => {
        this.getList();
        this.$modal.msgSuccess( "删除成功" );
      } ).catch( () => { } );
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download( 'fault/faultCase/export', {
        ...this.queryParams
      }, `faultCase_${new Date().getTime()}.xlsx` )
    }
  }
};
</script>
