<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.dmsBillsInstanceList[0].billsInspectCode" disabled />
          </el-form-item>
        </el-col>
<!--        <el-col :span="8" :offset="2">-->
<!--          <el-form-item label="当前流程" >-->
<!--            <el-input v-model="this.dmsBillsInstanceList[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.dmsBillsInstanceList[0].createBy" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="创建时间" >
            <el-input v-model="this.dmsBillsInstanceList[0].createTime" 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.dmsBillsInstanceList[0].remark" disabled />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>

    <el-form  ref="form" :model="form"  label-width="120px">
      <el-row>
        <h4 class="form-header h4">{{this.wfProcessActivityList[0].processActivityName}}</h4>
        <el-col :span="8" >
          <el-form-item label="巡检线路" prop="inspectRouteId">
            <el-input v-model="form.routeName" placeholder="" :disabled="true"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备总数" prop="deviceAmount">
            <el-input v-model="form.deviceAmount"  placeholder="请输入检修组别" :disabled=true />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执行人员" prop="performer">
            <el-input v-model="form.performer"  :disabled=true />
          </el-form-item>
        </el-col>
      </el-row>
      <!--  中间步骤-->
      <el-form-item  v-for="(dmsBillsInspectActivity,index) in dmsBillsInspectActivityList"
                     :key="dmsBillsInspectActivityList.instanceActivityId"
                     label-width="0px"
                     :model="form"
                     v-if="dmsBillsInspectActivity.processStepOrder!=1"
      >
        <h4 class="form-header h4">{{dmsBillsInspectActivity.processActivityName}}</h4>
          <el-col :span="8">
          <el-form-item label="检修类型" prop="inspectRouteId">
<!--            <el-input v-model="form.routeName" placeholder="" :disabled="true"/>-->
            <el-input
              v-model="form.routeName"
              v-bind:disabled="dmsBillsInspectActivity.processStepOrder!=dmsBillsInspectActivityList.length+1"
              placeholder="检修类型">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检修组别">
            <el-input
              v-model="dmsBillsInspectActivity.deviceAmount"
              v-bind:disabled="dmsBillsInspectActivity.processStepOrder!=dmsBillsInspectActivityList.length+1"
              placeholder="检修组别">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="检修负责人"
          >
            <el-input
              v-model="dmsBillsInspectActivity.performer"
              v-bind:disabled="dmsBillsInspectActivity.processStepOrder!=dmsBillsInspectActivityList.length+1"
              placeholder="检修负责人">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">

          <el-form-item label="处理意见"
          >
            <el-input
              v-model="dmsBillsInspectActivity.processHandleResolution"
              v-bind:disabled="dmsBillsInspectActivity.processStepOrder!=dmsBillsInspectActivityList.length+1"
              placeholder="">
            </el-input>
          </el-form-item>
        </el-col>
      </el-form-item>
      <!--新增审批流程表单显示-->
      <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.dmsBillsInspectActivityList.length].processActivityName}}</h4>
        <el-col :span="8">
          <el-form-item label="巡检线路" prop="inspectRouteId">
            <el-select v-model="form1.inspectRouteId" placeholder="巡检线路">
              <el-option
                v-for="item in dmsBaseInspectRouteList"
                :key="item.inspectRouteId"
                :label="item.routeName"
                :value="item.inspectRouteId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备总数"
          >
            <el-input
              v-model="form1.deviceAmount"
              placeholder="设备总数">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="执行人员"
          >
            <el-input
              v-model="form1.performer"
              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-form>
    <!--润滑计划明细列表-->
    <h4 class="form-header h4" align="center">点巡检线路详细信息</h4>
    <!-- 从这里 点击跳转，通过巡检步骤与设备id查询出《点巡检工单明细的主键》 将主键传入后台
    通过主键查询点巡检工单明细项目信息的列表，将列表返回到新的页面
    -->
    <el-table v-loading="loading" :data="this.inspectRouteLists" >
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="巡检步骤" align="center" prop="lineStep" />
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="巡检标准" align="center" prop="standardName" />
      <el-table-column label="操作描述" align="center" prop="operationDescription" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            style="float: none"
            size="mini"
            type="text"
            icon="el-icon-bell"
            @click="getJump(scope.row)"
            v-hasPermi="['dms:dmsInspectRouteDetail:edit']"
          >查看详情</el-button>
        </template>
      </el-table-column>
    </el-table>





  </div>
