<template>
  <div>
    <QueryListPage ref="queryListPage" :pager-config="pagerConfig" :query-form="queryForm" :query-list="queryList" />
    <deivce-add ref="add" @reloadTable="reloadTable" />
    <edit-config ref="editConfig" @reloadTable="reloadTable" />
  </div>
</template>

<script>
import { getProjectDeviceList, deleteProjectDevice } from '@/api/project';
import DeivceAdd from './add';
import EditConfig from './editConfig';
export default {
    name: 'DeviceManage',
    components: {
        DeivceAdd,
        EditConfig
    },
    data() {
        return {
            examResultList: [],
            pagerConfig: {
                pageSize: 'pageSize',
                pageIndex: 'pageNum'
            },
            deviceTypeList:[{label: '道岔', value: 'SWITCH'}, {label: '信号机', value:'SIGNAL'}, {label: '屏蔽门', value: 'PSD'}],
            projectList: [{label:'西铁院', value: 'XTY'}, {label: '贵州装备', value:'GZB'}],
            queryForm: {
                labelWidth: '120px',
                reset: true,
                queryObject: {
                    projectCode: {
                        type: 'select',
                        label: '项目编码',
                        config: {
                            data: [{label:'西铁院', value: 'XTY'}, {label: '贵州装备', value:'GZB'}]
                        }
                    },
                    code: {
                        type: 'text',
                        label: '设备编号'
                    },
                    type: {
                        type: 'select',
                        label: '设备类型',
                        config: {
                            data: [{label: '道岔', value: 'SWITCH'}, {label: '信号机', value:'SIGNAL'}, {label: '屏蔽门', value: 'PSD'}]
                        }
                    }
                }

            },
            queryList: {
                query: getProjectDeviceList,
                selectCheckShow: false,
                indexShow: true,
                columns: [
                    {
                        title: '设备编号',
                        prop: 'code'
                    },
                    {
                        title: '项目名称',
                        prop: 'projectCode',
                        type: 'tag',
                        columnValue: (row) => { return this.$convertField(row.projectCode, this.projectList, ['value', 'label']); },
                        tagType: (row) => { return 'success'; }
                    },
                    {
                        title: '设备类型',
                        prop: 'type',
                        type: 'tag',
                        columnValue: (row) => { return this.$convertField(row.type, this.deviceTypeList, ['value', 'label']); },
                        tagType: (row) => { return 'success'; }
                    },
                    // {
                    //     title: this.$t('system.trainingUseTime'),
                    //     prop: 'duration',
                    //     type: 'tag',
                    //     columnValue: (row) => { return this.computation(row.duration); },
                    //     tagType: (row) => { return 'success'; }
                    // },
                    // {
                    //     title: '网关唯一编码',
                    //     prop: 'config',
                    //     columnValue: (row) => { return row.config; }
                    // },
                    {
                        title: '创建时间',
                        prop: 'createTime',
                        type: 'tag',
                        columnValue: (row) => { return this.handleTime(row.createTime); },
                        tagType: (row) => { return 'success'; }
                    },
                    // {
                    //     title: '创建人',
                    //     prop: 'creator'
                    // },
                    {
                        type: 'button',
                        title: this.$t('global.operate'),
                        width: '250',
                        buttons: [
                            {
                                name: '编辑配置',
                                handleClick: this.editConfig
                            },
                            {
                                name: this.$t('global.delete'),
                                handleClick: this.handleDelete,
                                type: 'danger'
                            }
                        ]
                    }
                ],
                actions: [
                    { text: this.$t('global.add'), handler: this.createProjectDevice}
                ]
            },
            currentModel: {}
        };
    },
    created() {
    },
    methods: {
        createProjectDevice() {
            this.$refs.add.show();
        },
        computation(fieldValue, type) {
            let list = [];
            if (type === 'projectType') {
                list = this.projectList;
            } else if ( type === 'deviceType' ) {
                list = this.deviceTypeList;
            }
            let value = '';
            list.forEach((elem) => {
                elem.label = fieldValue;
                value = elem.value;
            });
            console.log(value, fieldValue, type);
            return value;
        },
        handleTime(time) {
            const timeList = time.split('T');
            let newTime = '';
            timeList.forEach(elem => {
                newTime += elem;
            });
            return newTime;
        },
        // 删除
        handleDelete(index, row) {
            this.$confirm('此操作将删除该项目设备！', this.$t('global.tips'), {
                confirmButtonText: this.$t('global.confirm'),
                cancelButtonText: this.$t('global.cancel'),
                type: 'warning'
            }).then(() => {
                deleteProjectDevice(row.id).then(response => {
                    this.$message.success(this.$t('system.deleteSuccess'));
                    this.reloadTable();
                }).catch(() => {
                    this.reloadTable();
                    this.$messageBox(this.$t('error.deleteFailed'));
                });
            });
        },
        editConfig(index, row) {
            this.$refs.editConfig.doShow(row);
        },
        reloadTable() {
            this.queryList.reload();
        }
    }
};
</script>
