<template>
  <div class="testdiv">
    <div class="top_title">
      <span>设备维修看板</span>
      <img :src="imgLogo" class="logo" @click="back" />
      <div class="times_bg">{{ nowTime }}</div>
      <Icon
        class="backBtn"
        type="md-arrow-round-back"
        size="40"
        @click="back"
      />
      <div class="IconSetting">
        <img src="../../../static/images/setting.svg" @click="setting()" />
      </div>
    </div>

    <Modal class="settingmodal" v-model="settingmodal" :styles="modalStyle">
      <div class="modal_body" style="width: 430px; padding: 0 0">
        <div class="filter">
          <label>选择展示岗位：</label>
          <Select v-model="usergroup" label-in-value style="width: 200px">
            <Option :value="1">全部</Option>
            <Option :value="2">TS</Option>
            <Option :value="3">非TS</Option>
          </Select>
        </div>
      </div>
      <div slot="footer">
        <Button @click="settingmodal = false">取消</Button>
        <Button type="primary" @click="settingadd()">确定</Button>
      </div>
    </Modal>

    <div class="container">
      <!-- 左边看板 -->
      <div class="formBox_left">
        <!-- 维修班组状态 -->
        <div class="formBox compre_analysis">
          <div class="form_title">维修班组状态</div>
          <div class="formMain table_bg team_status" style="padding: 15px 30px">
            <table class="teamTable">
              <ul style="height: 250px; overflow-y: hidden">
                <vue-seamless-scroll
                  :data="teamLists"
                  :class-option="optionSetting3"
                  class="seamless-warp tbaleSlidex"
                >
                  <li v-if="!teamLists.length"></li>
                  <li v-for="(v, i) in teamLists" :key="i" class="t_sle" v-else>
                    <div class="t_sle_col1">
                      <span style="display: inline-block; height: 48px">
                        <img
                          v-if="v.state == '在岗'"
                          class="t_sle_ol"
                          src="../../../static/images/u1212.png"
                        />
                        <img
                          v-else
                          class="t_sle_no_ol"
                          src="../../../static/images/u1211.png"
                        />
                      </span>
                      <span class="t_sle_col1_name">{{ v.name }}</span>
                    </div>
                    <div class="t_sle_col2">{{ v.state }}</div>
                    <div class="t_sle_col3">{{ v.phone }}</div>
                  </li>
                </vue-seamless-scroll>
              </ul>
            </table>
          </div>
        </div>
        <!-- 设备故障分布 -->
        <div class="formBox">
          <div class="form_title">设备故障分布</div>
          <div class="formMain table_bg pie_bg">
            <div class="pieForm_M" id="pieForm_M"></div>
          </div>
        </div>
      </div>

      <!-- 中间看板 -->
      <div class="formBox_middle">
        <div class="middle_top">
          <div class="failureRateBox">
            <div class="failureRat failureRate_left">
              <div class="day_rate">本日故障次数</div>
              <div class="daysRate rate">{{ daysRate }}</div>
            </div>
            <div class="failureRat failureRate_right">
              <div class="month_rate">本月故障次数</div>
              <div class="monthRate rate">{{ monthRate }}</div>
            </div>
          </div>
        </div>

        <div class="middle_table">
          <div class="duty_table">
            <ul class="dutyForm">
              <vue-seamless-scroll
                :data="memberList"
                :class-option="optionSetting"
                class="seamless-warp tbaleSlide"
              >
                <li v-for="(item, index) in memberList" :key="index">
                  <ul class="table_row">
                    <li>{{ item.orderNum }}</li>
                    <li>{{ item.device }}</li>
                    <li>{{ item.failure }}</li>
                    <li>
                      <div class="red" v-if="item.state == 1">待接单</div>
                      <div class="orande" v-if="item.state == 3">维修中</div>
                      <div class="pink" v-if="item.state == 2">待签到</div>
                      <div class="green" v-if="item.state == 4">待复线</div>
                      <div class="green" v-if="item.state == 6">已完成</div>
                      <div class="blue" v-if="item.state == 5">待反馈</div>
                    </li>
                    <li>{{ item.time }}</li>
                  </ul>
                </li>
              </vue-seamless-scroll>
            </ul>
          </div>
        </div>

        <div class="middle_bottom">
          <div class="formBox">
            <ul class="table_title_bg">
              <li>维护设备</li>
              <li>维护项目</li>
              <li>执行人</li>
            </ul>
            <vue-seamless-scroll
              :data="repairLists"
              :class-option="optionSetting1"
              class="seamless-warp tbaleSlide"
            >
              <ul class="tableDetail">
                <li v-if="!repairLists.length"></li>
                <li v-for="(item, index) in repairLists" :key="index" v-else>
                  <div>{{ item.device }}</div>
                  <div>{{ item.project }}</div>
                  <div>{{ item.executor }}</div>
                </li>
              </ul>
            </vue-seamless-scroll>
          </div>
        </div>
      </div>

      <!-- 右边看板 -->
      <div class="formBox_left formBox_right">
        <div class="formBox compre_analysis">
          <div class="form_title">平均修复时间</div>
          <div class="formMain table_bg">
            <div class="barForm_M" id="barForm_M"></div>
          </div>
        </div>

        <div class="formBox">
          <div class="form_title">备件消耗报警</div>
          <div class="formMain table_bg spare_part">
            <div>
              <div class="table_title">消耗TOP5</div>
              <div class="alarmForm" id="alarmForm"></div>
            </div>
            <div class="alarmTable">
              <ul class="table_title_bg">
                <li>报警备件</li>
                <li>当前库存</li>
                <li>采购库存</li>
                <li>安全库存</li>
              </ul>
              <ul class="tableDetail">
                <vue-seamless-scroll
                  :data="partLists"
                  :class-option="optionSetting2"
                  class="seamless-warp tbaleSlide"
                >
                  <li v-if="!partLists.length"></li>
                  <li v-for="(item, index) in partLists" :key="index" v-else>
                    <div>{{ item.sparepart }}</div>
                    <div class="currentColor">{{ item.currentStock }}</div>
                    <div>{{ item.purchaseStock }}</div>
                    <div>{{ item.safetyStock }}</div>
                  </li>
                </vue-seamless-scroll>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import moment from "moment";
