<template>
  <view>
    <uni-nav-bar
      fixed="true"
      statusBar="true"
      title="工作台"
      right-icon="scan"
      @clickRight="HandlScanCode"
    ></uni-nav-bar>
    <view>
      <CellTip
        title="协同办公"
        v-if="internalManagementList.length > 0"
      ></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in internalManagementList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="生产管理" v-if="productionList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="item in productionList"
            :key="item.link_url"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <label>{{ item.title }}</label>
            <label>{{ item.num }}</label>
          </view>
        </view>
      </view>
    </view>
    <!-- <view>
      <CellTip title="生产执行"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in executeList"
            @click="toNav(item.link_url)"
          >
            <span :class="item.class"></span>
            <label>{{ item.title }}</label>
            <label>{{ item.num }}</label>
          </view>
        </view>
      </view>
    </view> -->
    <view>
      <CellTip title="运维管理" v-if="operationsList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in operationsList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="售后服务管理" v-if="serviceList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in serviceList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="仓储管理" v-if="warehousingList.length > 0"></CellTip>

      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in warehousingList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <label>{{ item.title }}</label>
            <!-- <label>{{ item.num }}</label> -->
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="运输管理" v-if="dispatchList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in dispatchList"
            @click="toNav(item.url)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="营销管理" v-if="saleManageList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in saleManageList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="采购管理" v-if="purchaseManageList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in purchaseManageList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="生产管控" v-if="pcsList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in pcsList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="质量管理" v-if="qmsList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in qmsList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <view>
      <CellTip title="溯源管理" v-if="traceabilityList.length > 0"></CellTip>
      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in traceabilityList"
            @click="toNav(item.path)"
          >
            <span :class="'iconfont ' + item.icon"></span>
            <i class="badge" v-if="item.badge">{{ item.badge }}</i>
            <label>{{ item.name }}</label>
          </view>
        </view>
      </view>
    </view>
    <!--    <view>
      <CellTip title="生产类"></CellTip>

      <view class="nav">
        <view class="nav-content">
          <view
            class="nav-item"
            v-for="(item, index) in productionList"
            @click="toNav(item.link_url)"
          >
            <span :class="item.class"></span>
            <label>{{ item.title }}</label>
            <label>{{ item.num }}</label>
          </view>
        </view>
      </view>
    </view> -->
  </view>
</template>

