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 { leftbottomcapitalgainstrend, leftbottomcapitalgainstype } from '../../../../api/index.js';

const map = {
  '经营收入': '万元',
  '补助收入': '万元',
  '投资收益': '万元',
  '其他收入': '万元'
}
export default {
  components: {
    LineCharts,
    PieCharts,
    PannelTabs,
    Pannel
  },
  data () {
    return {
      data: [], // 数据
      isLoad: false, // 是否加载完成
      tabIndex: '1',
      titleNum: 0
    };
  },
  computed: {
    ...mapGetters(['year', 'deptId'])
  },
  watch: {
    year: {
      handler () {
        this.getData();
      },
      immediate: true, // 立即执行
    },
    deptId: {
      handler () {
        this.getData();
      },
      immediate: true, // 立即执行
    }
  },
  methods: {
    // 获取数据
    getData () {
      if (this.year || this.deptId) {
        this.isLoad = false;
        this.titleNum = 0;
        // 如果是趋势
        if (this.tabIndex == 1) {
          leftbottomcapitalgainstrend( this.deptId, this.year).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) {
          // 获取类型
          leftbottomcapitalgainstype(this.deptId, this.year).then(res => {
            this.titleNum = res.data.total;
            let data = res.data.list.map(item => {
              return {
                name: item.name,
                value: item.value,
                unit: this.unitMaker(item.name)
              }

            })
            this.data = data
            this.isLoad = true;
          })
        }

      }
    },
    unitMaker (val) {
      return map[val];
    },
    tabChange (info) {
      this.tabIndex = info.id
      this.getData()
    }
  }
};
