<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.dmsBillsFaultInstanceList[0].billsFaultCode" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="报修来源" >
            <el-input v-model="this.faultSourceTypeCheck" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="报修来源编号" >
            <el-input v-model="this.dmsBillsFaultInstanceList[0].faultSourceId" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="工单状态" >
            <el-input  placeholder="待维修" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="创建人" >
            <el-input v-model="this.dmsBillsFaultInstanceList[0].createBy" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="创建时间" >
            <el-input v-model="this.dmsBillsFaultInstanceList[0].createTime" disabled />
          </el-form-item>
        </el-col>

        <el-col :span="16" :offset="2">
          <el-form-item label="备注" >
            <el-input v-model="this.dmsBillsFaultInstanceList[0].remark" disabled />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <!--  第一步-->
    <el-row>
    <el-form  ref="form" :model="form"  label-width="120px">

        <h4 class="form-header h4">{{this.wfProcessActivityList[0].processActivityName}}</h4>
        <el-col :span="8" >
          <el-form-item label="故障类型" prop="faultType">
<!--            <el-input v-model="form.faultType" placeholder="" :disabled="true"/>-->
            <el-select v-model="form.faultType" placeholder="" :disabled="true">
              <el-option
                v-for="item in dict.type.dms_fault_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="故障描述" prop="faultDescription">
            <el-input v-model="form.faultDescription"  placeholder="" :disabled=true />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="涉及操作" prop="designOperations">
            <el-input v-model="form.designOperations"  :disabled=true />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备名称" prop="deviceId">
            <el-select   v-model="form.deviceId" placeholder="" :disabled=true >
              <el-option
                v-for="item in ledgerList"
                :key="item.deviceId"
                :label="item.deviceName"
                :value="item.deviceId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="维修类型" prop="repairType">
            <el-select   v-model="form.repairType" placeholder="" :disabled=true >
              <el-option
                v-for="item in dict.type.dms_repair_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
<!--            <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="outsrcId">
            <el-select   v-model="form.outsrcId" placeholder="" :disabled=true>
              <el-option
                v-for="item in dmsInfoList"
                :key="item.outsrcId"
                :label="item.outsrcName"
                :value="item.outsrcId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>



    <!--  中间步骤-->
    <el-form-item  v-for="(activity,index) in activityList"
                   :key="activityList.instanceActivityId"
                   label-width="0px"
                   :model="form"
                   v-if="activity.processStepOrder!=1"
    >
      <h4 class="form-header h4">{{activity.processActivityName}}</h4>
      <el-col :span="8">
        <el-form-item label="故障类型" prop="faultType">
          <el-select v-model="activity.faultType" placeholder="" :disabled="true">
            <el-option
              v-bind:disabled="activity.processStepOrder!=activityList.length+1"
              v-for="item in dict.type.dms_fault_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="故障描述" prop="faultDescription">
          <el-input v-model="activity.faultDescription"
                    v-bind:disabled="activity.processStepOrder!=activityList.length+1"
                    placeholder="故障描述" :disabled=true />
        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="涉及操作" prop="designOperations">
          <el-input v-model="activity.designOperations"
                    v-bind:disabled="activity.processStepOrder!=activityList.length+1"
                    :disabled=true />
        </el-form-item>
      </el-col>
      <el-col :span="8">
          <el-form-item label="设备名称" prop="deviceId">
            <el-select   v-model="activity.deviceId" placeholder="" :disabled=true >
              <el-option
                v-bind:disabled="activity.processStepOrder!=activityList.length+1"
                v-for="item in ledgerList"
                :key="item.deviceId"
                :label="item.deviceName"
                :value="item.deviceId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      <el-col :span="8">
        <el-form-item label="维修类型" prop="repairType">
          <el-select   v-model="activity.repairType" placeholder="" :disabled=true >
            <el-option
              v-bind:disabled="activity.processStepOrder!=activityList.length+1"
              v-for="item in dict.type.dms_repair_type"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>

        </el-form-item>
      </el-col>
      <el-col :span="8">
        <el-form-item label="外协单位" prop="outsrcId">
          <el-select   v-model="activity.outsrcId" placeholder=""  :disabled=true>
            <el-option
              v-bind:disabled="activity.processStepOrder!=activityList.length+1"
              v-for="item in dmsInfoList"
              :key="item.outsrcId"
              :label="item.outsrcName"
              :value="item.outsrcId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">

        <el-form-item label="处理意见"
        >
          <el-input
            v-model="activity.processHandleResolution"
            v-bind:disabled="activity.processStepOrder!=activityList.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.activityList.length].processActivityName}}</h4>
        <el-col :span="8" >
          <el-form-item label="故障类型" prop="faultType">
            <!--            <el-input v-model="form.faultType" placeholder="" :disabled="true"/>-->
            <el-select v-model="form1.faultType" placeholder="故障类型">
              <el-option
                v-for="item in dict.type.dms_fault_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="故障描述" prop="faultDescription">
            <el-input v-model="form1.faultDescription"  placeholder="故障描述" />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="涉及操作" prop="designOperations">
            <el-input v-model="form1.designOperations"   />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="设备名称" prop="deviceId">
            <el-select   v-model="form1.deviceId" placeholder="设备名称" >
              <el-option
                v-for="item in ledgerList"
                :key="item.deviceId"
                :label="item.deviceName"
                :value="item.deviceId"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="维修类型" prop="repairType">
            <el-select   v-model="form1.repairType" placeholder="" >
              <el-option
                v-for="item in dict.type.dms_repair_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <!--            <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="outsrcId">
            <el-select   v-model="form1.outsrcId" placeholder="外协单位">
              <el-option
                v-for="item in dmsInfoList"
                :key="item.outsrcId"
                :label="item.outsrcName"
                :value="item.outsrcId"
              >
              </el-option>
            </el-select>
          </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 { listActivity, getActivity, delActivity, addActivity, updateActivity,selectUserIdByInspectInstanceId  } from "@/api/dms/activity";
