<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.dmsBillsMaintInstanceList[0].billsMaintCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="当前流程" >
            <el-input v-model="this.dmsBillsMaintInstanceList[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.dmsBillsMaintInstanceList[0].createBy" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="申请时间" >
            <el-input v-model="this.dmsBillsMaintInstanceList[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.dmsBillsMaintInstanceList[0].remark" disabled />
          </el-form-item>
        </el-col>

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






    <el-row>
      <!--    第一步-->
      <el-form ref="form" :model="form"  label-width="100px">
        <h4 class="form-header h4">{{this.wfProcessActivityList[0].processActivityName}}</h4>
        <el-col :span="8" >
          <el-form-item label="保养级别" prop="maintlevel">
            <el-select v-model="form.maintLevel" placeholder="保养级别" :disabled=true>
              <el-option
                v-for="item in dict.type.dms_maint_level"
                :key=" parseInt(item.value)"
                :label="item.label"
                :value="parseInt(item.value)">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="润滑组别" prop="maintGroup">
            <el-input v-model="form.maintGroup" placeholder="请输入检修组别"  :disabled=true />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="润滑负责人" prop="maintSupervisor">
            <el-input v-model="form.maintSupervisor" placeholder="请输入检修负责人" :disabled=true />
          </el-form-item>
        </el-col>


        <!--  中间步骤-->
        <el-form-item  v-for="(repairInstanceActivity,index) in dmsMaintInstanceActivityList"
                       :key="dmsMaintInstanceActivityList.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="maintLevel">
              <el-select v-model="form.maintLevel" placeholder="保养级别" :disabled=true>
                <el-option
                  v-for="item in dict.type.dms_maint_level"
                  :key="parseInt(item.value)"
                  :label="item.label"
                  :value="parseInt(item.value)"
                  v-bind:disabled="repairInstanceActivity.processStepOrder!=dmsMaintInstanceActivityList.length+1"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保养组别">
              <el-input
                v-model="repairInstanceActivity.maintGroup"
                v-bind:disabled="repairInstanceActivity.processStepOrder!=dmsMaintInstanceActivityList.length+1"
                placeholder="保养组别">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="保养负责人"
            >
              <el-input
                v-model="repairInstanceActivity.maintSupervisor"
                v-bind:disabled="repairInstanceActivity.processStepOrder!=dmsMaintInstanceActivityList.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!=dmsMaintInstanceActivityList.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.dmsMaintInstanceActivityList.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="maintLevel">
            <el-select v-model="form1.maintLevel" placeholder="保养等级" >
              <el-option
                v-for="item in dict.type.dms_maint_level"
                :key="parseInt(item.value)"
                :label="item.label"
                :value="parseInt(item.value)"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="保养组别"
          >
            <el-input
              v-model="form1.maintGroup"
              placeholder="保养组别">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="保养负责人"
          >
            <el-input
              v-model="form1.maintSupervisor"
              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>




  </div>
</template>

<script>
import { selectUserIdByDmsMaintInstanceActivityId,listDmsMaintInstanceActivity, getDmsMaintInstanceActivity, delDmsMaintInstanceActivity, addDmsMaintInstanceActivity, updateDmsMaintInstanceActivity } from "@/api/dms/dmsMaintInstanceActivity";
import { listWfprocessactivity } from '@/api/system/common/wfprocessactivity'
import { listDmsBillsMaintInstance } from '@/api/dms/dmsBillsMaintInstance'
import { selectUserIdByDmsBillsLubeInstanceActivityId } from '@/api/dms/dmsBillsLubeInstanceActivity'

