<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="name">
                    <el-input placeholder="请输入" v-model="addEditDialogForm.name" class="file-input" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="logo:" prop="file">
                    <p class="color666">图片大小不能超过3MB</p>
                    <Upload
                        :fileList="fileList"
                        @changeUploadFile="changeUploadFile"
                        @validateUpload="validateUpload"
                        :limitUpload="1"
                        :maximumSize="maximumSize"
                        :uploadType="1"
                        ref="upload"
                    ></Upload>
                </el-form-item>
                <el-form-item label="描述:" prop="descr">
                    <el-input
                        type="textarea"
                        placeholder="请输入内容"
                        v-model="addEditDialogForm.descr"
                        maxlength="300"
                    ></el-input>
                </el-form-item>
                <el-form-item label="排序:" prop="sort">
                    <el-input placeholder="请排序" v-model="addEditDialogForm.sort" maxlength="9"></el-input>
                </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 { validPositiveInteger } from '@/apps/mobile/utils/validate';
import Upload from '@/apps/mobile/components/uploadAuto/index';
import { addOperatingSystem, updateOperatingSystemStatus } from '@/apps/mobile/api/databank/index';
export default {
    components: {
        Upload,
    },
    data() {
        // 排序校验
        var checkSort = (rule, value, callback) => {
            if (value && !validPositiveInteger(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        return {
            addEditDialogVisible: false,
            addEditDialogTitle: '新增',
            addEditDialogForm: {},
            addEditDialogRules: {
                name: [
                    {
                        required: true,
                        message: '请填写系统名称',
                        trigger: 'blur',
                    },
                    { max: 20, message: '长度最大20个字符', trigger: 'blur' },
                ],
                file: [
                    {
                        required: true,
                        message: '请上传图片',
                        trigger: 'blur',
                    },
                ],
                descr: [
                    {
                        required: true,
                        message: '请填写描述',
                        trigger: 'blur',
                    },
                    { max: 500, message: '长度最大500个字符', trigger: 'blur' },
                ],
                sort: [
                    {
                        required: true,
                        message: '请填写排序',
                        trigger: 'blur',
                    },
                    { validator: checkSort, message: '请输入正整数', trigger: 'blur' },
                ],
                statuz: [
                    {
                        required: true,
                        message: '请填写排序',
                        trigger: 'change',
                    },
                ],
            },
            submitBtndisabled: false,
            type: null,
            fileList: [],
            maximumSize: 3,
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.submitBtndisabled = true
                    let formData = { ...this.addEditDialogForm };
                    console.log(this.fileList);
                    let files = this.fileList.map(item => {
                        if (item.response) {
                            return item.response.data.pathUrl;
                        } else {
                            return item.pathUrl;
                        }
                    });
                    console.log(files);
                    formData.logo = files.join(',');
                    if (this.type === 'add') {
                        console.log(formData);
                        let message = '新增成功';
                        addOperatingSystem(formData)
                            .then(res => {
                                console.log(res);
                                this.completeOperation(message);
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                    if (this.type === 'edit') {
                        console.log(formData);
                        console.log(this.fileList);
                        let message = '修改成功';
                        let data = {
                            id: formData.id,
                            name: formData.name,
                            logo: formData.logo,
                            descr: formData.descr,
                            sort: formData.sort,
                            statuz: formData.statuz,
                        };
                        updateOperatingSystemStatus(data)
                            .then(res => {
                                console.log(res);
                                this.completeOperation(message);
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                } else {
                    return false;
                }
            });
        },
        open(type, data) {
            this.type = type;
            if (type === 'edit') {
                this.addEditDialogTitle = '编辑';
                console.log(data);
                if (data.logo) {
                    this.fileList = [
                        {
                            url: data.logo,
                            // hasBeenUploaded: true,
                            pathUrl: data.logo,
                        },
                    ];
                    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;
        },
        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');
        },
        validateUpload(data) {
            console.log(data);
            this.changeUploadFile(data.fileList);
            this.addEditDialogForm.file = data.validateValues;
            this.$refs['addEditDialogForm'].validateField('file');
        },
        changeUploadFile(fileList) {
            console.log(fileList);
            this.fileList = fileList;
        },
    },
    watch: {
        fileList(val) {
            this.$nextTick(() => {
                this.$refs.upload.changeHideUpload();
            });
        },
    },
};
</script>
<style lang="stylus" scoped>
.add-edit-dialog-container {
    ::v-deep .el-input__inner {
        width: 200px;
    }

    ::v-deep .avatar-uploader .el-upload {
        border: 1px dashed #d9d9d9;
        border-radius: 6px;
        cursor: pointer;
        position: relative;
        overflow: hidden;
    }

    ::v-deep .avatar-uploader .el-upload:hover {
        border-color: #409EFF;
    }

    ::v-deep .avatar-uploader-icon {
        font-size: 28px;
        color: #8c939d;
        width: 80px;
        height: 80px;
        line-height: 80px;
        text-align: center;
    }

    ::v-deep .avatar {
        width: 80px;
        height: 80px;
        display: block;
    }
}
</style>
