import dateFormat from '@/utils/tool/date';
import test from '@/utils/test/index';
import toast from '@/utils/tool/toast';
export default {

    data(){
      return {
          value:'',
          loading:true,
          parentItem:{}
      }
    },

    computed:{
      disabled(){
          return this.loading || !test.empty(this.value);
      }
    },

    methods:{

        sendMessage(){

            if (this.loading) return;

            if (this.value && test.empty(this.value)) {

                let resultParams = {
                    user_nickname:this.user.nickname,
                    user_avatar: this.user.avatar,
                    user_id: this.user.id,
                    comment: this.value,
                    createtime:  dateFormat(new Date(),'YYYY-MM-DD'),
                    id: 'custom-'+new Date().getTime() +''+ Math.ceil(Math.random() * 1000),
                };

                this.flatUnshift(resultParams);

                this.parentItem.replay++;

                this.$request({
                    url:'product/addCommondReplay',
                    data:{
                        content: this.value,
                        commond_id: this.$params.id
                    },
                    token:true
                });

                this.value = '';
            } else {
                toast.info('请输入评价内容');
            }

        },

        fetch(obj){

            return  this.$request({
                url:'product/getCommondReplayList',
                data:{
                    commond_id:this.$params.id
                },
                next:({status})=> this.loading = status,
                token:true,
                page:obj
            }).then((data)=>{

               let resultData = data.data || [];

                resultData.map((item)=>{
                    item.createtime = dateFormat(item.createtime,'YYYY-MM-DD');

                    item.comment = item.content;

                    delete item.content;
                });

               return obj.success(resultData);
            }).catch(obj.fail);
        },

    },

    onLoad(){

        this.parentItem = this.$memoryParams.item;

    }

}