import echarts from "echarts";
import axios from "@/libs/api.request";
import vueSeamlessScroll from "vue-seamless-scroll";
import { setInterval, clearInterval } from "timers";

export default {
  data() {
    return {
      imgLogo: require("../../../static/images/siger.png"),
      teamLists: [],
      daysRate: "000",
      monthRate: "000",
      dutyTeam: "",
      memberList: [],
      repairLists: [],
      partLists: [],
      li_array: "",
      timer: 0,
      timeAll: 0,
      timerx: 0,
      nowTime: moment(new Date().getTime()).format("YYYY-MM-DD"),
      settingmodal: false,
      display_post: 1,
      ids: [],
      usergroup: 1,
    };
  },
  computed: {
    optionSetting: function () {
      return {
        step: 0.2,
        singleHeight: 336,
        waitTime: 6000,
      };
    },
    optionSetting1() {
      return {
        step: 0.2,
        singleHeight: 228,
        waitTime: 6000,
      };
    },
    optionSetting2() {
      return {
        step: 0.2,
        singleHeight: 138,
        waitTime: 6000,
      };
    },
    optionSetting3() {
      return {
        step: 0.2,
        singleHeight: 138,
        limitMoveNum: 3,
      };
    },
    modalStyle() {
      return {
        position: "fixed",
        top: "60px",
        right: "35px",
        width: "450px",
      };
    },
  },
  components: {
    vueSeamlessScroll,
  },
  created() {
    this.GetdisplayList();
  },
  mounted() {
    if (this.$store.state.user.logo) {
      this.imgLogo = this.$store.state.user.logo;
    }
    if (this.timer) {
      clearInterval(this.timer);
    } else {
      this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
      this.GetUserStateList();
      this.GetMachineRepairSummary();
      this.GetRepairList();
      this.GetPlanList();
      this.GetSparepares();
      this.GetMTTR();
      // 定时 10 分钟执行一次
      this.timer = setInterval(() => {
        this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
        this.GetUserStateList();
        this.GetMachineRepairSummary();
        this.GetRepairList();
        this.GetPlanList();
        this.GetSparepares();
        this.GetMTTR();
      }, 1000 * 60 * 10);
    }

    if (this.timeAll) {
      clearInterval(this.timeAll);
    } else {
      // 两小时刷新一次
      this.timeAll = setInterval(() => {
        location.reload();
      }, 1000 * 60 * 60 * 2);
    }

    this.timerx = setInterval(() => {
      this.GetRepairList();
    }, 1000 * 60);
  },
  methods: {
    GetdisplayList() {
      axios
        .request({
          url: "/config/ProjectGroup/GetUserGroups?departid=9",
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.displayList = res.data.data;
          }
        });
    },
    setting() {
      this.settingmodal = true;
    },
    settingadd() {
      if (this.timer) {
        clearInterval(this.timer);
        this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
        this.GetUserStateList();
        this.GetMachineRepairSummary();
        this.GetRepairList();
        this.GetPlanList();
        this.GetSparepares();
        this.GetMTTR();
        // 定时 10 分钟执行一次
        this.timer = setInterval(() => {
          this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
          this.GetUserStateList();
          this.GetMachineRepairSummary();
          this.GetRepairList();
          this.GetPlanList();
          this.GetSparepares();
          this.GetMTTR();
        }, 600000);
      } else {
        this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
        this.GetUserStateList();
        this.GetMachineRepairSummary();
        this.GetRepairList();
        this.GetPlanList();
        this.GetSparepares();
        this.GetMTTR();
        // 定时 10 分钟执行一次
        this.timer = setInterval(() => {
          this.nowTime = moment(new Date().getTime()).format("YYYY-MM-DD");
          this.GetUserStateList();
          this.GetMachineRepairSummary();
          this.GetRepairList();
          this.GetPlanList();
          this.GetSparepares();
          this.GetMTTR();
        }, 600000);
      }

      // 俩小时整个页面刷新一次
      if (this.timeAll) {
        clearInterval(this.timeAll);
      } else {
        // 两小时后刷新,刷回当前时间
        this.timeAll = setInterval(() => {
          location.reload();
        }, 1000 * 60 * 60 * 2);
      }
      this.timerx = setInterval(() => {
        this.GetRepairList();
      }, 1000 * 60);
      this.settingmodal = false;
    },
    GetUserStateList() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetUserStateList",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret == 1) {
            let data = [];
            res.data.data.forEach((item) => {
              if (item) {
                data.push({
                  name:
                    item.user_name && item.user_name.length > 5
                      ? item.user_name.substring(0, 5)
                      : item.user_name,
                  state: item.status,
                  phone: item.mobile,
                });
              }
            });
            this.teamLists = data;
          }
        });
    },
    GetMachineRepairSummary() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetMachineRepairSummary",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.toPie(res.data.data.repairs, res.data.data.total);
            this.list = res.data.data.repairs;
            this.daysRate = res.data.data.today_percent;
            this.monthRate = res.data.data.month_percent;
          }
        });
    },
    GetRepairList() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetRepairList",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            let data = [];
            res.data.data.forEach(function (item, key) {
              if (item) {
                data.push({
                  orderNum: item.id,
                  device: item.machinename,
                  failure: item.faultcontent == "" ? "NA" : item.faultcontent,
                  state: item.status,
                  time: item.createtimeformat,
                });
              }
            });
            this.memberList = data;
          }
        });
    },
    GetPlanList() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetPlanList",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            let data = [];
            res.data.data.forEach((item) => {
              if (item) {
                data.push({
                  device:
                    item.machinetitle && item.machinetitle.length > 12
                      ? item.machinetitle.substring(0, 12)
                      : item.machinetitle,
                  project: item.content,
                  usetime: item.standard_time,
                  executor:
                    item.name && item.name.length > 5
                      ? item.name.substring(0, 5)
                      : item.name,
                });
              }
            });
            this.repairLists = data;
          }
        });
    },
    GetSparepares() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetSparepares",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            let data = [];
            res.data.data.sparepareSkus.forEach((item) => {
              if (item) {
                data.push({
                  sparepart:
                    item.name && item.name.length > 6
                      ? item.name.substring(0, 6) + "..."
                      : item.name,
                  currentStock: item.current_sku,
                  purchaseStock: item.purchase_sku,
                  safetyStock: item.safe_sku,
                });
              }
            });
            this.partLists = data;
            this.toAlarmBar(res.data.data.sparepareUses);
          }
        });
    },
    GetMTTR() {
      var params = {
        usergroup: this.usergroup,
      };
      axios
        .request({
          url: "/dashboard/DashboardRepair/GetMttr",
          params,
          method: "get",
        })
        .then((res) => {
          if (res.data.ret === 1) {
            this.toBar(res.data.data);
          }
        });
    },
    toPie(item, total) {
      let data = [];

      let nameArray = [];
      item.forEach((ele) => {
        nameArray.push(ele.type_nme);
        data.push({
          value: ele.count,
          name: ele.type_nme,
        });
      });

      var myChart = echarts.init(document.getElementById("pieForm_M"));
      var option = {
        title: {
          text: "故障总数",
          subtext: total,
          x: "center",
          y: "center",
          textStyle: {
            fontSize: 16,
            color: "#fff",
          },
          subtextStyle: {
            fontSize: 16,
            color: "#fff",
          },
        },
        tooltip: {
          show: true,
          trigger: "item",
          formatter: "{a} <br/>{b}: {c} ({d}%)",
        },
        color: [
          "#FF9318",
          "#FF40AF",
          "#0087FF",
          "#8FEB5B",
          "#FFD669",
          "#7F25FE",
        ],
        legend: {
          left: "center",
          bottom: 0,
          data: nameArray,
          textStyle: {
            color: "#fff",
          },
        },
        series: [
          {
            name: "设备故障分布",
            type: "pie",
            radius: ["45%", "60%"],
            center: ["50%", "50%"],
            label: {
              formatter: "{d}%",
              color: "#fff",
            },
            labelLine: {
              lineStyle: {
                color: "#fff",
              },
            },
            data: data,
          },
        ],
      };
      myChart.setOption(option);
      window.onresize = function () {
        myChart.resize();
      };
    },
    toBar(item) {
      let date = [];
      let value = [];
      item.forEach((ele) => {
        date.push(ele.date);
        value.push(ele.repair_time);
      });
      var myChart = echarts.init(document.getElementById("barForm_M"));
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        textStyle: {
          color: "#a6a6a6",
        },
        grid: {
          left: "3%",
          top: "12%",
          right: "4%",
          bottom: "10%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: date,
            axisTick: {
              alignWithLabel: true,
            },
            nameTextStyle: {
              color: "#a6a6a6",
              padding: [2, 0, 0, -2],
            },
            axisLine: {
              show: false,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            axisLine: {
              show: false,
            },

            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
              },
            },
          },
        ],
        series: [
          {
            name: "",
            type: "bar",
            barWidth: "16",
            barCategoryGap: "48",
            data: value,
            itemStyle: {
              color: "#156ED3",
            },
            label: {
              show: true,
              position: "top",
              textStyle: {
                color: "#ffffff",
                fontSize: 12,
              },
              formatter: function (params) {
                if (params.value > 0) {
                  return params.value;
                } else {
                  return "";
                }
              },
            },
          },
        ],
      };
      myChart.setOption(option);
      window.onresize = function () {
        myChart.resize();
      };
    },
    toAlarmBar(item) {
      let names = [];
      let counts = [];
      item.forEach((ele) => {
        names.push(
          ele.name.length > 8 ? ele.name.substring(0, 8) + "..." : ele.name
        );
        counts.push(ele.count);
      });
      let lineshow = true;
      if (counts.length == 0) {
        lineshow = false;
      }
      var myChart = echarts.init(document.getElementById("alarmForm"));
      var option = {
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        textStyle: {
          color: "#a6a6a6",
        },
        grid: {
          left: "20%",
          top: "7%",

          x: "8%",
          y: 0,
        },
        xAxis: [
          {
            type: "value",
            boundaryGap: [0, 0.01],
            axisLine: {
              show: false,
            },
            splitLine: {
              show: true,
              lineStyle: {
                type: "dashed",
                color: "#a6a6a6",
              },
            },
          },
        ],
        yAxis: [
          {
            type: "category",
            data: names,
            axisLine: {
              show: lineshow,
            },
          },
        ],
        series: [
          {
            type: "bar",
            barWidth: "16",
            barCategoryGap: "48",
            data: counts,
            label: {
              show: true,
              position: "right",
              textStyle: {
                color: "#ffffff",
                fontSize: 14,
              },
            },
          },
        ],
      };
      myChart.setOption(option);
      window.onresize = function () {
        myChart.resize();
      };
    },
    back() {
      this.$router.go(-1);
    },
  },
  beforeDestroy() {
    clearInterval(this.timer);
    clearInterval(this.timeAll);
    clearInterval(this.timerx);
  },
};
</script>

