<template>
    <div id="tax_hardware_form">
        <el-form :model="form" :rules="formRules" ref="form" label-position="top">
            <el-form-item prop="digitalCertPwd">
                <span slot="label">
                    <span style="margin-right:10px">税控设备数字证书密码</span>
                    <el-tag type="warning" v-if="isModifyDigitalCertPwd">修改</el-tag>
                </span>
                <el-input v-model="form.digitalCertPwd" :readonly="readonly"></el-input>
            </el-form-item>
            <el-form-item prop="ukeyPwd">
                <span slot="label">
                    <span style="margin-right:10px">税控设备口令密码</span>
                    <el-tag type="warning" v-if="isModifyUkeyPwd">修改</el-tag>
                </span>
                <el-input v-model="form.ukeyPwd" :readonly="readonly"></el-input>
            </el-form-item>
            <el-form-item prop="rootLoginPwd">
                <span slot="label">
                    <span style="margin-right:10px">税控设备管理员密码</span>
                    <el-tag type="warning" v-if="isModifyRootLoginPwd">修改</el-tag>
                </span>
                <el-input v-model="form.rootLoginPwd" :readonly="readonly"></el-input>
            </el-form-item>
            <el-row :gutter="20">
                <el-col :span="12">
                    <el-form-item prop="taxDiscId">
                        <span slot="label">
                            <span style="margin-right:10px">税盘id</span>
                            <el-tag type="warning" v-if="isModifyTaxDiscId">修改</el-tag>
                        </span>
                        <el-input v-model="form.taxDiscId" :readonly="readonly"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item prop="entTaxOfficeCode">
                        <span slot="label">
                            <span style="margin-right:10px">税局</span>
                            <el-tag type="warning" v-if="isModifyEntTaxOfficeCode">修改</el-tag>
                        </span>
                        <el-input v-model="form.entTaxOfficeCode" :readonly="readonly"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </div>
</template>

<script>
// 表单的默认值
const formDefault = {
    digitalCertPwd: '11111111', // 税控设备数字证书密码
    ukeyPwd: '88888888', // 税控设备口令密码
    rootLoginPwd: 'wasd1234567890.', // 税控设备管理员密码
    taxDiscId: '', // 税盘id
    entTaxOfficeCode: '', // 税局
}

export default {
    name: 'TaxHardwareForm',
    props: {
        // 1.输入信息
        // 2.用于覆盖已有数据
        inputInfo: {
            type: Object,
            default: null,
        },
        readonly: {
            type: Boolean,
            default: false
        },
        isvPlatform: {
            type: Number,
            default: null
        }
    },
    data() {
        return {
            oldFormData: null,// 原数据
            form: JSON.parse(JSON.stringify(formDefault)),
            formRules: {
                digitalCertPwd: [
                    { required: true, message: '税务数字证书密码不可为空', trigger: 'change' }
                ],
                ukeyPwd: [
                    { required: true, message: '税务UKey密码不可为空', trigger: 'change' }
                ],
                rootLoginPwd: [
                    { required: true, message: '管理员登录密码不可为空', trigger: 'change' }
                ],
                taxDiscId: [
                    { required: true, message: '税盘ID不可为空', trigger: 'change' },
                    { min: 3, max: 18, message: '长度在 3 ~ 18 个字符', trigger: 'change' }
                ],
            },
        };
    },
    watch: {
        inputInfo(val) {
            this.updateForm(val);
        }
    },
    computed: {
        // 税控设备数字证书密码
        isModifyDigitalCertPwd() {
            if (this.oldFormData == null || (JSON.stringify(this.oldFormData) == '{}') || this.oldFormData.entPwdInfoVO == undefined) return false;
            return this.oldFormData.entPwdInfoVO.digitalCertPwd === this.form.digitalCertPwd ? false : true;
        },
        // 税控设备口令密码
        isModifyUkeyPwd() {
            if (this.oldFormData == null || (JSON.stringify(this.oldFormData) == '{}') || this.oldFormData.entPwdInfoVO == undefined) return false;
            return this.oldFormData.entPwdInfoVO.keyPwd === this.form.ukeyPwd ? false : true;
        },
        // 税控设备管理员密码
        isModifyRootLoginPwd() {
            if (this.oldFormData == null || (JSON.stringify(this.oldFormData) == '{}') || this.oldFormData.entPwdInfoVO == undefined) return false;
            return this.oldFormData.entPwdInfoVO.rootLoginPwd === this.form.rootLoginPwd ? false : true;
        },
        // 税盘id
        isModifyTaxDiscId() {
            if (this.oldFormData == null || (JSON.stringify(this.oldFormData) == '{}')) return false;
            return this.oldFormData.taxDiscId === this.form.taxDiscId ? false : true;
        },
        // 税局
        isModifyEntTaxOfficeCode() {
            if (this.oldFormData == null || (JSON.stringify(this.oldFormData) == '{}')) return false;
            return this.oldFormData.entTaxOfficeCode === this.form.entTaxOfficeCode ? false : true;
        }
    },
    methods: {
        updateForm(val) {
            if (val == null || (JSON.stringify(val) == "{}")) {
                this.oldFormData = null;
                this.form = JSON.parse(JSON.stringify(formDefault));
            } else {
                this.oldFormData = JSON.parse(JSON.stringify(val));
                this.form = {
                    digitalCertPwd: this.oldFormData.entPwdInfoVO.digitalCertPwd,
                    ukeyPwd: this.oldFormData.entPwdInfoVO.keyPwd,
                    rootLoginPwd: this.oldFormData.entPwdInfoVO.rootLoginPwd,
                    taxDiscId: this.oldFormData.taxDiscId,
                    entTaxOfficeCode: this.oldFormData.entTaxOfficeCode,
                }
            }
        },
        /**
         * 对外暴露获取数据的方法，内部进行表单的校验，父组件中通过refs调用
         */
        getFormInfo() {
            return new Promise((resolve, reject) => {
                this.$refs['form'].validate((valid) => {
                    if (valid) {
                        let tempForm = {
                            EntPwdInfoVO: {
                                ukeyPwd: this.form.ukeyPwd,
                                digitalCertPwd: this.form.digitalCertPwd,
                                rootLoginPwd: this.form.rootLoginPwd
                            },
                            taxDiscId: this.form.taxDiscId,
                            entTaxOfficeCode: this.form.entTaxOfficeCode,
                        }
                        resolve(tempForm)
                    } else {
                        reject('输入信息不合法，请检查您的输入信息！')
                    }
                });
            })
        },
        /**
         * 对外暴露取消表单验证提示的方法
         */
        resetFormFields() {
            // 取消验证提示
            this.$refs['form'].resetFields();
        },
    },
    mounted() {
        this.updateForm(this.inputInfo);
    }
}
</script>

<style scoped>
</style>