import Pannel from '@/components/pannel/index.vue';
import BarSpecial from '@/components/charts/bar-special/index.vue';
import { debtDistribution } 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) {
        debtDistribution(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;
        debtDistribution(this.deptId, this.year).then(res => {
          res.data.forEach(item => {
            if (item.name === this.currentselect) {
              let data = [
                {
                  name: '资不抵债务',
                  value: item.zbdzw
                },
                {
                  name: '高债务率',
                  value: item.gzwl
                },
                {
                  name: '中债务率',
                  value: item.zzwl
                },
                {
                  name: '低债务率',
                  value: item.dzwl
                }
              ]
              this.data = data
            }
          })
          this.isLoad = true;
        })
      }
    },
    handleCommand (item) {
      this.currentselect = item;
      this.getData()
    }
  }
};