<style lang="less" scoped>
@import "./repair.less";
.pieForm_M {
  height: 400px;
  width: 100%;
  margin: 0 auto;
}
.barForm_M {
  width: 522px;
  height: 264px;
}
.alarmForm {
  width: 500px;
  height: 198px;
  margin: 0 auto;
}
.t_sle {
  height: 48px;
  color: #fff;
  font-size: 14px;
  .t_sle_col1 {
    width: 33%;
    display: inline-block;
    line-height: 48px;
    overflow: auto;
    white-space: nowrap;
    .t_sle_ol {
      width: 36px;
      display: inline-block;
      line-height: 48px;
      vertical-align: middle;
    }
    .t_sle_no_ol {
      width: 36px;
      display: inline-block;
      line-height: 48px;
      vertical-align: middle;
    }
    .t_sle_col1_name {
      display: inline-block;
      line-height: 48px;
      margin-left: 20px;
    }
  }
  .t_sle_col2 {
    width: 33%;
    display: inline-block;
    line-height: 48px;
    text-align: center;
    overflow: auto;
    white-space: nowrap;
  }
  .t_sle_col3 {
    width: 30%;
    display: inline-block;
    line-height: 48px;
    text-align: center;
    overflow: auto;
    white-space: nowrap;
  }
}
</style>
