<template>
    <div class='container'>
        <div class='handle-box'>
            <el-form :inline='true'>
                <el-form-item>
                    <el-input
                        v-model='searchForm.title'
                        placeholder='请输入故障标题'
                        clearable>
                    </el-input>
                </el-form-item>

                <el-form-item>
                    <el-button @click='loadData'>搜索</el-button>
                </el-form-item>

                <el-form-item>
                    <el-button type='primary' @click='dialogVisible = true' v-if="hasAuth('sys:user:save')">新增
                    </el-button>
                    <el-button type='primary' @click='showQRCode'>生成WIFI二维码</el-button>
                </el-form-item>
            </el-form>
        </div>

        <el-table
            ref='noticeTable'
            :data='tableData'
            tooltip-effect='dark'
            style='width: 100%'
            border
            stripe>

            <el-table-column
                type='index'
                label='序号'
                width='50'>
            </el-table-column>

            <el-table-column
                prop='title'
                label='显示名称'
                :show-overflow-tooltip='true'>
            </el-table-column>

            <el-table-column
                prop='sourceName'
                width='240'
                label='文件名称'>
                <template scope='scope'>
                    <el-popover
                        placement='top-start'
                        title=''
                        trigger='hover'>
                        <img style='width: 300px;height: 300px;' :src='scope.row.filePath'
                             :alt=" scope.row.sourceName ? scope.row.sourceName : '无照片' "
                             class='img-class'>
                        <el-link slot='reference'>{{ scope.row.sourceName|ellipsis }}</el-link>
                    </el-popover>
                </template>
            </el-table-column>

            <el-table-column
                prop='status'
                align='center'
                label='启用状态'>
                <template slot-scope='scope'>
                    <el-tag size='small' v-if='scope.row.status == 10' type='success'>启用</el-tag>
                    <el-tag size='small' v-else-if='scope.row.status == 20' type='danger'>禁用</el-tag>
                </template>
            </el-table-column>

            <el-table-column
                prop='createTime'
                width='150'
                label='创建时间'>
            </el-table-column>

            <el-table-column
                prop='updateTime'
                width='150'
                label='修改时间'>
            </el-table-column>

            <el-table-column
                prop='icon'
                width='180px'
                fixed='right'
                label='操作'>
                <template slot-scope='scope'>
                    <el-button type='text' @click='editHandle(scope.row)'>编辑</el-button>
                    <el-divider direction='vertical'></el-divider>
                    <template>
                        <el-popconfirm title='确定删除这条数据吗？' @confirm='delHandle(scope.row.id)'>
                            <el-button type='text' slot='reference'>删除</el-button>
                        </el-popconfirm>
                    </template>
                </template>
            </el-table-column>

        </el-table>

        <el-pagination
            @size-change='handleSizeChange'
            @current-change='handleCurrentChange'
            layout='total, sizes, prev, pager, next, jumper'
            :page-sizes='[10, 20, 50, 100]'
            :current-page='current'
            :page-size='size'
            :total='total'>
        </el-pagination>


        <!--新增对话框-->
        <el-dialog
            title='提示'
            :visible.sync='dialogVisible'
            width='60%'
            :before-close='handleClose'>

            <el-form :model='editForm' :rules='editFormRules' ref='editForm' label-width='100px'>
                <el-row>
                    <el-col :span='12'>
                        <el-form-item label='显示名称:' prop='title'>
                            <el-input v-model='editForm.title' autocomplete='off' placeholder='请输入显示名称'></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span='12'>
                        <el-form-item label='启用状态:' prop='status'>
                            <el-select v-model='editForm.status' placeholder='请选择启用状态'>
                                <el-option
                                    v-for='item in statusList'
                                    :key='item.value'
                                    :label='item.label'
                                    :value='item.value'>
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span='24'>
                        <el-form-item label='二维码文件:' prop='uploadRecordId'>
                            <el-upload
                                ref='qrcodeUpload'
                                class='upload-demo'
                                drag
                                :data='{fileType:30}'
                                :auto-upload='true'
                                :limit='1'
                                :headers='headers'
                                :on-success='uploadSuccess'
                                :file-list='editForm.file'
                                :on-exceed='exceed'
                                :action="$axios.defaults.baseURL+'/api/upload/record/upload'"
                            >
                                <i class='el-icon-upload'></i>
                                <div class='el-upload__text'>将文件拖到此处，或<em>点击上传</em></div>
                                <div class='el-upload__tip' slot='tip'>只能上传jpg/png文件，且不超过500kb</div>
                            </el-upload>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot='footer' class='dialog-footer'>
                <el-button @click="resetForm('editForm')">取 消</el-button>
                <el-button type='primary' @click="submitForm('editForm')">确 定</el-button>
            </div>
        </el-dialog>
        <QrCode ref='qrCode'></QrCode>
    </div>
