<template>
  <view class="handler">
    <view class="head">
      <view class="tab" :class="{active: tab.value === dataInfo.tabActiveValue}" v-for="tab in dataInfo.tabs" :key="tab.id" @click="clickTab(tab.value)">
        {{tab.name}}<text v-if="tab.num !== ''">({{tab.num}})</text>
      </view>
    </view>
    <scroll-view scroll-y class="body page_padding">
      <!-- 故障处理 -->
      <template v-if="dataInfo.tabActiveValue === 'direct'">
        <view class="form_item column" v-if="dataInfo.category.hasSimple != 1">
          <view class="title"><text class="required newicon newicon-bitian"></text>解决方案:</view>
          <uni-easyinput class="value" type="textarea" v-model="dataInfo.handleDescription" placeholder="请输入解决方案" :class="{formRed: isSubmit && !dataInfo.handleDescription.trim()}" />
        </view>
        <view class="form_item" v-if="dataInfo.category.hasSimple != 1">
          <view class="title"><text class="required newicon newicon-bitian"></text>故障现象:</view>
          <view class="value category" @click="selectCategory">
            <text class="categoryName ellipsis-multiline">{{dataInfo.category.mutiCategory}}</text>
            <text class="newicon newicon-weibiaoti2010104"></text>
          </view>
        </view>
        <view class="form_item">
          <view class="title select"><text class="required newicon newicon-bitian"></text>处理方式:</view>
          <uni-data-select class="value" v-model="dataInfo.handleCategory" :localdata="dataInfo.handleCategoryList" :clear="false" placeholder="请选择处理方式" :class="{formRed: isSubmit && !dataInfo.handleCategory}"></uni-data-select>
        </view>
        <view class="form_item" v-if="dataInfo.category.hasSimple != 1">
          <view class="title select"><text class="required newicon newicon-bitian"></text>处理结果:</view>
          <uni-data-select class="value" v-model="dataInfo.closecode" :localdata="dataInfo.closecodeList" :clear="false" placeholder="请选择处理结果" :class="{formRed: isSubmit && !dataInfo.closecode}"></uni-data-select>
        </view>
        <view class="form_item">
          <view class="title"><text class="required newicon newicon-bitian transparent"></text>协同人员:</view>
          <text class="synergeticNames ellipsis">{{dataInfo.synergetic.map(v => v.name).join(',')}}</text>
          <button type="primary" plain size="mini" class="primaryPlainButton synergeticAdd" @click="synergeticAdd">+立即添加</button>
        </view>
        <view class="form_item_column">
          <view class="form_item">
            <view class="title"><text class="required newicon newicon-bitian transparent"></text>处理图片:</view>
            <view class="value">
              <uni-file-picker ref="handlerImgRef" v-model="dataInfo.handlerImgList" limit="3" @success="handlerImgSuccess" @fail="handlerImgFail" @select="handlerImgSelect" @delete="handlerImgDelete"></uni-file-picker>
            </view>
          </view>
          <view class="form_item">
            <view class="title transparent"><text class="required newicon newicon-bitian transparent"></text>处理图片:</view>
            <view class="value">
              <text class="imgTips ellipsis">(支持JPG/PNG格式图片,单张大小10M以内)</text>
            </view>
          </view>
        </view>
      </template>
      
      <!-- 派单 -->
      <template v-if="dataInfo.tabActiveValue === 'assign'">
        <view class="form_item">
          <view class="title select"><text class="required newicon newicon-bitian"></text>工作组:</view>
          <uni-data-select class="value" v-model="dataInfo.groupId" :localdata="dataInfo.groupList" :clear="false" placeholder="请选择工作组" @change="changeGroup" :class="{formRed: isSubmit && !dataInfo.groupId}"></uni-data-select>
        </view>
        <view class="form_item">
          <view class="title radio"><text class="required newicon newicon-bitian"></text>是否转派到人:</view>
          <uni-data-checkbox class="value" v-model="dataInfo.isAssignUser" :localdata="dataInfo.isAssignUserList" @change="changeIsAssignUser" />
        </view>
        <view class="form_item" v-if="dataInfo.isAssignUser === 1">
          <view class="title select"><text class="required newicon newicon-bitian"></text>转派对象:</view>
          <uni-data-select class="value" v-model="dataInfo.userId" :localdata="dataInfo.userList" :clear="false" placeholder="请选择转派对象" :class="{formRed: isSubmit && dataInfo.groupId && dataInfo.isAssignUser == 1 && !dataInfo.userId}"></uni-data-select>
        </view>
        <!-- <view class="form_item_column">
          <view class="form_item">
            <view class="title"><text class="required newicon newicon-bitian transparent"></text>处理图片:</view>
            <view class="value">
              <uni-file-picker ref="handlerImgRef" v-model="dataInfo.handlerImgList" limit="3" @success="handlerImgSuccess" @fail="handlerImgFail" @select="handlerImgSelect" @delete="handlerImgDelete"></uni-file-picker>
            </view>
          </view>
          <view class="form_item">
            <view class="title transparent"><text class="required newicon newicon-bitian transparent"></text>处理图片:</view>
            <view class="value">
              <text class="imgTips ellipsis">(支持JPG/PNG格式图片,单张大小10M以内)</text>
            </view>
          </view>
        </view> -->
      </template>
    </scroll-view>
    <view class="foot_common_btns">
      <button @click="goBack" type="default" class="cancelButton btn">返回</button>
      <button @click="submit" type="default" class="primaryButton btn">提交</button>
    </view>
  </view>
