<template>
    <el-dialog class="client-data-dialog" :title="title" width="40%" :visible.sync="visible" :before-close="handleClose">
        <el-form :model="ruleForm" ref="ruleForm"  :rules="rules" label-width="80px" class="demo-ruleForm">
            <el-form-item label="确认意见" prop="operaterDesc">
                <el-col :span="20"><el-input v-model="ruleForm.operaterDesc" placeholder="请输入确认意见"></el-input></el-col>
            </el-form-item>
        </el-form>
        <div slot="footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" v-if="button.type == 'CONFIRM'" @click="confirmBacked('ruleForm')">确 定</el-button>
            <el-button type="primary" v-if="button.type == 'REFUSE'" @click="refuseBacked('ruleForm')">驳 回</el-button>
        </div>
    </el-dialog>
</template>

<script>
    export default {
        name: "ByhConfirmRefuseDailog",
        props: {
            dialogVisible: {
                type: Boolean,
                required: true
            },
            backedNo: {
                type: String
            },
            btnType: {
                type: String
            },
        },

        data() {
            return {
                ui: {
                    submitLoading: false
                },
                visible: this.$props.dialogVisible,

                ruleForm: {
                    backedNo: '',
                    operater: '',
                    operaterDesc: '',
                },
                rules: {
                    operaterDesc: [{required: true, message: '请输入确认意见', trigger: 'blur'}],
                },
                loginName:'',
                button:{
                    type: '',
                },
            }
        },

        created() {
            this.ruleForm = {
                backedNo: this.backedNo,
            }
        },

        computed: {
            title() {
                if (this.button.type === 'CONFIRM') return '还款单确认';
                else if (this.button.type === 'REFUSE') return '还款单驳回'
            }
        },

        mounted() {
            this.loginName = localStorage.getItem("loginName");
            this.button.type = this.btnType;
        },

        methods: {
            /** 确认点击 */
            confirmBacked() {
                this.$refs['ruleForm'].validate((valid) => {
                    if (!valid) {
                        return false
                    }
                    this.$$post(`/back/pass`, {
                        backedNo: this.ruleForm.backedNo,
                        operater: this.loginName,
                        operaterDesc: this.ruleForm.operaterDesc,
                    }).then(res => {
                        if (res.data.code != 200) {
                            this.$message.error(res.data.message);
                            return
                        }
                        this.$message({message: '操作成功', type: 'success'});
                        this.handleClose();
                        window.location.reload();
                    })
                    .catch(err => {
                        alert("error!")
                    })
                })
            },

            /** 驳回点击 */
            refuseBacked() {
                this.$refs['ruleForm'].validate((valid) => {
                        if (!valid) {
                            return false
                        }
                    this.$$post(`/back/fail`,{backedNo: this.ruleForm.backedNo, operater: this.loginName, operaterDesc:this.ruleForm.operaterDesc,
                    }).then(res => {
                        if (res.data.code < 200) {
                            this.$message.error(res.data.message);
                            return
                        }
                        this.$message({message: '操作成功', type: 'success'});
                        this.handleClose();
                        window.location.reload();
                    })
                })
            },

            /** 关闭处理 */
            handleClose() {
                this.visible = false;
                setTimeout(() => {
                    this.$emit('update:dialogVisible', false)
                }, 300);
            },
        }
    }
</script>

<style scoped>

</style>