<script>
import CellTip from "@/components/CellTip.vue";
import { statistics } from "@/api/myTicket";
export default {
  components: {
    CellTip,
  },
  data() {
    return {
      timer: null,
      src1: "https://cdn.uviewui.com/uview/album/1.jpg",
      workOrder: {}, // 工单统计数据
      qmsList: [],
      //生产类
      productionList: [],
      // executeList: [
      //   {
      //     class: "iconfont icon-gongdanguanli",
      //     title: "成型",
      //     link_url: "/pages/production/execute/extrusion/index",
      //     // "num": 1
      //   },
      //   {
      //     class: "iconfont icon-gongdanguanli",
      //     title: "自然干燥",
      //     link_url: "/pages/production/execute/drying/index",
      //     // "num": 1
      //   },
      //   {
      //     class: "iconfont icon-gongdanguanli",
      //     title: "升温干燥",
      //     link_url: "/pages/production/execute/heating/index",
      //     // "num": 1
      //   },
      //   {
      //     class: "iconfont icon-gongdanguanli",
      //     title: "半加定长",
      //     link_url: "/pages/production/execute/halfAdded/index",
      //     // "num": 1
      //   },
      //   // {
      //   //   class: "iconfont icon-gongdanguanli",
      //   //   title: "领料",
      //   //   link_url: "/pages/production/execute/picking/index",
      //   //   // "num": 1
      //   // },
      // ],
      //仓储管理

      warehousingList: [],

      //运维类
      operationsList: [],
      dispatchList: [],
      internalManagementList: [],
      treeList: [],
      saleManageList: [],
      // 售后服务
      serviceList: [],
      // 溯源管理
      traceabilityList: [],
      // 采购管理
      purchaseManageList: [],
	  pcsList:[]
    };
  },
  created() {
    this.getTree();
  },
  onShow() {
    this.getStatistics();
  },
  onHide() {
    clearTimeout(this.timer);
  },
  onUnload() {
    clearTimeout(this.timer);
  },
  methods: {
    getStatistics() {
      // 获取工单统计数
      statistics()
        .then((res) => {
          this.workOrder = res;
          console.log("this.operationsList---", this.operationsList);
          console.log("res", res);
          this.operationsList.forEach((item) => {
            if (item.name == "保养工单") item.badge = res.maintenanceNum;
            if (item.name == "巡点检工单") item.badge = res.patrolInspection;
            if (item.name == "维修工单") item.badge = res.repairsNum;
          });
        })
        .finally(() => {
          this.timer = setTimeout(() => {
            this.getStatistics();
          }, 12000);
        });
    },
    getTree() {
      let _list = uni.getStorageSync("treeList");
      let list = JSON.parse(_list) || [];
      console.log("-----------list--------------");
      console.log(list);
      if (list[0] && list[0].children.length > 0) {
        console.log(list[0].children, "list[0].children ----");
        list[0].children.forEach((f) => {
          if (f.path == "productionManage") {
            this.productionList = f.children;
          } else if (f.path == "operationsList") {
            statistics().then((res) => {
              this.workOrder = res;
              console.log("this.operationsList---", this.operationsList);
              console.log("res", res);
              f.children.forEach((item) => {
                if (item.name == "保养工单") item.badge = res.maintenanceNum;
                if (item.name == "巡点检工单")
                  item.badge = res.patrolInspection;
                if (item.name == "维修工单") item.badge = res.repairsNum;
              });
              this.operationsList = f.children;
            });
          } else if (f.path == "warehouseManagement") {
            this.warehousingList = f.children;
          } else if (f.path == "saleManageList") {
            this.saleManageList = f.children;
            this.saleManageList.forEach((item) => {
              item.name = item.name.replace("员工", "");
              item.name = item.name.replace("管理员", "");
            });
          } else if (f.path == "internalManagement") {
            console.log(f, "f");
            this.internalManagementList = f.children;
          } else if (f.path == "serviceList") {
            this.serviceList = f.children;
          } else if (f.path == "traceability") {
            this.traceabilityList = f.children;
          } else if (f.path == "purchasingManage") {
            this.purchaseManageList = f.children;
          } else if (f.path == "qualityManage") {
            this.qmsList = f.children;
          } else if (f.path == "productionControlManagement") {
            this.pcsList = f.children;
          }
        });
      }
    },

    toNav(url) {
      console.log(url);
      uni.navigateTo({
        url: url,
      });
    },
    HandlScanCode() {
      let _this = this;
      uni.scanCode({
        success: function (res) {
          console.log(res);
          // 获取扫描结果中的URL
          const scanResult = res.result;
          // 检查URL是否包含traceability
          if (scanResult && scanResult.includes("/traceability")) {
            // 从扫描结果中提取id参数
            let id = "";
            if (scanResult.includes("id=")) {
              // 分割URL获取id参数值
              const idPart = scanResult.split("id=")[1];
              // 如果id后面还有其他参数，只取到第一个&之前的部分
              if (idPart.includes("&")) {
                id = idPart.split("&")[0];
              } else {
                id = idPart;
              }
            }
            // 跳转到溯源页面并传递id
            uni.navigateTo({
              url: `/pages/traceability/scanCode/index?id=${id}`,
            });
            return;
          }
          _this.scantoRouter(scanResult);
        },
      });
    },
    scantoRouter(result) {
      uni.navigateTo({
        url: `/pages/pda/workOrder/extrusionMolding/index?id=${result}`,
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "index.scss";
</style>
