import { Component, Vue, Inject } from 'vue-property-decorator';
import { ChainStatus } from '@/const/enum';
@Component
export default class Proof extends Vue {
    @Inject('goToBrowser') protected goToBrowser!: (proof: any) => void;
    protected baseProof: any = '';
    protected preProof: any = '';
    protected isZengliang: boolean = false;
    protected showAddProof: boolean = false;
    protected ActionList: any = {
        blockChain: {
          key: 'blockChain',
          icon: 'shanglian1',
          text: '上链',
          callback: this.blockChainProof,
        },
        edit: {
          key: 'edit',
          icon: 'bianji1',
          text: '编辑',
          callback: this.goProofDetail,
        },
        del: {
          key: 'del',
          icon: 'shanchu3',
          text: '删除',
          callback: this.delProof,
        },
        copy: {
          key: 'copy',
          icon: 'fuzhi1',
          text: '复制',
          callback: this.copyProof,
        },
        blockquery: {
          key: 'blockquery',
          icon: 'qukuailianchaxun',
          text: '区块链查询',
          callback: this.goToBrowser,
        },
        copyhash: {
          key: 'copyhash',
          icon: 'fuzhiHashdizhi',
          text: '复制hash地址',
          callback: this.copyHash,
        },
        del2: {
          key: 'del2',
          icon: 'shanchu3',
          text: '删除',
          callback: this.abandonProof,
        },
        zengliang: {
          key: 'zengliang',
          icon: 'zenglianggengxin2',
          text: '增量',
          callback: this.increment,
        },
        hide: {
          key: 'hide',
          icon: 'zenglianggengxin2',
          text: '隐私设置',
          callback: this.hide,
        },
    };
    protected resetList() {
        throw Error('resetList 在列表实现');
    }
    private abandonProof(proof: any) {
        const { id: proofId } = proof;
        this.$confirmdel().then((res: any) => {
          this.$api.proof.abandon(proofId, '暂无原因').then(() => {
                this.$toast.success('已放入回收站');
                this.resetList();
          });
        });
    }
    private  blockChainProof(proof: any) {
        this.$confirmchain().then((res: any) => {
            this.$api.proof.sendBlockChian(proof.id).then(() => {
                this.$toast.success('提交上链成功');
                this.resetList();
            }).catch((err: any) => {
                if (err.code === 10811) {
                this.$dialog.confirm({
                    title: '提示',
                    message: '您还未认证，请先认证后在进行上链',
                    confirmButtonText: '去认证',
                    cancelButtonText: '取消',
                }).then(() => {
                    this.$router.push({name: 'Auth'});
                });
                }
            });
        });
    }
    private goProofDetail(proof: any) {
        const { id: proofId } = proof;
        const toBlockStatuss: ChainStatus[] = [
            ChainStatus.SUCCESS,
            ChainStatus.HIDED,
            ChainStatus.HIDING,
            ChainStatus.HIDEFAILURE,
        ];
        if (toBlockStatuss.includes(proof.status) ) {
          this.goToBrowser(proof);
        } else {
          this.$router.push({ name: 'ProofDetail', query: { proofId }});
        }
    }
    private async copyProof(proof: any) {
        await this.$api.proof.add(proof.name, JSON.parse(proof.detail), proof.note);
        this.$toast.success('复制成功');
        this.resetList();
    }
    private async reName(proof: any) {
        this.$toast('重命名成功');
    }
    private delProof(proof: any) {
        const { id: proofId} = proof;
        this.$dialog.confirm({
            title: '提示',
            message: '删除后，信息无法找回，确定要删除吗？',
            beforeClose: (action, done) => {
            if ( action === 'confirm' ) {
                this.$api.proof.delete(proofId).then((res: any) => {
                this.$toast.success('删除成功');
                this.resetList();
                done();
                }).catch((err: any) => {
                done();
                });
            } else {
                done();
            }
            },
        });
    }
    private copyHash(proof: any) {
        this.$copyText(proof.hash);
        this.$toast('hash地址复制成功');
    }
    private async increment(proof: any) {
        const { increment_num, hash } = proof;
        if ( increment_num > 0 ) {
            const { results } = await this.$api.proof.incrementList(hash);
            const preProof = results[0];
            if (!preProof.hash) {
            this.$toast('请先上链上一条存证');
            return Promise.reject('请先上链上一条存证');
            }
            this.preProof = preProof;
        } else {
            this.preProof = proof;
        }
        this.baseProof = proof;
        this.showAddProof = true;
        this.isZengliang = true;
    }
    private async hide(proof: any) {
        await this.$api.proof.deleteBlockChainProof(proof.id);
        this.$toast('隐藏成功');
        this.resetList();
    }
}
