<template>
    <el-button size="mini" type="text" icon="el-icon-sort" @click="putHangdle()">
        <span>指派</span>
        <el-dialog title="指派投手" :visible.sync="visible" width="450px" append-to-body>
            <el-form :model="paramsForm" :rules="rulesType" ref="platFormPut" label-width="80px" label-position="top">
                <el-form-item label="用户" prop="putUserId">
                    <el-select v-model="paramsForm.putUserId" clearable placeholder="请选择" filterable style="width: 100%">
                        <el-option v-for="item in userAll" :key="item.id" :label="item.nickName" :value="item.id" />
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="paramsForm = {}; visible = false">取 消</el-button>
                <el-button type="primary" @click="onSubmit('platFormPut')" :loading="loading">确 定</el-button>
            </span>
        </el-dialog>
    </el-button>
</template>
<script>
/**
 * 指派投手
 */
import { allUser } from "@/api/system/user";
import { putUser } from "@/api/accounts/ttAdapi";
export default {
    data() {
        return {
            visible: false,
            rulesType: {
                putUserId: [
                    { required: true, message: '请选择投手', trigger: 'blur' }
                ]
            },
            paramsForm: {},
            userAll: [],
            loading: false
        }
    },
    props: {
        accountId: {
            type: Number,
            default: null,
        },
        authUserId: {
            type: Number,
            default: null,
        },
        putUserId: {
            type: Number,
            default: null,
        }
    },
    methods: {
        putHangdle() {
            this.getUserAllHandle()
            if(this.putUserId && this.putUserId !== -1) {
                this.$set(this.paramsForm, 'putUserId', this.putUserId)
            }
            this.visible = true
        },
        onSubmit(val) {
            this.$refs[val].validate((valid) => {
                if (valid && this.accountId && this.authUserId) {
                    this.loading = true
                    putUser({putUserId: this.paramsForm.putUserId, adAccountList: [{ authUserId: this.authUserId, accountId: this.accountId }] }).then(res => {
                        this.loading = false
                        if (res.data) {
                            this.$message({
                                showClose: true,
                                message: '指派成功',
                                type: 'success'
                            });
                            this.$emit("onChange");
                            this.visible = false
                        }
                    }).catch(() => this.loading = false)
                }
            })
        },
        getUserAllHandle() {
            // 所有用户
            allUser().then(response => {
                let data = response.data
                this.userAll = data.map((item) => {
                    return { id: item.userId, nickName: item.nickname }
                })
            })
        }
    }
}
</script>