import { listDmsBillsFaultInstance } from '@/api/dms/dmsBillsFaultInstance'
import { listWfprocessactivity } from '@/api/system/common/wfprocessactivity'
import { listLedger } from '@/api/dms/ledger'
import { listDmsInfo } from '@/api/dms/dmsInfo'

export default {
  name: "Activity",
  dicts:['dms_fault_type','dms_repair_type','dms_bills_status','dms_inspect_type','dms_fault_source_type'],
  data() {
    return {
      //报修来源类型
      faultSourceTypeCheck:"",
      //外协
      dmsInfoList:[],
      //设备
      ledgerList:[],
      //流程信息
      wfProcessActivityList:[],
      //报修工单
      dmsBillsFaultInstanceList:[],
      stepsNum:"",
      ListLength:"",
      isUpdate:false,
      nowPorcessStepOrder:"",
      repairInstanceIdCheck:"",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 故障报修工单实例节点表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairInstanceId: null,
        processActivityId: null,
        faultType: null,
        faultDescription: null,
        designOperations: null,
        deviceId: null,
        repairType: null,
        outsrcId: 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: {
        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 repairInstanceId = this.$route.params && this.$route.params.repairInstanceId;
    this.repairInstanceIdCheck = repairInstanceId;
    this.queryParams.repairInstanceId = repairInstanceId;
    this.queryParamWfProcessActivity.wfProcessId = 102;
    console.log(this.repairInstanceIdCheck)
    this.isUpdate = true;
    this.getList();
    this.getWfprocessActivity();
    this.getBillsFaultInstance();
    this.getDeviveLedger();
    this.getoutsrcId();
  },
  methods: {
    getSelectUserId(){
      if (this.nowPorcessStepOrder==this.wfProcessActivityList.length){
        selectUserIdByInspectInstanceId(this.repairInstanceIdCheck,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/planRepair/dmsBillsFaultInstance"})
          }
        });
      }

    },
    getoutsrcId() {
      listDmsInfo().then(response => {
        this.dmsInfoList = response.rows;
      });
    },
    /** 查询设备台账信息列表 */
    getDeviveLedger() {
      listLedger().then(response => {
        this.ledgerList = response.rows;
      });
    },
    /**查询流程信息*/
    getWfprocessActivity(){
      this.loading = true;
      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.activityList[i].processStepOrder==this.wfProcessActivityList[j].processActivityOrder){
              this.activityList[i].processActivityName = this.wfProcessActivityList[j].processActivityName;

            }
          }
        }
        this.loading = false;
      })
    },
    /** 查询报修工单列表 */
    getBillsFaultInstance() {
      this.loading = true;
      listDmsBillsFaultInstance(this.queryParams).then(response => {
        this.dmsBillsFaultInstanceList = response.rows;
        console.log(this.dmsBillsFaultInstanceList)
        //将0/1转化为字典label
        for (let i = 0; i < this.dict.type.dms_fault_source_type.length;i++){
          if (this.dmsBillsFaultInstanceList[0].faultSourceType==i){
            this.faultSourceTypeCheck = this.dict.type.dms_fault_source_type[i].label;
          }
        }
        for (let i = 0; i < this.dict.type.dms_bills_status.length;i++){
          if (this.dmsBillsInstanceList[0].inspectStatus==i){
            this.billsStatusCheck = this.dict.type.dms_bills_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;
          }
        }
        this.loading = false;
      });
    },
    /** 查询故障报修工单实例节点列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        console.log(this.activityList)
        this.form.repairInstanceId = this.activityList[0].repairInstanceId;
        this.form.faultType = this.activityList[0].faultType;
        this.form.faultDescription = this.activityList[0].faultDescription;
        this.form.designOperations = this.activityList[0].designOperations;
        this.form.deviceId = this.activityList[0].deviceId;
        this.form.repairType = this.activityList[0].repairType;
        this.form.outsrcId = this.activityList[0].outsrcId;
        this.ListLength = this.activityList.length;
        this.form1 = JSON.parse(JSON.stringify(this.activityList[this.ListLength-1]))
        this.form1.processHandleResolution = null;
        this.nowPorcessStepOrder = this.ListLength+1;
        console.log(this.form.repairType)
        this.getSelectUserId();
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        instanceActivityId: null,
        repairInstanceId: null,
        processActivityId: null,
        faultType: null,
        faultDescription: null,
        designOperations: null,
        deviceId: null,
        repairType: null,
        outsrcId: 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
      getActivity(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) {
            updateActivity(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;
            addActivity(this.form1).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getWfprocessActivity();
              this.getBillsFaultInstance();
              this.getDeviveLedger();
              this.getoutsrcId();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const instanceActivityIds = row.instanceActivityId || this.ids;
      this.$modal.confirm('是否确认删除故障报修工单实例节点编号为"' + instanceActivityIds + '"的数据项？').then(function() {
        return delActivity(instanceActivityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('dms/activity/export', {
        ...this.queryParams
      }, `activity_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