</template>

<script>
import { listDmsBillsInspectActivity, getDmsBillsInspectActivity, delDmsBillsInspectActivity, addDmsBillsInspectActivity, updateDmsBillsInspectActivity } from "@/api/dms/dmsBillsInspectActivity";
import { listWfprocessactivity } from '@/api/system/common/wfprocessactivity'
import { listDmsBillsInstance } from '@/api/dms/dmsBillsInstance'
import { listDmsBaseInspectRoute } from '@/api/dms/dmsBaseInspectRoute'
import {selectUserIdByInspectInstanceId} from '@/api/dms/dmsBillsInspectActivity'
import { selectInspectRouteListByInspectRouteId} from '@/api/dms/dmsInspectRouteDetail'

export default {
  name: "DmsBillsInspectActivity",
  dicts:['dms_inspect_status','dms_instance_type','dms_repair_type','dms_inspect_type'],
  data() {
    return {
      //工单id
      inspectInstanceId:"",
      //线路明细列表
      inspectRouteLists:[],
      //线路id
      selectInspectRouteId:"",
      queryParamWfProcessActivity:{
        wfProcessId:null,
      },
      ListLength:"",
      nowPorcessStepOrder:"",
      dmsBaseInspectRouteList:[],
      //判断是通过修改工单进入还是查看工单进入
      isUpdate:"",
      billsStatusCheck:"",
      //工单状态
      instanceTypeCheck:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 点巡检工单实例节点表格数据
      dmsBillsInspectActivityList: [],
      repairInstanceActivityList:[],
      dmsBillsInstanceList:[],
      dmsBillsInspectActivity:[],
      wfProcessActivityList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inspectInstanceId: null,
        processActivityId: null,
        inspectRouteId: null,
        deviceAmount: null,
        performer: null,
        processHandleResolution: null,
        processHandleStatus: null,
        processStepOrder: null,
        startTime: null,
        endTime: null,
        handleUserId: null,
        handleBy: null,
        handleTime: null,
        transferUserId: null,
        attr1: null
      },
      // 表单参数
      form: {inspectRouteId:null},
      form1:{},
      // 表单校验
      rules: {
        inspectInstanceId: [
          { required: true, message: "巡检工单实例ID，关联dms_bills_inspect_instance的inspect_instance_id不能为空", trigger: "blur" }
        ],
        processHandleResolution: [
          { required: true, message: "处理意见;第一步不显示，从第2步开始只显示此字段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const inspectInstanceId = this.$route.params && this.$route.params.inspectInstanceId;
    this.form.inspectInstanceId = inspectInstanceId;
    this.inspectInstanceId = inspectInstanceId;
    this.queryParams.inspectInstanceId = inspectInstanceId;
    this.queryParamWfProcessActivity.wfProcessId = 105;
    let flag=1;
    this.getList();
    //通过修改工单进入
    if(this.$route.name == "childDmsBillsInspectActivity"){
      this.isUpdate = true;
      //通过repairInstanceId查询工单实例表，查出工单流程节点id
      //通过工单流程节点id查询出去查询用户
      //如果最后一步已经完成，则不需要权限判定

    }
    else if(this.$route.name == "selectDmsBillsInspectActivity"){
      this.isUpdate = false;
    }

    this.getDmsBillsInstance();
    this.getRoute();
    this.getWfprocessActivity();
  },
  methods: {
    getJump(row){
      // alert(row)
      row.inspectInstanceId =this.inspectInstanceId;
      console.log(row)
      this.$router.push({path:'/dms/inspect/dmsInspectInstanceDetailProject',query:{inspectInstanceId:row.inspectInstanceId,lineStep:row.lineStep}})
    },
      getselectUserIdByInspectInstanceId(){
        if (this.nowPorcessStepOrder==this.wfProcessActivityList.length){
        selectUserIdByInspectInstanceId(this.form.inspectInstanceId,this.$store.state.user.id).then(response=>{
          console.log(response)
        console.log("=========================================================")
        if (response == false) {
          alert("无权限")
          this.$store.dispatch("tagsView/delView",this.$route)
          this.$router.replace({path:"/dms/inspect/dmsBillsInstance"})
        }
      })
    }},
    getRoute() {
      listDmsBaseInspectRoute(this.queryParams).then(response => {
        this.dmsBaseInspectRouteList = response.rows;
      });
    },
    /** 查询巡检工单列表 */
    getDmsBillsInstance() {
      this.loading = true;
      listDmsBillsInstance(this.queryParams).then(response => {
        this.dmsBillsInstanceList = response.rows;
        // this.
        //将0/1转化为字典label
        for (let i = 0; i < this.dict.type.dms_inspect_status.length;i++){
          if (this.dmsBillsInstanceList[0].inspectStatus==i){
            this.billsStatusCheck = this.dict.type.dms_inspect_status[i].label;
          }
        }
        for (let j = 0; j < this.dict.type.dms_inspect_type.length;j++){
          //因为字典dms_instance_type没有键值0，所以从==j+1
          if (this.dmsBillsInstanceList[0].inspectType==j+1){
            this.instanceTypeCheck = this.dict.type.dms_inspect_type[j].label;
          }
        }

      });
    },
    /**查询流程信息*/
    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.dmsBillsInspectActivityList[i].processStepOrder==this.wfProcessActivityList[j].processActivityOrder){
              this.dmsBillsInspectActivityList[i].processActivityName = this.wfProcessActivityList[j].processActivityName;
              console.log(this.dmsBillsInspectActivityList[i].processActivityName)
            }
          }
        }
        this.loading = false;
      })
    },
    /** 通过线路id查询线路明细信息*/
    // inspectRouteLists
      getInspectRouteLists(){
      selectInspectRouteListByInspectRouteId(this.selectInspectRouteId).then(response=>{
        this.inspectRouteLists = response.rows
        this.loading = false;
      })
    },

    /** 查询点巡检工单实例节点列表 */
    getList() {
      this.loading = true;
      listDmsBillsInspectActivity(this.queryParams).then(response => {
        this.dmsBillsInspectActivityList = response.rows;
        this.selectInspectRouteId = response.rows[0].inspectRouteId;
        console.log("=========================================================")
        console.log(this.selectInspectRouteId)
        this.getInspectRouteLists();
        // console.log(this.dmsBillsInspectActivityList)
        this.total = response.total;
        this.form.inspectRouteId = this.dmsBillsInspectActivityList[0].inspectRouteId;
        this.form.routeName = this.dmsBillsInspectActivityList[0].routeName;
        this.form.deviceAmount = this.dmsBillsInspectActivityList[0].deviceAmount;
        this.form.performer = this.dmsBillsInspectActivityList[0].performer;
        this.ListLength = this.dmsBillsInspectActivityList.length;
        this.form1 = JSON.parse(JSON.stringify(this.dmsBillsInspectActivityList[this.ListLength-1]))
        this.form1.processHandleResolution = null;
        this.nowPorcessStepOrder = this.ListLength+1;
        listWfprocessactivity(this.queryParamWfProcessActivity).then(response=>{
            this.wfProcessActivityList = response.rows;
            this.getselectUserIdByInspectInstanceId();
          })



      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceActivityId: null,
        inspectInstanceId: null,
        processActivityId: null,
        inspectRouteId: null,
        deviceAmount: null,
        performer: 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
      getDmsBillsInspectActivity(instanceActivityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改点巡检工单实例节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form1"].validate(valid => {
        if (valid) {
          if (this.form.instanceActivityId != null) {
            updateDmsBillsInspectActivity(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)
            addDmsBillsInspectActivity(this.form1).then(response => {
                this.$store.dispatch("tagsView/delView",this.$route)
                this.$router.replace({path:"/dms/inspect/dmsBillsInstance"})
                // this.$modal.msgSuccess("新增成功");
                // this.open = false;
                // this.getList();
                // this.getBillsFaultInstance();
              }
            );
            // addDmsBillsInspectActivity(this.form).then(response => {
            //   this.$modal.msgSuccess("新增成功");
            //   this.open = false;
            //   this.getList();
            // });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const instanceActivityIds = row.instanceActivityId || this.ids;
      this.$modal.confirm('是否确认删除点巡检工单实例节点编号为"' + instanceActivityIds + '"的数据项？').then(function() {
        return delDmsBillsInspectActivity(instanceActivityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dmsBillsInspectActivity/export', {
        ...this.queryParams
      }, `dmsBillsInspectActivity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
