import { add } from 'date-fns';

export default {
    created() {
        this.getSetting(this.flag, this.isMode ? 'modeOptions' : 'tradingMethodOptions');
    },
    data() {
        return {
            flag: 24,
            isMode: false,
            tradingMethodOptions: [
                { label: '转让', value: 'TRANSFER' },
                { label: '普通许可', value: 'GENERAL_PERMIT' },
                { label: '独占许可', value: 'EXCLUSIVE_PERMIT' },
                { label: '排他许可', value: 'EXCLUSIVITY_PERMIT' },
                { label: '入股', value: 'INVEST' },
                { label: '其他', value: 'OTHER' }
            ],
            modeOptions: [
                { label: '技术转让', value: 'TRANSFER' },
                { label: '技术许可', value: 'PERMIT' },
                { label: '技术入股', value: 'INVEST' },
                { label: '合作生产', value: 'CO_PRODUCTION' },
                { label: '其他', value: 'OTHER' }
            ],
            statusOptions: [
                { label: '通过', value: 'PASS' },
                { label: '申请中', value: 'PENDING' },
                { label: '拒绝', value: 'DENY' }
            ],
            getSetting(flag = 1, keyName) {
                return this.$http.post('/setting/byFlag', { flag: flag }).then(res => {
                    let list = res.map(item => {
                        return {
                            label: item.name,
                            value: item.id
                        };
                    });
                    if (keyName) {
                        this[keyName] = list;
                    }
                    return Promise.resolve(list);
                });
            }
        };
    },
    methods: {
        tradingMethodFormatter(row, column, cellValue, index) {
            let selectedOption = this.tradingMethodOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        modeFormatter(row, column, cellValue, index) {
            let selectedOption = this.modeOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        statusFormatter(row, column, cellValue, index) {
            let selectedOption = this.statusOptions.find(i => i.value === cellValue);
            if (selectedOption) {
                return selectedOption.label;
            }
            return '';
        },
        audit(row, pass, name) {
            if (pass) {
                this.$set(row, 'loading', true);
                this.$http
                    .post('/' + name + '/audit', {
                        id: row.id,
                        pass: pass
                    })
                    .then(res => {
                        console.log(res);
                        this.$set(row, 'loading', false);
                        this.$message.success('OK');
                        this.getData();
                    })
                    .catch(e => {
                        console.log(e);
                        this.$set(row, 'loading', false);
                        this.$message.error(e.error);
                    });
            } else {
                this.$prompt('请输入理由', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                })
                    .then(({ value }) => {
                        this.$set(row, 'loading', true);
                        this.$http
                            .post('/' + name + '/audit', {
                                id: row.id,
                                remark: value,
                                pass: pass
                            })
                            .then(res => {
                                console.log(res);
                                this.$set(row, 'loading', false);
                                this.$message.success('OK');
                                this.getData();
                            })
                            .catch(e => {
                                console.log(e);
                                this.$set(row, 'loading', false);
                                this.$message.error(e.error);
                            });
                    })
                    .catch(() => {
                        this.$message({
                            type: 'info',
                            message: '取消输入'
                        });
                    });
            }
        }
    }
};
