<template>
    <div>
        <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList">
        </QueryListPage>
    </div>
</template>

<script>
    import { getCommodityList, delCommodity, setCommodityStatus } from '@/api/management/goods';
    import { UrlConfig } from '@/router/index';
    import { listPublishMap } from '@/api/jmap/map';

    export default {
        name: 'Dictionary',
        data() {
            return {
                productTypeList: [],
                EffectiveTypeList: [],
                pagerConfig: {
                    pageSize: 'pageSize',
                    pageIndex: 'pageNum'
                },
                queryForm: {
                    labelWidth: '80px',
                    reset: true,
                    queryObject: {
                        name: {
                            type: 'text',
                            label: '名称'
                        },
                        productType: {
                            type: 'select',
                            label: '产品类型',
                            config: {
                                data: []
                            }
                        },
                        mapId: {
                            type: 'select',
                            label: '地图',
                            config: {
                                data: []
                            }
                        },
                        status: {
                            type: 'select',
                            label: '状态',
                            config: {
                                data: this.$ConstSelect.Status
                            }
                        }
                    }
                },
                queryList: {
                    query: getCommodityList,
                    selectCheckShow: false,
                    indexShow: true,
                    columns: [
                        {
                            title: '商品名称',
                            prop: 'name'
                        },
                        {
                            title: '产品类型',
                            prop: 'productType',
                            type: 'tag',
                            columnValue: (row) => { return this.$convertField(row.productType, this.productTypeList, ['value', 'label']) },
                            tagType: (row) => { return 'success' }
                        },
                        {
                            title: '地图名称',
                            prop: 'mapName',
                        },
                        {
                            title: '产品名称',
                            prop: 'prdName',
                        },
                        {
                            title: '课程名称',
                            prop: 'lessonName',
                        },
                        {
                            title: '价格',
                            prop: 'price'
                        },
                        {
                            title: '状态',
                            prop: 'status',
                            type: 'tag',
                            columnValue: (row) => { return this.$convertField(row.status, this.EffectiveTypeList, ['value', 'label']) },
                            tagType: (row) => {
                                switch (row.status) {
                                    case '1': return 'success';
                                    default: return 'danger';
                                }
                            }
                        },
                        {
                            title: '描述',
                            prop: 'remarks'
                        },
                        {
                            title: '创建时间',
                            prop: 'createTime'
                        },
                        {
                            type: 'button',
                            title: '操作',
                            width: '250',
                            buttons: [
                                {
                                    name: '编辑',
                                    type: 'primary',
                                    handleClick: this.handleEdit
                                },
                                {
                                    name: '设置失效',
                                    handleClick: this.handleEfficacy,
                                    type: 'warning',
                                    showControl: (row) => {
                                        return row.status == '1';
                                    }
                                },
                                {
                                    name: '设置有效',
                                    type: 'primary',
                                    handleClick: this.handleEfficacy,
                                    showControl: (row) => {
                                        return row.status == '0';
                                    }
                                },
                                {
                                    name: '删除',
                                    type: 'danger',
                                    handleClick: this.handleDelete,
                                },
                            ]
                        }
                    ],
                    actions: [
                        { text: '新增', handler: this.handleAdd },
                    ]
                },

                currentModel: {}
            }
        },
        mounted() {
            this.loadInitData();
        },
        methods: {
            async loadInitData() {
                this.$Dictionary.effectiveType().then(list => {
                    this.$convertList(list, this.EffectiveTypeList, elem => {
                        return true;
                    });
                });
                // 产品类型
                this.$Dictionary.productType().then(list => {
                    list.forEach(elem => {
                        this.queryForm.queryObject.productType.config.data.push({ value: elem.code, label: elem.name });
                    });
                    this.$convertList(list, this.productTypeList, elem => {
                        return true;
                    });
                });
                try {
                    // 获取地图
                    let res = await listPublishMap();
                    res.data.forEach(elem => {
                        this.queryForm.queryObject.mapId.config.data.push({ value: elem.id, label: elem.name });
                    });
                } catch (error) {
                    console.error(error, '获取发布地图');
                }
            },
            handleEdit(index, row) {
                this.$router.push({ path: `${UrlConfig.orderauthor.commodityDraft}/edit/${row.id}` });
            },

            handleAdd() {
                this.$router.push({ path: `${UrlConfig.orderauthor.commodityDraft}/add/0` });
            },
            // 设置失效
            handleEfficacy(index, row) {
                this.$confirm('此操作将修改商品状态?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    setCommodityStatus(row.id).then(res => {
                        this.$message.success('操作成功');
                        this.reloadTable()
                    }).catch(err => {
                        this.$messageBox('操作失败');
                        this.reloadTable()
                    })
                }).catch(() => { })
            },
            handleDelete(index, row) {
                this.$confirm('此操作将删除该商品, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    delCommodity(row.id).then(response => {
                        this.$message.success('删除成功')
                        this.reloadTable()
                    }).catch(error => {
                        this.reloadTable()
                        this.$messageBox('删除失败')
                    })
                }).catch(() => { })
            },

            reloadTable() {
                this.queryList.reload()
            }
        }
    }
</script>