import Pannel from '@/components/pannel/index.vue';
import PannelTabs from '@/components/pannel-tabs/index.vue';
import LineCharts from '@/components/charts/line/index.vue';
import PieCharts from '@/components/charts/pie/index.vue';
import { mapGetters } from 'vuex';
import { analysisOfCapitalExpenditureType, analysisOfCapitalExpenditureRend } from '../../../../api/index.js';

export default {
  components: {
    LineCharts,
    PieCharts,
    PannelTabs,
    Pannel
  },
  data () {
    return {
      data: [], // 数据
      isLoad: false, // 是否加载完成
      tabIndex: '1'
    };
  },
  computed: {
    ...mapGetters(['year', 'deptId'])
  },
  created () {
  },
  mounted () {
  },
  watch: {
    year: {
      handler () {
        this.getData();
      },
      immediate: true, // 立即执行
    },
    deptId: {
      handler () {
        this.getData();
      },
      immediate: true, // 立即执行
    }
  },
  methods: {
    tabChange (info) {
      //console.log(info);
      this.tabIndex = info.id
      this.getData()
    },
    // 获取数据
    getData () {
      if (this.year || this.deptId) {

        this.isLoad = false;
        // 如果是趋势
        if (this.tabIndex == 1) {
          analysisOfCapitalExpenditureRend(this.year, this.deptId).then(res => {
            let data = res.data.map(item => {
              return {
                name: item.name + '月',
                value: item.value
              }
            })
            this.data = data;
            this.isLoad = true;
          })
        } else if (this.tabIndex == 2) {
          // 获取类型
          analysisOfCapitalExpenditureType(this.year, this.deptId).then(res => {

            let data = res.data.list.map(item => {

              return {
                name: item.name,
                value: item.value,
                unit: '万元'
              }
            })
            this.data = data
            this.isLoad = true;
          })
        }

      }
    },
  }
};
