<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="工单编号" >
            <el-input v-model="this.dmsRepairInstanceList[0].billsRepairCode" disabled />
          </el-form-item>
        </el-col>
<!--        <el-col :span="8" :offset="2">-->
<!--          <el-form-item label="当前流程" >-->
<!--            <el-input v-model="this.dmsRepairInstanceList[0].wfProcessId" disabled />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="8" :offset="2">
          <el-form-item label="工单状态" >
            <el-input v-model="this.billsStatusCheck" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="申请人" >
            <el-input v-model="this.dmsRepairInstanceList[0].applyBy" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="申请时间" >
            <el-input v-model="this.dmsRepairInstanceList[0].applyTime" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="工单类型" >
            <el-input v-model="this.instanceTypeCheck" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="16" :offset="2">
          <el-form-item label="备注" >
            <el-input v-model="this.dmsRepairInstanceList[0].remark" disabled />
          </el-form-item>
        </el-col>

<!--        <el-col :span="8" :offset="2">-->
<!--          <el-form-item label="检修计划名称">-->
<!--            <el-input v-model="this.routeCheck" 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-row>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <h4 class="form-header h4">{{this.repairInstanceActivityList[0].name}}</h4>
          <el-col :span="8" >
          <el-form-item label="检修类型" prop="repairType">
            <el-radio-group v-model="form.repairType" :disabled=true >
              <el-radio
                v-for="dict in dict.type.dms_repair_type"
                :key="dict.value"
                :label="dict.value"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
      </el-col>
          <el-col :span="8">
            <el-form-item label="检修组别" prop="repairGroup">
              <el-input v-model="form.repairGroup" placeholder="请输入检修组别"  :disabled=true />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="检修负责人" prop="repairSupervisor">
              <el-input v-model="form.repairSupervisor" placeholder="请输入检修负责人" :disabled=true />
            </el-form-item>
          </el-col>

