<?php /*a:2:{s:54:"D:\phpEnv\www\SCM-API\app\admin\view\addons\index.html";i:1750736984;s:52:"D:\phpEnv\www\SCM-API\app\admin\view\layout\vue.html";i:1750736984;}*/ ?>

    import table from "http://yun.cn/assets/js/components/Table.js";
    import {rand} from "http://yun.cn/assets/js/util.js";
    //生成订单号
    function create_out_trade_no()
    {
        let date=new Date();
        let year=date.getFullYear();
        let month=date.getMonth()+1;
        let day=date.getDate();
        if(day<10){
            day='0'+day;
        }
        let hour=date.getHours();
        if(hour<10){
            hour='0'+hour;
        }
        let minute=date.getMinutes();
        if(minute<10){
            minute='0'+minute;
        }
        let seconds=date.getSeconds();
        if(seconds<10){
            seconds='0'+seconds;
        }
        let r=''+year+month+day+hour+minute+seconds+rand(10000,99999);
        return r;
    }
    function formatTime(seconds)
    {
        let interval = Math.floor(seconds / 31536000);
        if (interval >= 1) {
            return `${interval}年后`;
        }
        interval = Math.floor(seconds / 2592000);
        if (interval >= 1) {
            return `${interval}月后`;
        }
        interval = Math.floor(seconds / 86400);
        if (interval >= 1) {
            return `${interval}天后`;
        }
        interval = Math.floor(seconds / 3600);
        if (interval >= 1) {
            return `${interval}小时后`;
        }
        interval = Math.floor(seconds / 60);
        if (interval >= 1) {
            return `${interval}分钟后`;
        }
        return `${Math.floor(seconds)}秒后`;
    }
    export default{
        components:{'YunTable':table},
        data:{
            extend:{
                index_url: 'addons/index',
                multi_url: 'addons/multi'
            },
            type:Yunqi.data.type,
            tabsValue:'plugin',
            plain:'all',
            columns:[
                {field:"plain",visible:'none',operate:{form:'hidden',value:function (){return Yunqi.app.plain},filter:false}},
                {field:"name",title:"扩展名称"},
                {field:"type",title:"扩展类型",searchList:Yunqi.data.type,operate:false},
                {field:"author",title:"作者"},
                {field:"price",title:"价格",width: 80,formatter:function (data) {
                        let tag=Yunqi.formatter.tag;
                        if (data) {
                            tag.type='primary';
                            tag.value='￥'+data;
                        }else{
                            tag.type='success';
                            tag.value='免费';
                        }
                        return tag;
                    }},
                {field:"version",title:"版本号",width: 80},
                {field:"document",title:"说明文档",formatter: function (data){
                        if(data) {
                            let link=Yunqi.formatter.link;
                            link.value = data;
                            return link;
                        }else{
                            return '';
                        }
                    }},
                {field:"description",title:"简介",width: 300},
                {
                    field:"local",
                    title:"本地",
                    width: 80,
                    formatter: function (data){
                        let tag=Yunqi.formatter.tag;
                        if(data){
                            tag.value='是';
                            tag.type='success';
                        }else{
                            tag.value='否';
                            tag.type='danger';
                        }
                        return tag;
                    }
                },
                {
                    field:"packed",
                    title:"打包",
                    width: 80,
                    formatter:function (data,row){
                        if(!row.local){
                            return '';
                        }
                        let tag=Yunqi.formatter.tag;
                        if(data){
                            tag.value='是';
                            tag.type='primary';
                        }else{
                            tag.value='否';
                            tag.type='danger';
                        }
                        return tag;
                    }
                },
                {
                    field:"open",
                    title:"开放",
                    width: 80,
                    formatter:function (data,row){
                        if(!row.local){
                            return '';
                        }
                        let sw=Yunqi.formatter.switch;
                        sw.activeValue=1;
                        sw.inactiveValue=0;
                        sw.value=row.packed?data:0;
                        sw.disabled=true;
                        if(row.packed){
                            sw.disabled=false;
                        }
                        return sw;
                    }
                },
                {
                    field: 'operate',
                    title: __('操作'),
                    direction:'column',
                    width:100,
                    action:{
                        download:{
                            text:__('下载'),
                            type:'primary',
                            tooltip:false,
                            icon:'fa fa-download',
                            method:'downloadAddon',
                            visible:function (row) {
                                return !row.download;
                            }
                        },
                        install:{
                            text:__('安装'),
                            type:'primary',
                            tooltip:false,
                            icon:'fa fa-wrench',
                            method:'installAddon',
                            visible:function (row) {
                                return row.download && !row.install;
                            }
                        },
                        pack:{
                            text:__('打包'),
                            type:'primary',
                            tooltip:false,
                            icon:'fa fa-briefcase',
                            method:'packAddon',
                            visible:function (row) {
                                return row.download && row.install && !row.packed;
                            }
                        },
                        edit:{
                            text:__('编辑'),
                            type:'warning',
                            tooltip:false,
                            icon:'fa fa-edit',
                            method:'editAddon',
                            visible:function (row) {
                                return row.local && row.is_author && !row.packed;
                            }
                        },
                        uninstall:{
                            text:__('卸载'),
                            type:'danger',
                            tooltip:false,
                            icon:'fa fa-remove',
                            method:'uninstallAddon',
                            visible:function (row) {
                                return row.install && row.packed;
                            }
                        },
                        remove:{
                            text:__('删除'),
                            type:'danger',
                            tooltip:false,
                            icon:'fa fa-remove',
                            method:'delAddon',
                            visible:function (row) {
                                return row.download && !row.install;
                            }
                        },
                    },
                }
            ],
            buyDialog:{
                show:false,
                out_trade_no:'',
                transaction_id:'',
                expire_time:0,
                status:0,
                message:'',
                code_url:'',
                row:''
            }
        },
        onLoad:function (){
            setInterval(()=>{
                this.checkPayStatus();
            },2000)
        },
        methods:{
            changePlain:function (plain) {
                this.plain = plain;
                this.$refs.yuntable.submit();
            },
            packAddon:function(row) {
                Yunqi.ajax.post('addons/pack',row).then(res=>{
                    this.$refs.yuntable.reload();
                });
            },
            editAddon:function (row){
                let that=this;
                Yunqi.api.open({
                    title: __('修改本地扩展'),
                    url: 'addons/create?id='+row.id,
                    icon:'fa fa-edit',
                    close:function (r) {
                        if(r){
                            that.$refs.yuntable.reload();
                        }
                    }
                });
            },
            downloadAddon:function(row){
                if(row.price>0){
                    this.buyDialog.out_trade_no=create_out_trade_no();
                    this.buyDialog.code_url=Yunqi.config.baseUrl+'addons/payCode?key='+row.key+'&out_trade_no='+this.buyDialog.out_trade_no;
                    this.buyDialog.show=true;
                    this.buyDialog.row=row;
                }else{
                    Yunqi.ajax.post('addons/download', row).then(res=>{
                        this.$refs.yuntable.reload();
                    });
                }
            },
            payDownload:function (){
                let postdata={...this.buyDialog.row,transaction_id:this.buyDialog.transaction_id}
                Yunqi.ajax.post('addons/download',postdata).then(res=>{
                    this.closeBuyDialog();
                    this.$refs.yuntable.reload();
                });
            },
            checkPayStatus:function (){
                if(this.buyDialog.show){
                    Yunqi.ajax.get('addons/checkPayStatus',{out_trade_no:this.buyDialog.out_trade_no,key:this.buyDialog.row.key}).then(res=>{
                        if(res){
                            this.buyDialog.transaction_id=res.transaction_id;
                            this.buyDialog.expire_time=res.expire_time;
                            this.buyDialog.message='支付成功';
                            this.buyDialog.status=1;
                        }
                    });
                }
            },
            checkTransactionId:function (){
                let postdata={
                    pack:this.buyDialog.row.pack,
                    transaction_id:this.buyDialog.transaction_id
                };
                Yunqi.ajax.get('addons/checkTransactionId',postdata).then(res=>{
                    this.buyDialog.status=res.status;
                    this.buyDialog.expire_time=res.expire_time;
                    if(res.status==0){
                        this.buyDialog.message='交易单号不存在';
                    }else{
                        if(res.expire_time>0){
                            this.buyDialog.message=formatTime(res.expire_time)+'过期';
                        }else{
                            this.buyDialog.message='交易单号已经过期';
                        }
                    }
                });
            },
            closeBuyDialog:function () {
                this.buyDialog={
                    show:false,
                    out_trade_no:'',
                    transaction_id:'',
                    expire_time:0,
                    status:0,
                    message:'',
                    code_url:'',
                    row:''
                }
            },
            installAddon:function(row)
            {
                Yunqi.ajax.post('addons/install',{key:row.key}).then(res=>{
                    this.$refs.yuntable.reload();
                });
            },
            uninstallAddon:function(row)
            {
                let that=this;
                Yunqi.api.open({
                    title: __('卸载扩展'),
                    url: 'addons/uninstall?key='+row.key,
                    icon:'fa fa-remove',
                    close:function (r) {
                        if(r){
                            that.$refs.yuntable.reload();
                        }
                    }
                });
            },
            delAddon:function (row){
                let that=this;
                Yunqi.confirm('删除扩展将会清空掉所有文件，重新下载扩展可能会再次收取费用，你确定要删除吗？','提醒').then(res=>{
                    Yunqi.ajax.post('addons/del',{key:row.key}).then(res=>{
                        that.$refs.yuntable.reload();
                    });
                });
            },
            createAddon:function(){
                let that=this;
                Yunqi.api.open({
                    title: __('创建本地扩展'),
                    url: 'addons/create',
                    icon:'fa fa-plus',
                    close:function (r) {
                        if(r){
                            that.$refs.yuntable.reload();
                        }
                    }
                });
            }
        }
    }