export default {
  name: "DmsMaintInstanceActivity",
  dicts:['dms_bills_status','dms_instance_type','dms_maint_level'],
  data() {
    return {
      nowPorcessStepOrder:"",
      ListLength:"",
      //工单类型
      instanceTypeCheck:"",
      //工单状态
      billsStatusCheck:"",
      stepsNum:"",
      isUpdate:false,
      maintInstanceIdCheck:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      //润滑工单
      dmsBillsMaintInstanceList:[],
      // 保养工单实例节点表格数据
      dmsMaintInstanceActivityList: [],
      wfProcessActivityList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintInstanceId: null,
        processActivityId: null,
        maintLevel: null,
        maintGroup: null,
        maintSupervisor: null,
        processHandleResolution: null,
        processHandleStatus: null,
        processStepOrder: null,
        startTime: null,
        endTime: null,
        handleUserId: null,
        handleBy: null,
        handleTime: null,
        transferUserId: null,
        attr1: null
      },
      queryParamWfProcessActivity:{
        wfProcessId:null,
      },
      // 表单参数
      form: {},
      form1: {},
      // 表单校验
      rules: {
        maintInstanceId: [
          { required: true, message: "保养工单实例ID，关联dms_bills_maint_instance的maint_instance_id不能为空", trigger: "blur" }
        ],
        processHandleResolution: [
          { required: true, message: "处理意见;第一步不显示，从第二步开始只显示此字段不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    const maintInstanceId = this.$route.params && this.$route.params.maintInstanceId;
    console.log(this.dict.type.dms_maint_level)
    this.maintInstanceIdCheck = maintInstanceId;
    this.form.maintInstanceId = maintInstanceId;
    this.queryParams.maintInstanceId = maintInstanceId;
    this.queryParamWfProcessActivity.wfProcessId = 103;
    this.isUpdate = true;
    this.getList();
    this.getDmsBillsMaintInstance();
    this.getWfprocessActivity();
  },
  methods: {
    getselectUserIdByMaintInstanceId(){
      // console.log(this.nowPorcessStepOrder)
      // console.log(this.wfProcessActivityList.length)
      if (this.nowPorcessStepOrder==this.wfProcessActivityList.length){
        selectUserIdByDmsMaintInstanceActivityId(this.maintInstanceIdCheck,this.$store.state.user.id).then(response=>{
          console.log(response)

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

    },
    /** 查询保养工单列表 */
    getDmsBillsMaintInstance() {
      this.loading = true;
      listDmsBillsMaintInstance(this.queryParams).then(response => {
        this.dmsBillsMaintInstanceList = response.rows;
        for (let i = 0; i < this.dict.type.dms_bills_status.length;i++){
          if (this.dmsBillsMaintInstanceList[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.dmsBillsMaintInstanceList[0].instanceType==j+1){
            // console.log(this.dmsBillsMaintInstanceList)
            this.instanceTypeCheck = this.dict.type.dms_instance_type[j].label;
            // console.log(this.instanceTypeCheck)
          }
        }
        this.total = response.total;
        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.dmsMaintInstanceActivityList[i].processStepOrder==this.wfProcessActivityList[j].processActivityOrder){
              this.dmsMaintInstanceActivityList[i].name = this.wfProcessActivityList[j].processActivityName;
              console.log(this.dmsMaintInstanceActivityList[i].name)
            }
          }
        }

      })
    },
    /** 查询保养工单实例节点列表 */
    getList() {
      this.loading = true;
      listDmsMaintInstanceActivity(this.queryParams).then(response => {
        this.dmsMaintInstanceActivityList = response.rows;
        this.total = response.total;
        this.form.maintLevel = this.dmsMaintInstanceActivityList[0].maintLevel;
        this.form.maintGroup = this.dmsMaintInstanceActivityList[0].maintGroup;
        this.form.maintSupervisor = this.dmsMaintInstanceActivityList[0].maintSupervisor;
        this.form.remark = this.dmsMaintInstanceActivityList[0].remark;
        this.ListLength = this.dmsMaintInstanceActivityList.length;
        this.form1 = JSON.parse(JSON.stringify(this.dmsMaintInstanceActivityList[this.ListLength-1]))
        this.form1.processHandleResolution = null;
        this.nowPorcessStepOrder = this.ListLength+1;
        listWfprocessactivity(this.queryParamWfProcessActivity).then(response=>{
          this.wfProcessActivityList = response.rows;
          this.getselectUserIdByMaintInstanceId();
        })
        this.loading = false;

      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceActivityId: null,
        maintInstanceId: null,
        processActivityId: null,
        maintLevel: null,
        maintGroup: null,
        maintSupervisor: 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
      getDmsMaintInstanceActivity(instanceActivityId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改保养工单实例节点";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.instanceActivityId != null) {
            updateDmsMaintInstanceActivity(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)
              }
            }
            this.form1.instanceActivityId =null;
            //顺序自增
            this.form1.processStepOrder = this.form1.processStepOrder+1;
            this.form1.wfLength = this.wfProcessActivityList.length;
            addDmsMaintInstanceActivity(this.form1).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.getList();
              this.getDmsBillsMaintInstance();
              this.getWfprocessActivity();
              this.open = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const instanceActivityIds = row.instanceActivityId || this.ids;
      this.$modal.confirm('是否确认删除保养工单实例节点编号为"' + instanceActivityIds + '"的数据项？').then(function() {
        return delDmsMaintInstanceActivity(instanceActivityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/dmsMaintInstanceActivity/export', {
        ...this.queryParams
      }, `dmsMaintInstanceActivity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
