<template>

    <eye-page-popup :visible.sync="currentShowBreed"
                    :title="breedTitle">

        <Breed :dataProvider="currentBreedList"
               @select="onItemSelect"></Breed>

    </eye-page-popup>

</template>

<script type="text/babel">

  import  { Component,Field }  from 'eye-core';
  import CategoryService from 'eye-com/src/services/common/CategoryService';

  import Breed from './Breed';

  export default Component({

    name: 'breed-popup',

    mixins:[ Field ],

    services: [ CategoryService ],

    components:{
        Breed
    },

    props: {
        cateId:[String,Number],

        showBreedPopup: {  // 是否支持品种
            type: Boolean,
            default: false
        },

        // 是否支持单位加载
        supportUnit: {
            type: Boolean,
            default: false
        },
    },

    data() {
        return {
            currentCateId:this.cateId,
            currentShowBreed:false,
            breedTitle:this.title,
            currentBreedList: []
        }
    },

    watch: {

        'cateId'(newValue,oldValue) {
            if(!newValue || newValue === oldValue) return;
            this.load(newValue);
        },
    },

    methods: {

        onItemSelect(e) {
            this.currentShowBreed = false
            this.onSelectChange(e)
        },

        load(cateId) {

            this.currentCateId = cateId;
            this.$service.category.findBreedSpecOrUnitList(cateId,this.supportUnit).then(res => {
                let { showBreed,cateName,breeds } = res || {};
                this.currentBreedList = breeds;
                if(this.showBreedPopup && showBreed) {
                    this.breedTitle = `选择${cateName}品种`
                    this.currentShowBreed = true;
                }
                this.$emit('loaded',res)
            })

            // Promise.all(promiseList).then(resList => {
            //
            //     let { breeds, specifications,cateName } = resList[0];
            //     let showBreed = breeds && !!breeds.length && this.showBreedPopup;
            //     let showSpec = specifications && !!specifications.length;
            //     let units = [];
            //     this.currentBreedList = breeds;
            //
            //     if(showBreed) {
            //         this.breedTitle = `选择${cateName}品种`
            //         this.currentShowBreed = true;
            //     }
            //
            //     if(this.supportUnit && resList.length > 1) {
            //         units = resList[1];
            //     }
            //
            //     this.$emit('loaded',{ breeds, specifications, units, showBreed, showSpec })
            // })

        }

    }

  })


</script>

