import Pannel from '@/components/pannel/index.vue';
import BarSpecial from '@/components/charts/bar-special/index.vue';
import { distributionOfLiabilities } from '../../../../api/index.js';
import { mapGetters } from 'vuex';

export default {
  components: {
    BarSpecial,
    Pannel
  },
  computed: {
    ...mapGetters(['year', 'deptId'])
  },
  watch: {
    year: {
      handler () {
        this.getData();
      }
    },
    deptId: {
      handler () {
        this.getData();
      }
    }
  },
  data () {
    return {
      isLoad: false,
      currentselect: '',
      data: [],
      dropdown: []
    };
  },
  created () {
    this.getSelectData()
  },
  mounted () {
  },
  methods: {
    getSelectData () {
      if (this.year, this.deptId) {
        distributionOfLiabilities(this.deptId, this.year).then(res => {
          this.dropdown = res.data.map(item => {
            return {
              name: item.name
            }
          })
          this.currentselect = this.dropdown[0].name
          this.getData()
        })
      }
    },
    getData () {
      if (this.year, this.deptId) {
        this.total = 0
        this.isLoad = false;
        distributionOfLiabilities(this.deptId, this.year).then(res => {
          res.data.forEach(item => {
            if (item.name === this.currentselect) {
              let data = [
                {
                  name: '资不抵债',
                  value: item.zbdz
                },
                {
                  name: '高负债率',
                  value: item.gfzl
                },
                {
                  name: '中负债率',
                  value: item.zfzl
                },
                {
                  name: '低负债率',
                  value: item.dfzl
                }
              ]
              this.data = data
            }
          })
          this.isLoad = true;
        })
      }
    },
    handleCommand (item) {
      this.currentselect = item;
      this.getData()
    }
  }
};
