<template>
    <div class="add-edit-dialog-container">
        <el-dialog
            :title="addEditDialogTitle"
            :visible.sync="addEditDialogVisible"
            width="600px"
            :before-close="close"
        >
            <el-form
                :model="addEditDialogForm"
                ref="addEditDialogForm"
                :rules="addEditDialogRules"
                label-width="90px"
            >
                <el-form-item label="系统名称:" prop="dataId">
                    <el-select v-model="addEditDialogForm.dataId" placeholder="请选择">
                        <el-option
                            v-for="item in options"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="文件名称:" prop="name" class="classify-input">
                    <el-input placeholder="请输入" v-model="addEditDialogForm.name" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="文件:" prop="file">
                    <el-upload
                        ref="upload"
                        :action="uploadUrl"
                        :on-remove="handlePdfRemove"
                        :on-success="handlePdfSuccess"
                        :on-exceed="handlePdfExceed"
                        :before-upload="beforeUpload"
                        :file-list="fileList"
                        :multiple="false"
                        :limit="1"
                        accept="application/pdf"
                    >
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="启用状态:" prop="statuz">
                    <el-switch
                        v-model="addEditDialogForm.statuz"
                        active-text="开"
                        inactive-text="关"
                        :active-value="1"
                        :inactive-value="0"
                    ></el-switch>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="close()">取 消</el-button>
                <el-button
                    type="primary"
                    @click="submitForm('addEditDialogForm')"
                    :disabled="submitBtndisabled"
                >确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
import { uploadUrl, staticUrl } from '@/config.js';
import { addLifeFiles, updateLifeFilesStatus, queryAllOperatingSystem } from '@/apps/mobile/api/databank/index';
export default {
    data() {
        return {
            addEditDialogVisible: false,
            addEditDialogTitle: '新增',
            addEditDialogForm: {},
            addEditDialogRules: {
                dataId: [
                    {
                        required: true,
                        message: '请选择系统名称',
                        trigger: 'change',
                    },
                ],
                // controName: [
                //     {
                //         required: true,
                //         message: '请选择系统名称',
                //         trigger: 'change',
                //     },
                // ],
                name: [
                    {
                        required: true,
                        message: '请填写文件名称',
                        trigger: 'blur',
                    },
                    { max: 20, message: '长度最大20个字符', trigger: 'blur' },
                ],
                file: [
                    {
                        required: true,
                        message: '请上传文件',
                        trigger: 'blur',
                    },
                ],
                statuz: [
                    {
                        required: true,
                        message: '请上传文件',
                        trigger: 'change',
                    },
                ],
            },
            submitBtndisabled: false,
            type: null,
            fileList: [],
            maximumSize: 3,
            uploadUrl: uploadUrl,
            options: [],
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    let formData = {
                        ...this.addEditDialogForm,
                        dataTable: 3,
                    };
                    if (this.type === 'add') {
                        console.log(formData);
                        let message = '新增成功';
                        addLifeFiles(formData)
                            .then(res => {
                                console.log(res);
                                this.completeOperation(message);
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                    if (this.type === 'edit') {
                        console.log(formData);
                        let message = '修改成功';
                        let data = {
                            id: formData.id,
                            dataId: formData.dataId,
                            name: formData.name,
                            url: formData.url,
                            statuz: formData.statuz,
                            dataTable: 3,
                        };
                        console.log(data);
                        updateLifeFilesStatus(data)
                            .then(res => {
                                console.log(res);
                                this.completeOperation(message);
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                } else {
                    return false;
                }
            });
        },
        open(type, data) {
            this.type = type;
            this.getAllOperatingSystem();
            if (type === 'edit') {
                this.addEditDialogTitle = '编辑';
                console.log(data);
                if (data.url) {
                    this.fileList = [
                        {
                            name: data.url,
                            url: data.url,
                            hasBeenUploaded: true,
                            newName: data.url,
                        },
                    ];
                    this.addEditDialogForm = Object.assign({ file: true }, data);
                } else {
                    this.addEditDialogForm = Object.assign({ file: null }, data);
                }
            } else {
                this.addEditDialogTitle = '新增';
                this.addEditDialogForm = { statuz: 1, file: null };
            }
            this.addEditDialogVisible = true;
        },
        getAllOperatingSystem() {
            queryAllOperatingSystem()
                .then(res => {
                    this.options = res.data;
                })
                .catch();
        },
        close() {
            this.$refs.upload.clearFiles();
            this.fileList = [];
            this.$refs['addEditDialogForm'].resetFields();
            this.addEditDialogVisible = false;
        },
        completeOperation(message) {
            this.$message({
                type: 'success',
                message,
            });
            this.close();
            this.submitBtndisabled = false;
            this.$emit('queryListData');
        },
        handlePdfRemove() {
            this.addEditDialogForm.url = '';
            this.addEditDialogForm.file = null;
        },
        handlePdfSuccess(response) {
            console.log(response);
            this.addEditDialogForm.url = response.data.pathUrl;
            this.addEditDialogForm.file = true;
            this.$refs['addEditDialogForm'].validateField('file');
        },
        handlePdfExceed() {
            this.$message.warning('只能上传一个文件!');
        },
        beforeUpload(file) {
            console.log(file);
            let fileType = ['application/pdf'].indexOf(file.type);
            const isLt5M = file.size / 1024 / 1024 < 8;
            if (fileType === -1) {
                this.$message.warning('文件只能是pdf格式!');
            }
            if (!isLt5M) {
                this.$message.warning('文件大小不能超过8MB!');
            }
            return fileType !== -1 && isLt5M;
        },
    },
};
</script>
<style lang="stylus" scoped>
.add-edit-dialog-container {
    ::v-deep .el-input__inner {
        width: 200px;
    }

    .file-input {
        width: auto;
    }

    .upload {
        display: inline-block;
    }
}
</style>