</template>

<script setup>
  import { ref, reactive, computed } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import { generateNumberArray } from '@/utils/index.js'
  import { api_group, api_incidentDetail, api_user, api_incidentTask, api_branch, api_dutyDepartment, api_getDictionary, api_querySummaryDoc, api_addSummaryDoc, api_systemConfiguration, api_sj } from "@/http/api.js"
  import { defaultColor } from '@/static/js/theme.js'
  import { useSetTitle } from '@/share/useSetTitle.js'
  import { useMakePhoneCall } from '@/share/useMakePhoneCall.js'
  import { useUploadFile } from '@/share/useUploadFile.js'
  import { useGoBack } from '@/share/useGoBack.js'
  import { useLoginUserStore } from '@/stores/loginUser'
  import { useHandlerStore } from '@/stores/handler'
  import { useIncidentBuildStore } from '@/stores/incidentBuild'
  
  useSetTitle();
  const loginUserStore = useLoginUserStore();
  const handlerStore = useHandlerStore();
  const incidentBuildStore = useIncidentBuildStore();
  const { makePhoneCall }  = useMakePhoneCall();
  const { uploadFile }  = useUploadFile();
  const { goBack }  = useGoBack();
  
  // 主题颜色
  const primaryColor = ref(defaultColor)
  
  // 数据
  const dataInfo = reactive({
    tabs: [
      {id: 5, name: '直接处理', value: 'direct', num: ''},
      {id: 6, name: '派单', value: 'assign', num: ''},
    ],
    tabActiveValue: 0,//当前选择的tab
    incidentId: undefined,//事件ID
    incidentData: {},//事件对象
    groupList: [], //工作组列表
    groupId: undefined, //工作组ID
    userIdList: [], //延期天数列表
    userIdId: undefined, //延期天数ID
    deferralRemark: '',//延期说明
    summaryObj: {
      consumableList: [],//耗材列表
      workHourManagementList: [],//工时列表
    },//汇总单信息
    summaryId: undefined,//汇总单Id
    isNumber: false,//修改数量弹窗
    evtNumber: 1,//弹窗返回的数量
    selectData: {},//选择的对象
    selectType: {},//选择的对象类型
    handleDescription: '',//解决方案
    handleCategory: undefined,//处理方式
    handleCategoryList: [],//处理方式列表
    closecode: undefined,//处理结果
    closecodeList: [],//处理结果列表
    handlerImgList: [],//处理图片列表
    category: {},//故障现象
    synergetic: [],//协同人员
    isAssignUser: 1, //工作组是否转派到人
    isAssignUserList: [
      { text: '是', value: 1 },
      { text: '否', value: 0 },
    ], //工作组是否转派到人选项
  })
  
  // 是否提交
  const isSubmit = ref(false)
  
  // 处理图片
  const handlerImgRef = ref(null)
  
  // 上传报修图片
  function handlerImgSuccessNew(){
    let requestList = [];
    
    // 报修图片
    dataInfo.incidentData.repairImgList.forEach(v => {
      let repairOrderImg$ = repairOrderImg(v);
      requestList.push(repairOrderImg$);
    })
    
    Promise.all(requestList).then(resList => {
      uni.hideLoading();
      console.log(resList);
      incidentBuildStore.clearIncidentBuildData();
      uni.showToast({
      	icon: 'none',
        title: '建单成功',
        mask: true,
      });
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/incidentList/incidentList',
        })
      }, 1500)
    })
  }
  
  // 上传处理图片成功
  function handlerImgSuccess(e){
    dataInfo.handlerImgList.forEach(v => {
      v.url = v.path;
    })
    console.log(dataInfo.handlerImgList);
    let requestList = [];
    
    // 处理图片
    dataInfo.handlerImgList.forEach(v => {
      let handlerOrderImg$ = handlerOrderImg(v);
      requestList.push(handlerOrderImg$);
    })
    
    // 报修图片
    dataInfo.incidentData.repairImgList.forEach(v => {
      let repairOrderImg$ = repairOrderImg(v);
      requestList.push(repairOrderImg$);
    })
    
    Promise.all(requestList).then(resList => {
      uni.hideLoading();
      console.log(resList);
      incidentBuildStore.clearIncidentBuildData();
      uni.showToast({
      	icon: 'none',
        title: '建单成功',
        mask: true,
      });
      setTimeout(() => {
        uni.reLaunch({
          url: '/pages/incidentList/incidentList',
        })
      }, 1500)
    })
  }
  
  // 上传处理图片失败
  function handlerImgFail(e){
    dataInfo.handlerImgList.forEach(v => {
      v.url = v.path;
    })
    console.log(dataInfo.handlerImgList);
  }
  
  // 选择上传图片
  function handlerImgSelect(e){
    dataInfo.handlerImgList = dataInfo.handlerImgList.concat(e.tempFiles);
    console.log(dataInfo.handlerImgList);
  }
  
  // 删除上传图片
  function handlerImgDelete(e){
    dataInfo.handlerImgList = dataInfo.handlerImgList.filter(v => e.tempFile.uuid != v.uuid);
    console.log(dataInfo.handlerImgList);
  }
  
  // 添加协同人员
  function synergeticAdd(){
    handlerStore.setHandlerData(dataInfo, 'assign');
    uni.navigateTo({
      url: `/pages/synergeticAdd/synergeticAdd?incidentId=${dataInfo.incidentId}`
    })
  }
  
  // 选择故障现象
  function selectCategory(){
    handlerStore.setHandlerData(dataInfo, 'assign');
    uni.navigateTo({
      url: `/pages/categoryOne/categoryOne?incidentId=${dataInfo.incidentId}`
    })
  }
  
  // 点击修改数量
  function numberClick(data, type){
    if(type === 'editConsumable'){
      dataInfo.evtNumber = data.consumablesNum;
    }else if(type === 'editWorkHourManagement'){
      dataInfo.evtNumber = data.workHourNum2;
    }
    
    dataInfo.isNumber = true;
    dataInfo.selectData = data;
    dataInfo.selectType = type;
  }
  
  // 确认修改数量
  function conformNumber(evtNumber){
    dataInfo.evtNumber = evtNumber;
    dataInfo.isNumber = false;
    addSummaryDoc();
  }
  
  // 移除数量
  function removeNumber(evtNumber){
    dataInfo.evtNumber = evtNumber;
    dataInfo.isNumber = false;
    removeSummaryDoc();
  }
  
  // 关闭修改数量
  function cancelNumber(){
    dataInfo.isNumber = false;
  }
  
  // 修改耗材/工时
  function addSummaryDoc(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      "summaryId": dataInfo.summaryId,
      "modifyNum": 'edit',
    };
    if(dataInfo.selectType === 'editConsumable'){
      postData.consumableList = [
        {
          "consumablesId": dataInfo.selectData.consumableId,
          "consumablesNum": dataInfo.evtNumber,
        }
      ];
    }else if(dataInfo.selectType === 'editWorkHourManagement'){
      postData.workHourManagementList = [
        {
          "workHourId": dataInfo.selectData.id,
          "workHourNum": dataInfo.evtNumber,
        }
      ];
    }
    api_addSummaryDoc(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        uni.showToast({
        	icon: 'none',
          title: '修改数量成功',
          mask: true,
        });
        getSummaryList();
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败!'
        });
      }
    })
  }
  
  // 移除耗材/工时
  function removeSummaryDoc(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      "summaryId": dataInfo.summaryId,
      "remove": 'remove',
    };
    if(dataInfo.selectType === 'editConsumable'){
      postData.consumableList = [
        {
          "consumablesId": dataInfo.selectData.consumableId,
          "consumablesNum": dataInfo.evtNumber,
        }
      ];
    }else if(dataInfo.selectType === 'editWorkHourManagement'){
      postData.workHourManagementList = [
        {
          "workHourId": dataInfo.selectData.id,
          "workHourNum": dataInfo.evtNumber,
        }
      ];
    }
    api_addSummaryDoc(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        uni.showToast({
        	icon: 'none',
          title: '移除成功',
          mask: true,
        });
        getSummaryList();
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败!'
        });
      }
    })
  }
  
  // 重置
  function reset(){
    dataInfo.repairTypeList = []; //延期原因列表
    dataInfo.groupId = undefined; //延期原因ID
    dataInfo.userIdList = []; //延期天数列表
    dataInfo.userIdId = undefined; //延期天数ID
    dataInfo.deferralRemark = '';//延期说明
    dataInfo.summaryObj = {
      consumableList: [],//耗材列表
      workHourManagementList: [],//工时列表
    };//汇总单信息
    dataInfo.summaryId = undefined;//汇总单Id
    dataInfo.isNumber = false;//修改数量弹窗
    dataInfo.evtNumber = 1;//弹窗返回的数量
    dataInfo.selectData = {};//选择的对象
    dataInfo.selectType = {};//选择的对象类型
    dataInfo.handleDescription = '';//解决方案
    dataInfo.handleCategory = undefined;//处理方式
    dataInfo.handleCategoryList = [];//处理方式列表
    dataInfo.closecode = undefined;//处理结果
    dataInfo.closecodeList = [];//处理结果列表
    dataInfo.handlerImgList = [];//处理图片列表
    dataInfo.category = {};//故障现象
    dataInfo.synergetic = [];//协同人员
  }
  
  // 初始化表单
  function initForm(){
    if(dataInfo.tabActiveValue === 'direct'){
      getHandleCategorys();
      getClosecodes();
    }else if(dataInfo.tabActiveValue === 'assign'){
      getGroups();
    }
  }
  
  // 点击tab
  function clickTab(tabValue){
    if(dataInfo.tabActiveValue == tabValue){
      return;
    }
    dataInfo.tabActiveValue = tabValue;
    isSubmit.value = false;
    reset();
    dataInfo.category = dataInfo.incidentData.category || {};
    dataInfo.synergetic = dataInfo.incidentData.synergetic || [];
    initForm();
  }
  
  // 选择工作组
  function changeGroup(){
    dataInfo.userId = undefined;
    dataInfo.userList = [];
    getUsers();
  }
  
  // 选择是否转派到人
  function changeIsAssignUser(){
    dataInfo.userId = undefined;
  }
  
  // 获取工作组列表
  function getGroups(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
    let postData = {
      "idx": 0,
      "sum": 9999,
      "group": {
        "duty": dataInfo.incidentData.duty || undefined,
        "selectType": "nouser"
      }
    };
    api_group(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        res.list = res.list || [];
        dataInfo.groupList = res.list.map(v => ({
          text: v.groupName,
          value: v.id,
        }));
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败!'
        });
      }
    })
  }
  
  // 获取用户列表
  function getUsers(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      "idx": 0,
      "sum": 9999,
      "user": {
        "duty": dataInfo.incidentData.duty || undefined,
        "groupdata":{
          "id": dataInfo.groupId,
        },
        "roledata": {
            "rolecode": "first-line support"
        },
        "selectType": "1",
        "selectDetails": 1,
        "simple": true,
        "engineer": 1,
      }
    };
    api_user(postData).then(res => {
      uni.hideLoading();
      if(res.status == 200){
        res.list = res.list || [];
        dataInfo.userList = res.list.map(v => ({
          text: v.name,
          value: v.id,
        }));
      }else{
        uni.showToast({
          icon: 'none',
          title: res.msg || '请求数据失败!'
        });
      }
    })
  }
  
  // 获取处理方式列表
  function getHandleCategorys(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      "key": 'incident_handleCategory',
      "type": "list",
    };
    api_getDictionary(postData).then(res => {
      uni.hideLoading();
      res = res || [];
      dataInfo.handleCategoryList = res.map(v => ({
        text: v.name,
        value: v.id,
        key: v.value,
      }));
      if(!dataInfo.handleCategory){
        let handleCategory = dataInfo.handleCategoryList.find(v => v.key == 'SUPPORT');
        dataInfo.handleCategory = handleCategory ? handleCategory.value : undefined;
      }
    })
  }
  
  // 获取处理结果列表
  function getClosecodes(){
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    let postData = {
      "key": 'incident_closecode',
      "type": "list",
    };
    api_getDictionary(postData).then(res => {
      uni.hideLoading();
      res = res || [];
      dataInfo.closecodeList = res.map(v => ({
        text: v.name,
        value: v.id,
        key: v.value,
      }));
      if(!dataInfo.closecode){
        let closecode = dataInfo.closecodeList.find(v => v.key == '060');
        dataInfo.closecode = closecode ? closecode.value : undefined;
      }
    })
  }
  
  // 添加耗材
  function addConsumable(){
    uni.navigateTo({
      url: `/pages/consumableList/consumableList?incidentId=${dataInfo.incidentId}&summaryId=${dataInfo.summaryId}`
    })
  }
  
  // 添加工时
  function addWorkHourManagement(){
    uni.navigateTo({
      url: `/pages/workHourManagementOne/workHourManagementOne?incidentId=${dataInfo.incidentId}&summaryId=${dataInfo.summaryId}`
    })
  }
  
  // 提交
  function submit(){
    isSubmit.value = true;
    if(dataInfo.tabActiveValue === 'direct'){
      submitHandler();
    }else if(dataInfo.tabActiveValue === 'assign'){
      submitAssign();
    }
  }
  
  // 处理提交事件
  async function handlerOrder(){
    let postData = {
      incident: dataInfo.incidentData,
    }
    
    let result = await beforeBuild();
    if(result[0].status == 200 && result[1].status == 200){
      postData.incident.repairType = result[0].list.length ? result[0].list[0].valueconfig : undefined;
      postData.incident.incidentsign = result[1].data || undefined;
    }else{
      uni.showToast({
        icon: 'none',
        title: '请求数据失败!'
      });
      return;
    }
    
    postData.incident.handleDescription = dataInfo.handleDescription;
    postData.incident.handleCategory = {id: dataInfo.handleCategory};
    postData.incident.closecode = {id: dataInfo.closecode};
    postData.incident.category = dataInfo.category;
    postData.incident.synergetic = dataInfo.synergetic;
    postData.incident.directProcess = 1;//直接解决
    
    return api_incidentTask('accept', postData);
  }
  
  // 建单前的接口
  function beforeBuild(){
    let repairMain$ = api_systemConfiguration({
        "idx": 0,
        "sum": 1,
        "systemConfiguration": {
          "keyconfig": "repairMain"
        }
    })
    
    let sj$ = api_sj();
    return Promise.all([repairMain$, sj$])
  }
  
  // 派单提交事件
  async function assignOrder(){
    let postData = {
      incident: dataInfo.incidentData,
    }
    
    let result = await beforeBuild();
    if(result[0].status == 200 && result[1].status == 200){
      postData.incident.repairType = result[0].list.length ? result[0].list[0].valueconfig : undefined;
      postData.incident.incidentsign = result[1].data || undefined;
    }else{
      uni.showToast({
        icon: 'none',
        title: '请求数据失败!'
      });
      return;
    }
    
    if(dataInfo.userId){
      // 派人
      postData.incident.assignee = dataInfo.userId;
    } else {
      // 派组
      postData.incident.candidateGroups = dataInfo.groupId;
    }
    
    return api_incidentTask('accept', postData);
  }
  
  // 处理图片
  function handlerOrderImg(imgObj){
    return uploadFile(imgObj, 'incident', dataInfo.incidentId)
  }
  
  // 报修图片
  function repairOrderImg(imgObj){
    return uploadFile(imgObj, 'wechatRequesterIncident', dataInfo.incidentId)
  }
  
  // 处理提交
  function submitHandler(){
    console.log(dataInfo);
    if(!dataInfo.handleDescription.trim() && dataInfo.category.hasSimple != 1){
      uni.showToast({
      	icon: 'none',
        title: '请填写解决方案'
      });
      return;
    }
    
    if(!dataInfo.category.id && dataInfo.category.hasSimple != 1){
      uni.showToast({
      	icon: 'none',
        title: '请选择故障现象'
      });
      return;
    }
    
    if(!dataInfo.handleCategory){
      uni.showToast({
      	icon: 'none',
        title: '请选择处理方式'
      });
      return;
    }
    
    if(!dataInfo.closecode && dataInfo.category.hasSimple != 1){
      uni.showToast({
      	icon: 'none',
        title: '请选择处理结果'
      });
      return;
    }
    
    if(!dataInfo.incidentData.duty){
      uni.showToast({
      	icon: 'none',
        title: '您选择的故障现象没有设置责任科室'
      });
      return;
    }
    console.log(dataInfo)
    console.log(dataInfo.handlerImgList)
    console.log(dataInfo.incidentData.repairImgList)
    
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
    if(dataInfo.handlerImgList.length){
      // 有处理图片
      let handlerOrder$ = handlerOrder();
      let requestList = [handlerOrder$];
      Promise.all(requestList).then(resList => {
        console.log(resList);
        if(resList[0].state == 200){
          dataInfo.incidentId = resList[0].data.id;
          handlerImgRef.value.upload();
        }else{
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: resList[0].msg || '请求数据失败!'
          });
        }
      })
    }else if(dataInfo.incidentData.repairImgList.length){
      // 有报修图片,无处理图片
      let handlerOrder$ = handlerOrder();
      let requestList = [handlerOrder$];
      Promise.all(requestList).then(resList => {
        console.log(resList);
        if(resList[0].state == 200){
          dataInfo.incidentId = resList[0].data.id;
          handlerImgSuccessNew();
        }else{
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: resList[0].msg || '请求数据失败!'
          });
        }
      })
    }else{
      // 没有图片
      let handlerOrder$ = handlerOrder();
      let requestList = [handlerOrder$];
      Promise.all(requestList).then(resList => {
        uni.hideLoading();
        console.log(resList);
        if(resList[0].state == 200){
          incidentBuildStore.clearIncidentBuildData();
          uni.showToast({
          	icon: 'none',
            title: '建单成功',
            mask: true,
          });
          setTimeout(() => {
            uni.reLaunch({
              url: '/pages/incidentList/incidentList',
            })
          }, 1500)
        }else{
          uni.showToast({
            icon: 'none',
            title: resList[0].msg || '请求数据失败!'
          });
        }
      })
    }
  }
  
  // 派单提交
  function submitAssign(){
    if(!dataInfo.groupId){
      uni.showToast({
      	icon: 'none',
        title: '请选择工作组'
      });
      return;
    }
    
    if(dataInfo.isAssignUser == 1 && !dataInfo.userId){
      uni.showToast({
      	icon: 'none',
        title: '请选择指派对象'
      });
      return;
    }
    
    if(!dataInfo.incidentData.duty){
      uni.showToast({
      	icon: 'none',
        title: '您选择的故障现象没有设置责任科室'
      });
      return;
    }
    
    uni.showLoading({
      title: "加载中",
      mask: true,
    });
    
    if(dataInfo.incidentData.repairImgList.length){
      // 有图片
      let assignOrder$ = assignOrder();
      let requestList = [assignOrder$];
      Promise.all(requestList).then(resList => {
        console.log(resList);
        if(resList[0].state == 200){
          dataInfo.incidentId = resList[0].data.id;
          // handlerImgRef.value.upload();
          handlerImgSuccessNew();
        }else{
          uni.hideLoading();
          uni.showToast({
            icon: 'none',
            title: resList[0].msg || '请求数据失败!'
          });
        }
      })
    }else{
      // 没有图片
      let assignOrder$ = assignOrder();
      let requestList = [assignOrder$];
      Promise.all(requestList).then(resList => {
        uni.hideLoading();
        console.log(resList);
        if(resList[0].state == 200){
          incidentBuildStore.clearIncidentBuildData();
          uni.showToast({
          	icon: 'none',
            title: '建单成功',
            mask: true,
          });
          setTimeout(() => {
            uni.reLaunch({
              url: '/pages/incidentList/incidentList',
            })
          }, 1500)
        }else{
          uni.showToast({
            icon: 'none',
            title: resList[0].msg || '请求数据失败!'
          });
        }
      })
    }
   
  }
  
  onLoad((option) => {
    // 跳转页面选择了选项
    if(handlerStore.handler.data){
      Object.assign(dataInfo, handlerStore.handler.data);
      console.log(dataInfo)
      handlerStore.clearHandlerData();
      
      if(dataInfo.category){
         dataInfo.incidentData.category = dataInfo.category;
        
        // 处理责任科室
        if(dataInfo.category.branchType == 1){
          // 分院区
          if(dataInfo.incidentData.branch){
            let dutyConfig = dataInfo.category.branchUserGroups.find(v=>v.branchId == dataInfo.incidentData.branch);
            if(dutyConfig && dutyConfig.dutyId && dutyConfig.dutyName){
              dataInfo.incidentData.duty = {id:dutyConfig.dutyId,dept:dutyConfig.dutyName};
            }else{
              dataInfo.incidentData.duty = undefined;
            }
          }else{
            dataInfo.incidentData.duty = undefined;
          }
        }else{
          // 不分院区
          dataInfo.incidentData.duty = dataInfo.category.dutyDepartment;
        }
      }
    }else if(incidentBuildStore.incidentBuild.data){
      // 初始化
      Object.assign(dataInfo.incidentData, incidentBuildStore.incidentBuild.data);
      if(dataInfo.incidentData.category){
        dataInfo.category = dataInfo.incidentData.category;
        
        // 处理责任科室
        if(dataInfo.category.branchType == 1){
          // 分院区
          if(dataInfo.incidentData.branch){
            let dutyConfig = dataInfo.category.branchUserGroups.find(v=>v.branchId == dataInfo.incidentData.branch);
            if(dutyConfig && dutyConfig.dutyId && dutyConfig.dutyName){
              dataInfo.incidentData.duty = {id:dutyConfig.dutyId,dept:dutyConfig.dutyName};
            }else{
              dataInfo.incidentData.duty = undefined;
            }
          }else{
            dataInfo.incidentData.duty = undefined;
          }
        }else{
          // 不分院区
          dataInfo.incidentData.duty = dataInfo.category.dutyDepartment;
        }
      }
      if(dataInfo.incidentData.priority){
        dataInfo.incidentData.priority = { id: dataInfo.incidentData.priority };
      }
      if(dataInfo.incidentData.source){
        dataInfo.incidentData.source = { id: dataInfo.incidentData.source };
      }
      if(dataInfo.incidentData.requester){
        dataInfo.incidentData.requester = dataInfo.incidentData.requester
      }
    }
    
    dataInfo.tabActiveValue = dataInfo.tabs[0].value;
    initForm()
  })
