<template>
    <div class="container">
        <el-form :model="formData" :rules="rules" ref="form" label-width="80px" label-position="right" size="small" style="max-width: 500px;">
            <!-- <el-form-item prop="storeId" label="店铺">
                <el-input v-model="formData.storeId" :disabled="'storeId'==subColumn"></el-input>
            </el-form-item> -->
            <el-form-item prop="serviceName" label="昵称">
                <el-input v-model="formData.serviceName" :disabled="'serviceName'==subColumn"></el-input>
            </el-form-item>
            <el-form-item prop="icon" label="头像">
                <single-upload v-model="formData.icon" :disabled="'icon'==subColumn"></single-upload>
                <span style='color:#999;fonts-size:12px;'>注：建议尺寸 180*180</span>
            </el-form-item>
            <el-form-item prop="username" label="账号名">
                <el-input v-model="formData.username" :disabled="'username'==subColumn" placeholder="英文字母加数字不能含有中文字符"></el-input>
            </el-form-item>
            <!-- <el-form-item prop="password" label="密码">
                <el-input v-model="formData.password" :disabled="'password'==subColumn"></el-input>
            </el-form-item> -->
            <el-form-item prop="useFlag" label="是否可用">
                <el-switch v-model="formData.useFlag" active-color="#13ce66" inactive-color="#ff4949" :disabled="'useFlag'==subColumn">
                </el-switch>
            </el-form-item>
            <!-- <el-form-item prop="statusFlag" label="状态">
                <el-input v-model="formData.statusFlag" :disabled="'statusFlag'==subColumn"></el-input>
            </el-form-item> -->
            <!-- <el-form-item prop="loginTime" label="登录时间">
                <template>
                    <div class="block">
                        <el-date-picker v-model="formData.loginTime" type="datetime" value-format="timestamp" placeholder="选择日期">
                        </el-date-picker>
                    </div>
                </template>
            </el-form-item> -->
            <el-form-item>
                <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
                <el-button @click="onDelete" v-if="formData.id" type="danger">删除</el-button>
                <el-button @click="$router.go(-1)">取消</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
import formValidator from '../formValidator'
import { mapState } from 'vuex'
export default {
    created() {
        this.formData.storeId = this.myStoreInfo.id
        if (this.$route.query.column) {
            this.subColumn = this.$route.query.column.split(',')[1];
            this.subValue = this.$route.query.column.split(',')[0];
        }

        if (this.$route.query.id) {
            this.$http.get({
                url: '/customerService/getOne',
                data: {
                    id: this.$route.query.id
                }
            }).then(res => {
                if (res.success) {


                    this.formData = res.data;

                    if (this.$route.query.column) {
                        this.formData[this.subColumn] = this.subValue;
                    }

                    this.formData.useFlag = this.formData.useFlag == 'Y' ? true : false;

                }
            })
        } else {
            if (this.$route.query.column) {
                this.formData[this.subColumn] = this.subValue;
            }
        }

    },
    data() {
        return {
            saving: false,
            formData: {
                useFlag: true,
            },
            rules: {
                serviceName: [
                    { required: true, message: '请输入客服名', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (!value) {
                                callback(new Error('请输入用户名'));
                            } else {
                                 callback();

                            }
                        }, trigger: 'blur'
                    }
                ],
                icon: [
                    { required: true, message: '请上传客服头像', trigger: 'blur' },
                ],
                username: [
                    { required: true, message: '请输入客服账号名', trigger: 'blur' },
                    {
                        validator: (rule, value, callback) => {
                            if (!value) {
                                callback(new Error('请输入用户名'));
                            } else if (!/^[a-zA-Z0-9_]{0,}$/.test(value)) {
                                callback(new Error('不能含有中文'));
                            }
                            else {
                                this.$http.get({
                                    url: '/customerService/getOne',
                                    data: {
                                        username: value
                                    }
                                }).then(res => {
                                    if (res.data) {
                                        if (res.data.id != this.formData.id) {
                                            callback(new Error('已存在该账号名'));
                                        }
                                        else {
                                            callback();
                                        }

                                    } else {
                                        callback();
                                    }
                                });

                            }
                        }, trigger: 'blur'
                    }
                ],
            },
            subColumn: '',
            subValue: '',
        }
    },
    computed: {
        ...mapState(['userInfo', 'myStoreInfo']),

    },
    methods: {
        onSave() {
            this.$refs.form.validate((valid) => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            var data = JSON.parse(JSON.stringify(this.formData));

            data.useFlag = this.formData.useFlag ? 'Y' : 'N';

            this.$http.post({
                url: this.formData.id ? '/customerService/update' : '/customerService/save',
                data: data
            }).then(res => {
                if (res.success) {
                    this.$message.success('成功');
                    this.$router.go(-1);
                } else {
                    this.$message.warning('失败')
                }
            });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' }).then(() => {
                return this.$http.post({
                    url: '/customerService/del',
                    data: { id: this.formData.id }
                })
            }).then(() => {
                this.$message.success('删除成功');
                this.$router.go(-1);
            }).catch(action => {
                if (action === 'cancel') {
                    this.$message.info('删除取消');
                } else {
                    this.$message.error('删除失败');
                }
            })
        },
    }
}
</script>
<style lang="less" scoped>
.container {
    background-color: #fff;
    border: 1px solid #ebebeb;
    padding: 20px;
    border-radius: 4px;
}
</style>
