<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="toolMaintenanceCode">
        <el-input
          v-model="queryParams.toolMaintenanceCode"
          placeholder="请输入维护保养编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="单据状态" prop="maintenanceStatus">
        <el-select v-model="queryParams.maintenanceStatus" placeholder="请选择单据状态" clearable>
          <el-option 
          v-for="dict in dict.mes_maintenance_status"
          :key="dict.value"
          :label="dict.label" 
          :value="dict.value" />
        </el-select>
      </el-form-item> -->
          <el-form-item label="单据状态" prop="maintenanceStatus">
        <el-select v-model="queryParams.maintenanceStatus" placeholder="请选择单据状态" clearable>
          <el-option
            v-for="dict in dict.type.mes_maintenance_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
         </el-form-item>
      <el-form-item label="物料编码" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工装夹具名称" prop="toolName">
        <el-input
          v-model="queryParams.toolName"
          placeholder="请输入工装夹具名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="SN编码" prop="toolCode">
        <el-input
          v-model="queryParams.toolCode"
          placeholder="请输入SN编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存放位置" prop="location">
        <el-input
          v-model="queryParams.location"
          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="['md:maintenance:add']"
        >创建</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-query"
          size="mini"
          @click="handleView"
          v-hasPermi="['md:maintenance:query']"
        >查看</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['md:maintenance:remove']"
        >开始维护</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate(new Date().getTime)"
          v-hasPermi="['tm:maintenance:edit']"
        >结束维护</el-button>
      </el-col>
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="maintenanceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="维护保养单号" align="center" prop="toolMaintenanceCode" />
<!-- 
       <el-table-column label="单据状态" align="center" prop="maintenanceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_maintenance_status" :value="scope.row.maintenanceStatus"/>
        </template>
      </el-table-column> -->
         <el-table-column label="单据状态" align="center" prop="maintenanceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_maintenance_status" :value="scope.row.maintenanceStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="维护类型" align="center" prop="maintenanceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_course_type" :value="scope.row.maintenanceType"/>
        </template>
      </el-table-column>

       
      <el-table-column label="物料编码" align="center" prop="itemCode" />
      <el-table-column label="工装夹具名称" align="center" prop="toolName" />
      <el-table-column label="SN编码" align="center" prop="toolCode" />
      <el-table-column label="存放位置" align="center" prop="location" />
      <el-table-column label="维护人" align="center" prop="processPerson" />
      
      
        <el-table-column label="开始时间" align="center" prop="startTime" />
        <el-table-column label="结束时间" align="center" prop="endTime" />
     <el-table-column label="维护时间" align="center" prop="processTime" />
      <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-query"
            @click="handleView(scope.row)"
            v-hasPermi="['md:maintenance:query']"
          >查看</el-button>
         <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDe(scope.row)"
            v-hasPermi="['tm:maintenance: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" 
      :disabled="form.toolMaintenanceId&&optType!='add'"
      label-width="80px" >
        <!-- <el-form-item label="单据状态">
          <el-radio-group v-model="form.maintenanceStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <!-- <el-form-item label="物料编码" prop="itemCode">
          <el-input v-model="form.itemCode" placeholder="请输入物料编码" />
        </el-form-item> -->
         <el-form-item label="物料编码" prop="itemCode">
              <el-input v-model="form.itemCode" placeholder="请选择物料号" readonly="readonly" >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleBrandSelect"
                   :disabled="form.toolMaintenanceId"
                   
                ></el-button>
              </el-input>
              <BrandSelect
              ref="brSelect"
              @onSelected="onBrandSelected"
            ></BrandSelect>
            </el-form-item>
 

        
        <el-form-item label="名称" prop="toolName">
          <el-input v-model="form.toolName" readonly="readonly" />
        </el-form-item>
        <el-form-item label="SN编码" prop="toolCode">
          <el-input v-model="form.toolCode" readonly="readonly" />
        </el-form-item>
        <el-form-item label="存放位置" prop="location" >
          <el-input 
          v-model="form.location" 
           readonly="readonly"
          
          ></el-input>
        </el-form-item>
        
        <el-form-item label="维护类型" prop="maintenanceType">
              <el-select v-model="form.maintenanceType" placeholder="请选择维护类型">
                <el-option
                  v-for="dict in dict.type.mes_course_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
      </el-form>
      
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="optType != 'view'">确 定</el-button>
        <el-button @click="cancel" v-if="optType != 'view'">取 消</el-button>
      </div>
    </el-dialog>
     <!-- 结束维护 -->
    <el-dialog :title="title" :visible.sync="opena" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<el-form-item label="物料编码" prop="itemCode">
          <el-input v-model="form.itemCode" readonly="readonly" />
        </el-form-item>
         <el-form-item label="工装夹具名称" prop="maintenanceType">
          <el-input v-model="form.maintenanceType" readonly="readonly" />
        </el-form-item>
        <el-form-item label="SN编码" prop="toolCode">
          <el-input v-model="form.toolCode" readonly="readonly" />
        </el-form-item>
        <el-form-item label="存放位置" prop="location">
          <el-input 
          v-model="form.location" placeholder="请选择维护类型"
        ></el-input>
        </el-form-item>
        <el-form-item label="维护人" prop="processPerson">
          <el-input v-model="form.processPerson" readonly="readonly" />
        </el-form-item>
        
       <el-form-item label="维护时间" prop="processTime">
          <el-input v-model="form.processTime" readonly="readonly" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-input v-model="form.startTime" readonly="readonly" />
        </el-form-item>
        
        
           <el-form-item label="结束时间" prop="endTime">
          <el-input v-model="form.endTime" readonly="readonly" />
        </el-form-item>
       
        
        
         <!-- <el-form-item label="维护状态">
          <el-radio-group v-model="form.processStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="剩余寿命" prop="lifeTime">
          <el-input v-model="form.lifeTime" placeholder/>
        </el-form-item>
        <el-form-item label="维护后状态" prop="processStatus">
              <el-select v-model="form.processStatus" placeholder="请选择分维护后状态">
                <el-option
                  v-for="dict in dict.type.mes_to_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
      
      
        <el-form-item label="维护描述" prop="remark">
          <el-input v-model="form.remark" 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 { listMaintenance, getMaintenance, delMaintenance, addMaintenance, updateMaintenance,delMaintenan } from "@/api/mes/tm/maintenance";
