import { MatDialog } from "@angular/material/dialog";
import { Router } from "@angular/router";
import Parse from "parse";
import { ParseSchema } from "./func-parse";

export const EduTextbook:ParseSchema = {
    title:"教材",
    className:"EduTextbook",
    emptyImg:"/img/webhook-empty.png",
    include:["user"],
    buttons:[
        // 仅在 /nav-author/manage/space 显示的编辑
        {
            name:"编辑",
            place:"item",
            show:(options:{object:Parse.Object})=>{
                if(location?.pathname=='/nav-author/manage/space' && !options.object.get('render')){
                    return true
                }
                return false
            },
            handle:(options:{dialog:MatDialog,object:Parse.Object,router?:Router})=>{
                options.router?.navigate(['/nav-author/manage/apply',{id:options.object.id}])
            }
        },
        {
            name:"提交材料",
            place:"item",
            show:(options:{object:Parse.Object})=>{
                if(location?.pathname=='/nav-author/manage/space' && !options.object.get('render')){
                    return true
                }
                return false
            },
            handle:(options:{dialog:MatDialog,object:Parse.Object,router?:Router,modal:any})=>{
                options.modal?.confirm({
                    nzTitle: '你确定提交吗?',
                    nzContent: '<p>提交后不可撤回，你可以在已提交评审教材中查看</p>',
                    nzOkText: '确认',
                    nzOkType: 'primary',
                    nzOkDanger: true,
                    nzOnOk:async () => {
                        options.object.set('render',true)
                        await options.object.save()
                        history.go(0)
                    },
                    nzCancelText: '取消',
                    nzOnCancel: () => console.log('Cancel')
                  });
            }
        },
        {
            name:"删除",
            place:"item",
            show:(options:{object:Parse.Object})=>{
                if(location?.pathname=='/nav-author/manage/space' && !options.object.get('render')){
                    return true
                }
                return false
            },
            handle:(options:{dialog:MatDialog,object:Parse.Object,router?:Router,modal?:any})=>{
                options.modal?.confirm({
                    nzTitle: '你确定删除吗?',
                    nzContent: '<p>删除后你可以在回收站恢复</p>',
                    nzOkText: '确认',
                    nzOkType: 'primary',
                    nzOkDanger: true,
                    nzOnOk:async () => {
                        options.object.set('discard',true)
                        await options.object.save()
                        history.go(0)
                    },
                    nzCancelText: '取消',
                    nzOnCancel: () => console.log('Cancel')
                  });
            }
        },
        {
            name:"恢复",
            place:"item",
            show:(options:{object:Parse.Object})=>{
                if(location?.pathname=='/nav-author/manage/recycle' && options.object.get('discard')){
                    return true
                }
                return false
            },
            handle:(options:{dialog:MatDialog,object:Parse.Object,router?:Router,modal?:any})=>{
                options.modal?.confirm({
                    nzTitle: '你确定恢复吗?',
                    nzContent: '<p>恢复后你可以在个人空间中查看</p>',
                    nzOkText: '确认',
                    nzOkType: 'primary',
                    nzOkDanger: true,
                    nzOnOk:async () => {
                        options.object.set('discard',false)
                        await options.object.save()
                        history.go(0)
                    },
                    nzCancelText: '取消',
                    nzOnCancel: () => console.log('Cancel')
                  });
            }
        },
        {
            name:"预览材料",
            place:"item",
            show:(options:{object:Parse.Object})=>{
                if((location?.pathname=='/nav-author/manage/space' && options.object.get('render')) || location?.pathname=='/nav-author/manage/recycle'){
                    return true
                }
                return false
            },
            handle:(options:{dialog:MatDialog,object:Parse.Object,router?:Router})=>{
                options.router?.navigate(['/nav-author/manage/dateils',{id:options.object.id}])
            }
        },
    ],
    fieldsArray:[
        {key:"title",name:"教材名称",type:"String",isHeader:true},
        {key:"typeNumber",name:"册数",type:"Number",isHeader:true},
        {key:"author",name:"第一主编 / 作者",type:"String",isHeader:true},
        {key:"major",name:"所属本科专业",type:"Object",isHeader:true,showName:"${name}"},
        {key:"lang",name:"主要语种类型",type:"String",isHeader:true},
        {key:"ISBN",name:"ISBN",type:"String",isHeader:true},
        {key:"user",name:"创建人",type:"Pointer",targetClass:"_User",isHeader:true,showName:"${mobile}"},
    ]
}