</template>

<script>
import QrCode from './components/qrCode';

export default {
    name: 'qrcodeShowConfig',
    components: { QrCode },
    filters: {
        ellipsis(value) {
            if (!value) return '';
            if (value.length > 30) {
                return value.slice(0, 30) + '...';
            }
            return value;
        }
    },
    data() {
        let checkFile = (rule, value, callback) => {
            if (this.editForm.uploadRecordId && this.editForm.uploadRecordId > 0) {
                callback();
            } else {
                return callback(new Error('请上传二维码文件'));
            }
        };
        return {
            headers: { Authorization: localStorage.getItem('token') },
            searchForm: {
                title: ''
            },
            delBtlStatu: true,
            total: 0,
            size: 10,
            current: 1,
            dialogVisible: false,
            editForm: {
                title: '',
                status: '',
                uploadRecordId: '',
                file: []
            },
            statusList: [{
                label: '启用',
                value: 10
            }, {
                label: '禁用',
                value: 20
            }],
            tableData: [],
            editFormRules: {
                title: [
                    { required: true, message: '请输入显示名称', trigger: 'blur' }
                ],
                status: [
                    { required: true, message: '请选择启用状态', trigger: 'blur' }
                ],
                uploadRecordId: [
                    { required: true, validator: checkFile, trigger: 'blur' }
                ]
            }
        };
    },
    created() {
        this.loadData();
    },
    methods: {
        loadData() {
            this.$axios.get('/api/qrcode/show/config/list', {
                params: {
                    title: this.searchForm.title,
                    current: this.current,
                    size: this.size
                }
            }).then(res => {
                this.tableData = res.data.data.records;
                this.size = res.data.data.size;
                this.current = res.data.data.current;
                this.total = res.data.data.total;
            });
        },
        handleClose() {
            this.resetForm('editForm');
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
            this.editForm = {};
            this.$refs['qrcodeUpload'].uploadFiles.length = 0;
        },
        submitForm(formName) {
            let _this = this;
            _this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.$axios.post('/api/qrcode/show/config/' + (this.editForm.id ? 'updateBean' : 'addBean'), _this.editForm)
                        .then(res => {
                            _this.loadData();
                            _this.$notify({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success'
                            });
                            _this.resetForm(formName);
                        });
                } else {
                    return false;
                }
            });
        },
        editHandle(row) {
            this.editForm = JSON.parse(JSON.stringify(row));
            this.editForm.file = [{ name: this.editForm.sourceName, url: this.editForm.filePath }];
            this.dialogVisible = true;
        },
        delHandle(id) {
            this.$axios.post('/api/qrcode/show/config/deleteById?id=' + id).then(res => {
                this.$notify({
                    showClose: true,
                    message: '恭喜你，操作成功',
                    type: 'success'
                });
                this.loadData();
            });
        },
        handleSizeChange(val) {
            this.size = val;
            this.loadData();
        },
        handleCurrentChange(val) {
            this.current = val;
            this.loadData();
        },
        uploadSuccess(response, file, fileList) {
            //回写上传记录id
            this.editForm.uploadRecordId = response.data.id;
        },
        exceed(files, fileList) {
            this.$notify({
                showClose: true,
                message: '只能上传一个文件，如果是修改请先移除原来的文件',
                type: 'error'
            });
        },
        showQRCode() {
            this.$refs['qrCode'].show(null, 'WIFI');
        }
    }
};
</script>

<style scoped>

</style>