import BrandSelect from "@/components/TmTool/index.vue";
export default {
  components: {BrandSelect},
   dicts: ['mes_maintenance_status','mes_course_type','mes_to_status'],
  name: "Maintenance",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 要维护刀模板表格数据
      maintenanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
     // 是否显示弹出层
      opena: false,
      selections: [],
      
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        toolMaintenanceId: null,
        toolMaintenanceCode: null, 
        toolId: null,
        maintenanceStatus: null, 
        maintenanceType: null,       
        itemCode: null,        
        toolName: null,        
        toolCode: null,        
        location: null, 
        processId: null,
        processPerson: null,
        lifeTime: null,
        processTime: null,
        startTime: null,
        endTime: null,


         },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
   
    /** 查询要维护刀模板列表 */
    getList() {
      this.loading = true;
      listMaintenance(this.queryParams).then(response => {
        this.maintenanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.opena = false;
      this.reset();
    },
    
    // 表单重置
    reset() {
      this.form = {
        toolMaintenanceId: null,
        toolMaintenanceCode: null, 
        toolId: null,
        maintenanceStatus: null, 
        maintenanceType: null,       
        itemCode: null,        
        toolName: null,        
        toolCode: null,        
        location: null, 
        processId: null,
        processPerson: null,
        lifeTime: null,
        processTime: null,
        startTime: null,
        endTime: null,   };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    
     handleBrandSelect(){
      this.$refs.brSelect.showFlag = true;
    },
    onBrandSelected(row){
      if(row != undefined && row != null){
        this.form.toolId = row.toolId;
        this.form.toolName = row.toolName;
        this.form.toolCode = row.toolCode;
        this.form.itemCode = row.itemCode;
        this.form.location = row.location;
      }
    },



 
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.toolMaintenanceId)
      this.selections = selection

      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建工装台账维护单";
      this.optType = "add";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
       if (this.selections.some(v => v.endTime)) {
        return 
      }
       if (this.selections.some(v => v.startTime==null)) {
        return 
      }
      this.reset();
      const toolMaintenanceId = row.toolMaintenanceId || this.ids
      getMaintenance(toolMaintenanceId).then(response => {
        this.form = response.data;
        this.opena = true;
        this.title = "结束维护";
        
      });
    },
    //  查询明细按钮操作
    handleView(row){
      this.reset();
      const toolMaintenanceId = row.toolMaintenanceId || this.ids;
      getMaintenance(toolMaintenanceId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看工装夹具信息";
        this.optType = "view";
      });
    },
 

   
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.toolMaintenanceId != null) {
            updateMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("维护结束");
              this.opena = false;2
              this.getList();
            });
          } else {
            addMaintenance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 维护 */
    handleDelete(row) {
      if (this.selections.some(v => v.startTime)) {
        return 
      }
      const toolMaintenanceId = row.toolMaintenanceId || this.ids;
      this.$modal.confirm('是否确认要维护刀模板编号为"' + toolMaintenanceId + '"的数据项？').then(function() {
        return delMaintenance(toolMaintenanceId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("开始维护");
      }).catch(() => {});
    },
     /** 删除按钮操作 */
    handleDe(row) {
      const toolMaintenanceId = row.toolMaintenanceId || this.ids;
      this.$modal.confirm('是否确认删除台账维护编号为"' + toolMaintenanceId + '"的数据项？').then(function() {
        return delMaintenan(toolMaintenanceId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('md/maintenance/export', {
        ...this.queryParams
      }, `maintenance_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
