<style lang="scss">
@import '@/styles/index.scss'; //导入样式

.assignprojectdlg {
    width: 100%;
    margin: auto;

    &__item {
        width: 100%;
        display: flex;

        &-left {
            flex: 1;
        }

        &-right {
            flex: none;
            width: 100px;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding-left: 15px;
            text-align: right;
        }
    }
}
</style>

<template>
    <div class="assignprojectdlg">
        <el-radio-group v-model="mainData.projectId">
            <el-radio :label="item.project_id" :key="index" v-for="(item, index) in tableData.listData.items">{{
                item.name
            }}</el-radio>
        </el-radio-group>
        <div style="padding-top: 10px;text-align:right">
            <el-button type="success" @click="hSubmit">确定</el-button>
            <el-button type="warning" @click="hClose">取消</el-button>
        </div>
    </div>
</template>

<script>
//为管理员分配项目
import { getProjectList } from "@/api/project"
import { getOperatorById, updateOperator } from "@/api/user"

export default {
    name: 'assignprojectdlg',
    props: {
        id: {//操作员ID
            type: Number,
            require: true,
            default: 0
        }
    },
    data: function () {
        return {
            tableData: {
                listData: {
                    items: [],
                }
            },
            mainData: {
                projectId: 0,//项目编号
            }
        }
    },
    watch: {
    },
    computed: {
        controlSize() {
            return this.$store.getters.controlSize
        }
    },
    mounted() {
        this.$nextTick(function () {
            this.init()
        })
    },
    methods: {
        init() {
            this.getProjectList()
            this.getOperatorById()
        },
        hSubmit() {
            this.updateOperator()
        },
        hClose() {//关闭对话框
            this.$emit("closeAssignProject")
        },
        getData() {
            this.$emit("getDataProject")
        },
        //获得项目资料
        getProjectList() {
            getProjectList().then((res) => {
                this.tableData.listData.items = res.data
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        //获得操作员信息
        getOperatorById() {
            let paramData = {}
            paramData.id = parseInt(this.id)
            getOperatorById(paramData).then((res) => {
                this.mainData.projectId = res.data.project_id
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
        //修改操作员
        updateOperator() {
            if (this.mainData.projectId <= 0) {
                this.$message({ message: "请重新打开窗口", type: 'error' })
                return
            }

            let data = {}
            data.id = parseInt(this.id)
            data.project_id = parseInt(this.mainData.projectId)
            updateOperator(data).then(() => {
                this.getData()
                this.$message.success('修改成功', '提示')
            }).catch((err) => {
                this.$message({ message: err, type: 'error' })
            })
        },
    }
}
</script>
