<!--
 * @Author: ysh
 * @Date: 2025-01-16 11:17:08
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-09-03 16:38:56
-->
<template>
  <view class="work-container">
    <!-- 轮播图 -->
    <!-- <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot> -->

    <!-- 宫格组件 -->
    <uni-section title="常用操作" type="line"></uni-section>
    <view class="grid-body">
      <uni-grid :column="4" :showBorder="false">
        <uni-grid-item>
          <view class="grid-item-box" @click="$refs.popup.open()">
            <u-image :fade="false" src="@/static/images/work/sendFlow.png" width="40px" height="40px"></u-image>
            <text class="text">发起流程</text>
          </view>
        </uni-grid-item>
        <uni-grid-item v-for="item in gridList" :key="item.url" v-if="item.hasPermi">
          <view class="grid-item-box" @click="toUrlFn(item.url)" v-if="item.name != '发起流程'">
            <u-image :fade="false" :src="item.icon" width="40px" height="40px"></u-image>
            <text class="text">{{ item.name }}</text>
          </view>
        </uni-grid-item>
      </uni-grid>
    </view>

    <uv-popup ref="popup" mode="bottom">
      <view class="bottom-popup">
        <view v-for="item, index in sendFlowList" :key="index">
          <u-button @click="sendFlow(item)">{{ item.name }}</u-button>
        </view>
      </view>
    </uv-popup>
  </view>
</template>

<script>
import { listDefinition } from "@/api/flowable/definition";
import { Snowflake } from '@/utils/snowFlake.js';
import { getNextFlowNodeByStart, todoList } from "@/api/flowable/todo";
import { definitionStart, flowXmlAndNode } from "@/api/flowable/definition";
import { checkPermi, checkRole } from "@/utils/permission";
export default {
  data() {
    return {
      current: 0,
      swiperDotIndex: 0,
      data: [{
        image: '/static/images/banner/banner01.jpg'
      },
      {
        image: '/static/images/banner/banner02.jpg'
      },
      {
        image: '/static/images/banner/banner03.jpg'
      }],
      gridList: [
        {
          name: '借款管理',
          icon: '/static/images/work/borrow.png',
          url: 'borrow',
          hasPermi: checkPermi(['oa:borrow:list'])
        },
        {
          name: '用车管理',
          icon: '/static/images/work/car.png',
          url: 'car',
          hasPermi: checkPermi(['oa:car:list'])
        },
        {
          name: '仪器设备',
          icon: '/static/images/work/device.png',
          url: 'instruments',
          hasPermi: checkPermi(['oa:device:list'])
        },
        {
          name: '设备作业记录',
          icon: '/static/images/work/workDay.png',
          url: 'deviceLog',
          hasPermi: checkPermi(['oa:deviceLog:list'])
        },
        {
          name: '工作填报',
          icon: '/static/images/work/declare.png',
          url: 'declare',
          hasPermi: checkPermi(['oa:declare:list'])
        },
        {
          name: '人事管理',
          icon: '/static/images/work/staff.png',
          url: 'staff',
          hasPermi: checkRole(['staff','leader'])
        }
      ],
      flowList: ['借款审批', '用车审批', '设备审批', '工作填报'],
      sendFlowList: [],
      // 查询参数
      queryProcessParams: {
        pageNum: 1,
        pageSize: 9999,
        parentDeploymentId: null,
        name: null,
        category: null,
        key: null,
        tenantId: null,
        deployTime: null,
        derivedFrom: null,
        derivedFromRoot: null,
        parentDeploymentId: null,
        engineVersion: null
      },
      definitionList: [],
      processTotal: 0,
      processLoading: true
    }
  },
  created() {
    this.getDefinitionList();
  },
  methods: {
    checkPermi,
    checkRole,
    clickBannerItem(item) {
      console.info(item)
    },
    changeSwiper(e) {
      this.current = e.detail.current
    },
    toUrlFn(type) {
      if (type == 'borrow') {
        uni.navigateTo({
          url: '/pages/oa/borrow/borrowList'
        })
      } else if (type == 'deviceLog') {
        uni.navigateTo({
          url: '/pages/oa/device/deviceLog'
        })
      } else if (type == 'car') {
        uni.navigateTo({
          url: '/pages/oa/car/carList'
        })
      } else if (type == 'instruments') {
        uni.navigateTo({
          url: '/pages/oa/device/instrumentsList'
        })
      } else if (type == 'declare') {
        uni.navigateTo({
          url: '/pages/oa/declare/declareList'
        })
      } else if (type == 'staff') {
        uni.navigateTo({
          url: '/pages/oa/staff/staffList'
        })
      }
      else {
        this.$modal.showToast('模块建设中~')
      }
    },
    sendFlow(row) {
      let formId = new Snowflake(1n, 1n, 0n).nextId().toString();
      getNextFlowNodeByStart({ deploymentId: row.deploymentId, variables: { formId: formId } }).then(res => {
        let data = res.data;
        const variables = {};
        const formData = {};
        formData.disabled = true;
        formData.formBtns = false;
        formData.formId = formId
        if (row.id) {
          variables.variables = formData;
          definitionStart(row.id, JSON.stringify(variables)).then(res => {
            this.$modal.msgSuccess(res.msg);
            let procInstanceId = res.data;
            todoList({
              pageNum: 1,
              pageSize: 99999999, processInsId: procInstanceId
            }).then(toDoRes => {
              let records = toDoRes.data.records;
              if (records.length == 1) {
                records = records[0]
              }
              const query = {
                procInsId: records.procInsId,
                executionId: records.executionId,
                deployId: records.deployId,
                taskId: records.taskId,
                taskName: records.taskName,
                startUserName: records.startUserName + '-' + records.startDeptName,
                formId: formData.formId,
                procDefName: records.procDefName
              }
              const encodedParams = encodeURIComponent(JSON.stringify(query));
              uni.navigateTo({
                url: `/pages/message/apply/detail?params=${encodedParams}`
              })
              this.$refs.popup.close();
            })
          })
        }
      })
    },
    getDefinitionList() {
      listDefinition(this.queryProcessParams).then(response => {
        this.definitionList = response.data.records;
        this.processTotal = response.data.total;
        this.processLoading = false;
        let list = []
        for (let i of this.definitionList) {
          if (this.flowList.includes(i.name)) {
			if (i.name != "借款审批")
				list.push(i);
			if (i.name == "借款审批" && i.version == 2)
				list.push(i);
          }
        }
        this.sendFlowList = list;
      });
    },
  }
}
</script>

<style lang="scss">
/* #ifndef APP-NVUE */
page {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  background-color: #fff;
  min-height: 100%;
  height: auto;
}

view {
  font-size: 14px;
  line-height: inherit;
}

/* #endif */

.text {
  text-align: center;
  font-size: 26rpx;
  margin-top: 10rpx;
}

.grid-item-box {
  flex: 1;
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 15px 0;
}

.uni-margin-wrap {
  width: 690rpx;
  width: 100%;
  ;
}

.swiper {
  height: 300rpx;
}

.swiper-box {
  height: 150px;
}

.swiper-item {
  /* #ifndef APP-NVUE */
  display: flex;
  /* #endif */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  color: #fff;
  height: 300rpx;
  line-height: 300rpx;
}

@media screen and (min-width: 500px) {
  .uni-swiper-dot-box {
    width: 400px;
    /* #ifndef APP-NVUE */
    margin: 0 auto;
    /* #endif */
    margin-top: 8px;
  }

  .image {
    width: 100%;
  }
}

.bottom-popup {
  max-height: 50vh;
  margin-bottom: 65px;
  padding: 10px;
  text-align: center;
}
</style>
