<template>
    <el-dialog v-model="visible" title="奖励分配人员" width="680" @closed="$emit('closed')">
        <el-form ref="formRef" :model="form" :rules="rules" :disabled="disabled" label-width="110px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="奖励标准">
						<el-input-number v-model="form.rewardScore" readonly :precision="2" :controls="false"></el-input-number>
                    </el-form-item>
                </el-col>

                <el-col :span="24">
                    <el-form-item label="参与人" prop="partPerson">
                        <el-table header-cell-class-name="tjm_card_table_header" :data="form.partPerson" border>
			                <el-table-column label="姓名" prop="name"></el-table-column>
			                <el-table-column label="奖励分配" prop="reward">
                                <template #default="scope">
                                    <el-input-number v-model="scope.row.reward" :min="0" :max="form.rewardScore" :step="0.1" :precision="2" :controls="false"></el-input-number>
                                </template>
                            </el-table-column>
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>

        <template #footer>
            <el-button :loading="isSaving" type="primary" @click="submit">确 定</el-button>
        </template>
    </el-dialog>
</template>

<script>
import API from "@/api/policy/strive";

export default {
    emits: ["success", "closed"],
    props: {
        disabled: { type: Boolean, default: false }
    },

    data() {
        return {
            visible: false,
            isSaving: false,

            form: {
                id: null,
                rewardScore: null,
                partPersonArr: "[]",
                partPerson: []
            },

            rules: {
                partPerson: [{ required: true, message: "请分配奖励" },
                    { validator: (rule, value, callback) => {
                        if (value.findIndex(v => !v.reward) != -1) return callback(new Error("请分配奖励"));
                        callback();
                    }}
                ],
            }
        }
    },

    methods: {
        open() {
            this.visible = true;
            return this;
        },

        setData(data) {
            for (const key in this.form) {
                this.form[key] = data[key] || null;
            }

            this.form.partPerson = data["partPersonArr"] && JSON.parse(data["partPersonArr"]) || [];
        },

        // 表单提交方法
        submit() {
            this.$refs.formRef.validate(valid => {
                if (valid) {
                    this.isSaving = true;
                    API.updateById({ ...this.form, isReward: 1, partPersonArr: JSON.stringify(this.form.partPerson) }).then(res => {
                        this.isSaving = false;
                        if (res.code === 200) {
                            ElMessage.success("操作成功");
                            this.visible = false;
                            this.$emit("success");
                        } else ElMessage.error(res.msg);
                    }).catch(() => this.isSaving = false);
                } else {
                    return false;
                }
            });
        }
    }
}
</script>

<style lang="scss" scoped>
.el-input-number {
  width: 100%;

  :deep(.el-input) .el-input__wrapper {
    padding: 1px 11px;

    .el-input__inner {
      text-align: unset;
    }
  }
}
</style>