<template>
    <el-dialog title="批量指派投手" :visible.sync="visible" width="450px" append-to-body :show-close="false">
        <el-form :model="paramsForm" :rules="rulesType" ref="platFormPut1" 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="onClose()">取 消</el-button>
            <el-button type="primary" @click="onSubmit('platFormPut1')" :loading="loading">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
/**
 * 指派投手
 */
import { allUser } from "@/api/system/user";
import { putUser } from "@/api/accounts/ttAdapi";
export default {
    data() {
        return {
            rulesType: {
                putUserId: [
                    { required: true, message: '请选择投手', trigger: 'blur' }
                ]
            },
            paramsForm: {},
            userAll: [],
            loading: false
        }
    },
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        adAccountList: {
            type: Array,
            default: [],
        }
    },
    created() {
        this.getUserAllHandle()
    },
    methods: {
        onClose() {
            this.paramsForm = {};
            this.$emit("onClose");
        },
        onSubmit(val) {
            this.$refs[val].validate((valid) => {
                if (valid && this.adAccountList) {
                    this.loading = true
                    putUser({ putUserId: this.paramsForm.putUserId, adAccountList: this.adAccountList }).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>