<template>
    <div class="add-edit-dialog-container">
        <el-dialog
            :title="addEditDialogTitle"
            :visible.sync="addEditDialogVisible"
            width="40%"
            :before-close="close"
        >
            <el-form
                :model="addEditDialogForm"
                ref="addEditDialogForm"
                :rules="addEditDialogRules"
                label-width="90px"
            >
                <el-form-item label="品牌名称:" prop="name" class="classify-input">
                    <el-input
                        placeholder="请输入中文名称"
                        v-model="addEditDialogForm.name"
                        class="file-input"
                        maxlength="20"
                    ></el-input>
                </el-form-item>
                <el-form-item label="编码:" prop="code">
                    <el-input v-model="addEditDialogForm.code" class="sort-input" maxlength="20"></el-input>
                </el-form-item>
                <el-form-item label="英文名:" prop="ename" class="classify-input">
                    <el-input
                        placeholder="请输入英文名称"
                        v-model="addEditDialogForm.ename"
                        class="file-input"
                        maxlength="20"
                    ></el-input>
                </el-form-item>
                <el-form-item label="排序:" prop="sort">
                    <el-input placeholder="请排序" v-model="addEditDialogForm.sort" class="sort-input" maxlength="9"></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"
                        autosize
                        maxlength="300"
                    ></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 Upload from '@/apps/mobile/components/uploadAuto/index';
import { addLifeBrand, updateLifeBrandStatus } from '@/apps/mobile/api/databank/index';
import { staticUrl } from '@/config.js';
import { validPositiveInteger, validChinese, validEnglish } from '@/apps/mobile/utils/validate';
export default {
    components: {
        Upload,
    },
    data() {
        // 排序校验
        var checkSort = (rule, value, callback) => {
            if (value && !validPositiveInteger(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        // 验证中文
        var checkChinese = (rule, value, callback) => {
            if (value && !validChinese(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        // 验证英文
        var checkEnglish = (rule, value, callback) => {
            if (value && !validEnglish(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        return {
            addEditDialogVisible: false,
            addEditDialogTitle: '新增',
            addEditDialogForm: {},
            submitBtndisabled: false,
            addEditDialogRules: {
                name: [
                    {
                        required: true,
                        message: '请填写中文品牌名称',
                        trigger: 'blur',
                    },
                    { max: 20, message: '长度最大20个字符', trigger: 'blur' },
                    { validator: checkChinese, message: '请填写包含中文的品牌名称', trigger: 'blur' },
                ],
                ename: [
                    {
                        required: true,
                        message: '请填写英文品牌名称',
                        trigger: 'blur',
                    },
                    { max: 20, message: '长度最大20个字符', trigger: 'blur' },
                    { validator: checkEnglish, message: '请填写英文的品牌名称', trigger: 'blur' },
                ],
                sort: [
                    {
                        required: true,
                        message: '请输入排序',
                        trigger: 'blur',
                    },
                    { validator: checkSort, message: '请输入正整数', trigger: 'blur' },
                ],
                file: [
                    {
                        required: true,
                        message: '请上传图片',
                        trigger: 'blur',
                    },
                ],
                descr: [
                    {
                        required: true,
                        message: '请填写描述',
                        trigger: 'blur',
                    },
                    { max: 300, message: '长度最大300个字符', trigger: 'blur' },
                ],
                statuz: [
                    {
                        required: true,
                        message: '请选择状态',
                        trigger: 'change',
                    },
                ],
            },
            fileList: [],
            maximumSize: 3,
            type: null,
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    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);
                    delete formData.file;
                    formData.logo = files.join(',');
                    if (this.type === 'add') {
                        console.log(formData);
                        addLifeBrand(formData)
                            .then(res => {
                                if (res.statusCode === '1') {
                                    this.completeOperation(res.message);
                                } else {
                                    this.$message.warning(res.message);
                                }
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                    if (this.type === 'edit') {
                        console.log(formData);
                        console.log(this.fileList);
                        let data = {
                            id: formData.id,
                            descr: formData.descr,
                            code: formData.code,
                            ename: formData.ename,
                            logo: formData.logo,
                            name: formData.name,
                            sort: formData.sort,
                            statuz: formData.statuz,
                        };
                        updateLifeBrandStatus(data)
                            .then(res => {
                                if (res.statusCode === '1') {
                                    this.completeOperation(res.message);
                                } else {
                                    this.$message.warning(res.message);
                                }
                            })
                            .catch(() => {
                                this.submitBtndisabled = false;
                            });
                    }
                } else {
                    return false;
                }
            });
        },
        validateUpload(data) {
            this.changeUploadFile(data.fileList);
            console.log(data);
            this.addEditDialogForm.file = data.validateValues;
            this.$refs['addEditDialogForm'].validateField('file');
        },
        changeUploadFile(fileList) {
            console.log(fileList);
            this.fileList = fileList;
        },
        completeOperation(message) {
            this.$message({
                type: 'success',
                message,
            });
            this.close();
            this.submitBtndisabled = false;
            this.$emit('queryListData');
        },
        open(type, data) {
            this.type = type;
            console.log(data, 'data');

            if (type === 'edit') {
                this.addEditDialogTitle = '编辑';
                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;
        },
    },
    watch: {
        fileList(val) {
            this.$nextTick(() => {
                this.$refs.upload.changeHideUpload();
            });
        },
    },
};
</script>
<style lang="stylus" scoped>
.add-edit-dialog-container {
    .file-input {
        width: 200px;
    }

    .sort-input {
        width: 120px;
    }

    .upload {
        display: inline-block;
    }

    ::v-deep .el-input__inner {
        width: auto;
    }

    ::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;
    }

    .el-textarea {
        width: 80%;
    }

    ::v-deep .el-textarea__inner {
        min-height: 80px !important;
    }
}
</style>