</script>

<style lang="scss" scoped>
.handler{
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  .head{
    height: 88rpx;
    display: flex;
    position: fixed;
    z-index: 99;
    width: 100%;
    background-color: #fff;
    font-size: 30rpx;
    .tab{
      flex: 1;
      display: flex;
      justify-content: center;
      align-items: center;
      border-bottom: 4rpx solid transparent;
      &.active{
        color: $uni-primary;
        border-color: $uni-primary;
      }
    }
  }
  .body{
    margin-top: 88rpx;
    box-sizing: border-box;
    flex: 1;
    min-height: 0;
    &.bg{
      background-color: #F7F7F7;
    }
    .summaryItem{
      &:first-of-type{
        .summaryItem_head{
          border-bottom: 1rpx solid #DDDDDD;
        }
      }
      .summary_total{
        padding: 20rpx 0;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      .summaryItem_head{
        padding: 24rpx;
        font-size: 26rpx;
        color: #3A3A3A;
      }
      .summaryItem_body{
        font-size: 30rpx;
        background-color: #fff;
        .summaryItem_bodyItem{
          padding: 24rpx;
          border-bottom: 1rpx solid #DDDDDD;
          .summaryItem_bodyItem_top{
            display: flex;
            justify-content: space-between;
            align-items: center;
            .value{
              padding-left: 48rpx;
              flex-shrink: 0;
            }
          }
          .summaryItem_bodyItem_bottom{
            margin-top: 24rpx;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .name{
              text-align: right;
              flex: 1;
            }
            .value{
              width: 240rpx;
              text-align: right;
              padding-left: 48rpx;
              flex-shrink: 0;
            }
          }
        }
      }
      .summaryItem_foot{
        font-size: 30rpx;
        background-color: #fff;
        &.total{
          margin-top: 24rpx;
        }
        .summaryItem_foot_total{
          padding: 24rpx 0;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .summaryItem_foot_add{
          border-top: 1rpx solid #DDDDDD;
          padding: 24rpx 0;
          display: flex;
          justify-content: center;
          align-items: center;
          .newicon-icon-test{
            font-size: 30rpx;
            font-weight: bold;
          }
        }
      }
    }
    .form_item_column{
      padding-top: 24rpx;
      min-height: 86rpx;
      .form_item{
        padding-top: 0;
        min-height: auto;
      }
    }
    .form_item{
      display: flex;
      align-items: center;
      padding-top: 24rpx;
      min-height: 86rpx;
      &.column{
        height: auto;
        flex-direction: column;
        align-items: flex-start;
        .title{
          margin-right: 0;
        }
        .value{
          margin-top: 10rpx;
          padding-left: 20rpx;
          box-sizing: border-box;
        }
        .tips{
          padding: 24rpx;
          text-align: center;
          font-size: 22rpx;
          color: #909399;
          width: 100%;
          box-sizing: border-box;
        }
      }
      .title{
        font-size: 26rpx;
        display: flex;
        align-items: center;
        margin-right: 12rpx;
        flex-shrink: 0;
        &.select{
          width: calc(5em + 20rpx);
        }
      }
      .value{
        width: 100%;
        &.category{
          width: 100%;
          display: flex;
          justify-content: space-between;
          align-items: center;
          .categoryName{
            font-size: 26rpx;
            color: #555;
            flex: 1;
          }
          .newicon-weibiaoti2010104{
            color: $uni-primary;
            margin-left: 24rpx;
          }
        }
        .imgTips{
          color: #909399;
          font-size: 22rpx;
        }
      }
      .synergeticNames{
        font-size: 26rpx;
        margin-right: 24rpx;
      }
      .synergeticAdd{
        flex-shrink: 0;
      }
    }
  }
}
</style>