<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.landAmount" readonly :precision="2" :controls="false"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="考核基数">
						<el-input-number v-model="form.rewardRadix" readonly :controls="false"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="实施难易程度" prop="implementScore">
                        <el-input-number v-model="form.implementScore" :min="0" :max="1" :step="0.1" :precision="2" :controls="false" placeholder="实施难易程度" @change="calcWeight"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="复制推广潜力" prop="promotionScore">
                        <el-input-number v-model="form.promotionScore" :min="0" :max="1" :step="0.1" :precision="2" :controls="false" placeholder="复制推广潜力" @change="calcWeight"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="政策争取效果" prop="striveScore">
                        <el-input-number v-model="form.striveScore" :min="0" :max="1" :step="0.1" :precision="2" :controls="false" placeholder="政策争取效果" @change="calcWeight"></el-input-number>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="奖励标准" prop="rewardScore">
						<el-input-number v-model="form.rewardScore" readonly :controls="false"></el-input-number>
                    </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,
                landAmount: null,
                rewardRadix: null,
                implementScore: null,
                promotionScore: null,
                striveScore: null,
                rewardScore: null
            },

            rules: {
                implementScore: [{ required: true, message: "请输入分值" }],
                promotionScore: [{ required: true, message: "请输入分值" }],
                striveScore: [{ required: true, message: "请输入分值" }]
            }
        }
    },

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

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

            this.calcWeight();
        },

        calcWeight() {
            API.calculate({
                rewardRadix: !!this.form.rewardRadix && this.form.rewardRadix || 0,
                implementScore: !!this.form.implementScore && this.form.implementScore || 0,
                promotionScore: !!this.form.promotionScore && this.form.promotionScore || 0,
                striveScore: !!this.form.striveScore && this.form.striveScore || 0
            }).then(res => {
                if (res.code === 200) this.form.rewardScore = res.data != 0 && res.data || null;
                else this.form.rewardScore = null;
            }).catch(() => this.form.rewardScore = null);
        },

        // 表单提交方法
        submit() {
            this.$refs.formRef.validate(valid => {
                if (valid) {
                    this.isSaving = true;
                    API.updateById({ ...this.form }).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>