<template>
  <div id="index">
    <admin-frame @search="search" :limit="limit" :total="total" :usePage="active == '0' ? false : true" :useNav="false">
      <template v-slot:info>
        <van-tabs v-model="active" color="#409eff">
          <van-tab title="设备情况"><equipment-total :totalInfo="totalInfo"></equipment-total></van-tab>
          <van-tab title="设备信息"><list-1 :list="list" @toView="toView"></list-1></van-tab>
        </van-tabs>
      </template>
    </admin-frame>
    <van-dialog class="dialog" v-model="dialog.show" :title="dialog.title" :show-confirm-button="false" show-cancel-button cancel-button-text="返回">
      <info-1 :info="info" v-if="dialog.type == '1'"></info-1>
    </van-dialog>
  </div>
</template>

<script>
import equipmentTotal from '@/layout/infoview/equipment/equipment-total.vue';
import list1 from '@/layout/infoview/equipment/list-1.vue';
import info1 from '@/layout/infoview/equipment/info-1.vue';
import adminFrame from '@common/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('equipment');
const _ = require('lodash');
export default {
  name: 'index',
  props: {},
  components: {
    adminFrame,
    equipmentTotal,
    list1,
    info1,
  },
  data: function () {
    return {
      active: 0,
      list: [],
      total: 0,
      limit: 5,
      // 设备总数信息
      totalInfo: {},
      // 弹框
      dialog: { show: false, title: '详细信息', type: '1' },
      // 详细信息
      info: {},
    };
  },
  async created() {
    await this.search();
    await this.toTotal();
  },
  methods: {
    ...mapActions(['query', 'count']),
    async search({ skip = 0, limit = this.limit, searchName, ...info } = {}) {
      if (searchName) info.name = searchName;
      const res = await this.query({ skip, limit, ...info });
      if (this.$checkRes(res)) {
        this.$set(this, `list`, res.rows);
        this.$set(this, `total`, res.total);
      }
    },
    async toTotal() {
      const res = await this.count();
      if (this.$checkRes(res)) {
        const data = _.head(res.rows);
        const num = 6;
        const prefix = 'count';
        let obj = {};
        for (let n = 1; n <= num; n++) {
          const key = `${prefix}${n}`;
          const value = data[key];
          obj[`p${n}`] = value;
        }
        this.$set(this, `totalInfo`, obj);
        console.log(this.totalInfo);
      }
    },
    // 查看信息
    async toView(data) {
      this.$set(this, `info`, data);
      this.dialog = { show: true, title: '详细信息', type: '1' };
    },
  },
  computed: {
    ...mapState(['user']),
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped>
.dialog {
  /deep/.van-dialog__content {
    max-height: 350px;
    overflow-y: auto;
  }
}
</style>