<!--  中间步骤-->
      <el-form-item  v-for="(repairInstanceActivity,index) in repairInstanceActivityList"
                     :key="repairInstanceActivityList.instanceActivityId"
                     label-width="0px"
                     :model="form"
                     v-if="repairInstanceActivity.processStepOrder!=1"
      >
        <h4 class="form-header h4">{{repairInstanceActivity.name}}</h4>
        <el-col :span="8">
          <el-form-item label="检修类型" prop="repairType">
            <el-radio-group v-model="form.repairType" >
              <el-radio
                v-for="dict in dict.type.dms_repair_type"
                :key="dict.value"
                :label="dict.value"
                v-bind:disabled="repairInstanceActivity.processStepOrder!=repairInstanceActivityList.length+1"
              >{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="8">
        <el-form-item label="检修组别">
        <el-input
          v-model="repairInstanceActivity.repairGroup"
          v-bind:disabled="repairInstanceActivity.processStepOrder!=repairInstanceActivityList.length+1"
          placeholder="检修组别">
        </el-input>
        </el-form-item>
        </el-col>
        <el-col :span="8">
        <el-form-item label="检修负责人"
        >
        <el-input
          v-model="repairInstanceActivity.repairSupervisor"
          v-bind:disabled="repairInstanceActivity.processStepOrder!=repairInstanceActivityList.length+1"
          placeholder="检修负责人">
        </el-input>
        </el-form-item>
        </el-col>
        <el-col :span="24">

        <el-form-item label="处理意见"
        >
        <el-input
          v-model="repairInstanceActivity.processHandleResolution"
          v-bind:disabled="repairInstanceActivity.processStepOrder!=repairInstanceActivityList.length+1"
          placeholder="处理意见">
        </el-input>
        </el-form-item>
        </el-col>
      </el-form-item>

     </el-form>
<!--新增审批流程表单显示-->
    <el-form ref="form1" :model="form1" label-width="100px"

             v-if="form1.processStepOrder!=wfProcessActivityList.length&&this.isUpdate==true">
      <h4 class="form-header h4">{{this.wfProcessActivityList[this.repairInstanceActivityList.length].processActivityName}}</h4>
      <el-col :span="8">
<!--        <el-form-item label="检修类型" prop="repairType"-->
<!--        >-->
<!--          <el-input-->
<!--            v-model="form1.repairType"-->
<!--            placeholder="检修类型"-->
<!--          >-->
<!--          </el-input>-->
<!--        </el-form-item>-->
        <el-form-item label="检修类型" prop="repairType">
          <el-radio-group v-model="form1.repairType"  >
            <el-radio
              v-for="dict in dict.type.dms_repair_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="检修组别"
        >
          <el-input
            v-model="form1.repairGroup"
            placeholder="检修组别">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="检修负责人"
        >
          <el-input
            v-model="form1.repairSupervisor"
            placeholder="检修负责人">
          </el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">

        <el-form-item label="处理意见"
        >
          <el-input
            v-model="form1.processHandleResolution"
            placeholder="处理意见">
          </el-input>
        </el-form-item>
      </el-col>
      <div  >
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-form>

    </el-row>
    <!--检修计划明细列表-->
    <h4 class="form-header h4" align="center">润滑计划详细信息</h4>

    <el-table v-loading="loading" :data="this.planRepairDetail" >
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="计划ID" align="center" prop="planRepairId" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="故障类别" align="center" prop="faultType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.dms_fault_type" :value="scope.row.faultType"/>
        </template>
      </el-table-column>
      <el-table-column label="检修说明" align="center" prop="repairDesc" />
      <el-table-column label="检修标准" align="center" prop="repairProtocol" />
      <el-table-column label="操作描述" align="center" prop="operationDescription" />

    </el-table>



<!--      <el-form-item label="处理意见" prop="processHandleResolution">-->
<!--        <el-input v-model="form.processHandleResolution" placeholder="请输入处理意见" />-->
<!--      </el-form-item>-->

<!--    删除分页会导致信息不显示-->
    <pagination
      v-show="total>100"
      :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" label-width="80px">

        <el-form-item label="检修类型" prop="repairType">
          <el-radio-group v-model="form.repairType">
            <el-radio
              v-for="dict in dict.type.dms_repair_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="检修组别" prop="repairGroup">
          <el-input v-model="form.repairGroup" placeholder="请输入检修组别" />
        </el-form-item>
        <el-form-item label="检修负责人" prop="repairSupervisor">
          <el-input v-model="form.repairSupervisor" placeholder="请输入检修负责人" />
        </el-form-item>
        <el-form-item label="处理意见" prop="processHandleResolution">
          <el-input v-model="form.processHandleResolution" placeholder="请输入处理意见" />
        </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 { selectUserIdByRepairInstanceId,listRepairInstanceActivity, getRepairInstanceActivity, delRepairInstanceActivity, addRepairInstanceActivity, updateRepairInstanceActivity } from "@/api/dms/repairInstanceActivity";
import { listDmsBillsFaultInstance } from '@/api/dms/dmsBillsFaultInstance'
import { listDmsRepairInstance } from '@/api/dms/dmsRepairInstance'
import { listWfprocessactivity } from '@/api/system/common/wfprocessactivity'
import { selectPlanLubeDetailByPlanLubeId } from '@/api/dms/dmsPlanLubeDetail'
import { selectPlanRepairDetailByPlanRepairId } from '@/api/dms/dmsRepairDetail'

export default {
  name: "RepairInstanceActivity",
  dicts:['dms_bills_status','dms_instance_type','dms_repair_type','dms_fault_type'],
  data() {
    return {
      //计划明细列表
      planRepairDetail:[],
      //计划id
      planRepairId:"",
      //当前步骤
      nowPorcessStepOrder:"",
      //当前登录用户id
      nowUserId:"",
      //判断是通过修改工单进入还是查看工单进入
      isUpdate:"",
      //流程集合
      wfProcessActivityList:[],
      //该流程步骤数量
      stepsNum:"",
      //集合长度
      ListLength:"",
      isDis1:false,
      //工单类型
      instanceTypeCheck:"",
      billsRepairCode:"",
      //工单状态
      billsStatusCheck:"",
      repairInstanceIdCheck:"",
      dmsRepairInstanceList:[],
      dmsBillsFaultInstanceList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 检修工单实例节点;最后一步作为检修记录表格数据
      repairInstanceActivityList: [],
      repairInstanceActivity: [],
      userIdList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairInstanceId: null,
        processActivityId: null,
        repairType: null,
        repairGroup: null,
        repairSupervisor: null,
        processHandleResolution: null,
        processHandleStatus: null,
        processStepOrder: null,
        startTime: null,
        endTime: null,
        handleUserId: null,
        handleBy: null,
        handleTime: null,
        transferUserId: null,
        attr1: null
      },
      queryParamRepairInstance: {
        pageNum: 1,
        pageSize: 10,
        repairInstanceId:null,
      },
      queryParamWfProcessActivity:{
        wfProcessId:null,
      },
      // 表单参数
      form: {},
      form1:{},
      // 表单校验
      rules: {
        repairInstanceId: [
          { required: true, message: "维修工单实例ID，关联dms_bills_repair_instance的repair_instance_id不能为空", trigger: "blur" }
        ],
        processHandleStatus: [
          { required: true, message: "状态(0已结束，1执行中，2、待接取，3、已转发）不能为空", trigger: "change" }
        ],
        processStepOrder: [
          { required: true, message: "工单流程步骤顺序不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const repairInstanceIdCheck = this.$route.params && this.$route.params.repairInstanceId;
    this.repairInstanceIdCheck = repairInstanceIdCheck;
    this.form.repairInstanceId = repairInstanceIdCheck;
    this.queryParams.repairInstanceId = repairInstanceIdCheck;
    this.queryParamRepairInstance.repairInstanceId = repairInstanceIdCheck;
    let flag=1;
    this.getBillsFaultInstance();
    this.getList();
    //通过修改工单进入
    if(this.$route.name == "childInstanceUpdate"){
      this.isUpdate = true;
      //通过repairInstanceId查询工单实例表，查出工单流程节点id
      //通过工单流程节点id查询出去查询用户
      //如果最后一步已经完成，则不需要权限判定
      // if (this.nowPorcessStepOrder!=this.wfProcessActivityList.length){
      //   alert(this.nowPorcessStepOrder)
      //   alert(this.wfProcessActivityList.length)
      //  flag = 0;
      // }
      // if (flag!=0) {
      //   this.getselectUserIdByRepairInstanceId();
      // }
    }
    else if(this.$route.name == "childInstanceSelect"){
      this.isUpdate = false;
    }

    console.log(this.form)

  },
  methods: {
    getselectUserIdByRepairInstanceId(){
      // alert(this.wfProcessActivityList.length)
      if (this.nowPorcessStepOrder==this.wfProcessActivityList.length){
        selectUserIdByRepairInstanceId(this.repairInstanceIdCheck,this.$store.state.user.id).then(response=>{
          console.log("=========================================================")

          if (response == false) {
            alert("无权限")
            this.$store.dispatch("tagsView/delView",this.$route)
            this.$router.replace({path:"/dms/planRepair/dmsRepairInstance"})
          }
        })
      }

    },
    /** 查询检修工单明细*/
    selectDmsBillsRepairDetail(){
      selectPlanRepairDetailByPlanRepairId(this.planRepairId).then(response=>{
        console.log("===================================================================================")
        console.log(response)
        this.planRepairDetail = response.rows;
        console.log("===================================================================================")
        this.loading = false
      })
    },
    /**查询流程信息*/
    getWfprocessActivity(){
      listWfprocessactivity(this.queryParamWfProcessActivity).then(response=>{
        this.wfProcessActivityList = response.rows;
        console.log(this.wfProcessActivityList)

        this.stepsNum = this.wfProcessActivityList.length;
        for (let i=0;i<this.stepsNum;i++){
          for (let j=0;j<this.stepsNum;j++){
            if (this.repairInstanceActivityList[i].processStepOrder==this.wfProcessActivityList[j].processActivityOrder){
              this.repairInstanceActivityList[i].name = this.wfProcessActivityList[j].processActivityName;
              console.log(this.repairInstanceActivityList[i].name)
            }
          }
        }

      })
    },

    /** 查询检修工单;一是根据检修计划自动创建的检修工单，二是手工创建故障报修工单列表 */
    getBillsFaultInstance() {
      listDmsRepairInstance(this.queryParamRepairInstance).then(response => {
        this.dmsRepairInstanceList = response.rows;
        this.planRepairId = response.rows[0].planRepairId;

        for (let i = 0; i < this.dict.type.dms_bills_status.length;i++){
          if (this.dmsRepairInstanceList[0].billsStatus==i){
            this.billsStatusCheck = this.dict.type.dms_bills_status[i].label;
          }
        }
        for (let j = 0; j < this.dict.type.dms_instance_type.length;j++){
          //因为字典dms_instance_type没有键值0，所以从==j+1
          if (this.dmsRepairInstanceList[0].instanceType==j+1){
            console.log(this.dmsRepairInstanceList)
            this.instanceTypeCheck = this.dict.type.dms_instance_type[j].label;
            console.log(this.instanceTypeCheck)
          }
        }
        this.queryParamWfProcessActivity.wfProcessId = this.dmsRepairInstanceList[0].wfProcessId;
        this.getWfprocessActivity();
        this.selectDmsBillsRepairDetail();

      });
    },
    /** 查询检修工单实例节点;最后一步作为检修记录列表 */
    getList() {
      this.loading = true;
      listRepairInstanceActivity(this.queryParams).then(response => {
        this.repairInstanceActivityList = response.rows;
        this.total = response.total;
         // this.isDis1 = true;
         this.form.repairType = this.repairInstanceActivityList[0].processHandleStatus;
         this.form.repairGroup = this.repairInstanceActivityList[0].repairGroup;
         this.form.repairSupervisor = this.repairInstanceActivityList[0].repairSupervisor;
         this.form.remark = this.repairInstanceActivityList[0].remark;
        this.ListLength = this.repairInstanceActivityList.length;
        this.form1 = JSON.parse(JSON.stringify(this.repairInstanceActivityList[this.ListLength-1]))
        this.form1.processHandleResolution = null;
        this.nowPorcessStepOrder = this.ListLength+1;
          listWfprocessactivity(this.queryParamWfProcessActivity).then(response=>{
            this.wfProcessActivityList = response.rows;
            this.getselectUserIdByRepairInstanceId();}),

        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceActivityId: null,
        repairInstanceId: null,
        processActivityId: null,
        repairType: null,
        repairGroup: null,
        repairSupervisor: null,
        processHandleResolution: null,
        remark: null,
        processHandleStatus: null,
        processStepOrder: null,
        startTime: null,
        endTime: null,
        handleUserId: null,
        handleBy: null,
        handleTime: null,
        createBy: null,
        createTime: null,
        transferUserId: null,
        attr1: 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.instanceActivityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加检修工单实例节点;最后一步作为检修记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const instanceActivityId = row.instanceActivityId || this.ids
      getRepairInstanceActivity(instanceActivityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改检修工单实例节点;最后一步作为检修记录";
      });
    },
    // submitform(){
    //   this.form.instanceActivityId=1;
    //   this.form.repairInstanceId = this.repairInstanceIdCheck;
    //   this.form.processStepOrder = 1;
    //   updateRepairInstanceActivity(this.form).then(response => {
    //     this.$modal.msgSuccess("修改成功");
    //     this.open = false;
    //     this.getList();
    //   })
    //
    // },
    /** 提交按钮 */
    submitForm() {
      // alert(1)
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form.instanceActivityId != null) {
            updateRepairInstanceActivity(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();

            });
          } else {
            for (let i = 0; i < this.wfProcessActivityList.length;i++){
              if (this.form1.processStepOrder+1 == this.wfProcessActivityList[i].processActivityOrder){
                this.form1.processActivityId = this.wfProcessActivityList[i].processActivityId;
              // alert(this.wfProcessActivityList[i].processActivityId)
              }
            }
            // console.log(this.repairInstanceActivityList)
            this.form1.instanceActivityId =null;
            //顺序自增
            this.form1.processStepOrder = this.form1.processStepOrder+1;
            this.form1.wfLength = this.wfProcessActivityList.length;

            // console.log(this.form1)
            addRepairInstanceActivity(this.form1).then(response => {
                this.$store.dispatch("tagsView/delView",this.$route)
                this.$router.replace({path:"/dms/planRepair/dmsRepairInstance"})
              // this.$modal.msgSuccess("新增成功");
              // this.open = false;
              // this.getList();
              // this.getBillsFaultInstance();
            }
            );
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const instanceActivityIds = row.instanceActivityId || this.ids;
      this.$modal.confirm('是否确认删除检修工单实例节点;最后一步作为检修记录编号为"' + instanceActivityIds + '"的数据项？').then(function() {
        return delRepairInstanceActivity(instanceActivityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/repairInstanceActivity/export', {
        ...this.queryParams
      }, `repairInstanceActivity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
