<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="50%">
        <el-form ref="form" :model="form" :rules="rules" label-width="160px">
            <el-row>
              <Field :span="20" label="项目名称" prop="name" v-model="form.name" placeholder="请输入项目名称"/>
              <Field :span="20" label="mac" prop="mac" v-model="form.mac" placeholder="请输入机器mac地址"/>
              <Field :span="20" label="ip" prop="ip" v-model="form.ip" placeholder="请输入机器ip地址"/>
             <Field :span="20" label="许可证颁发日期" prop="startTime" v-model="form.startTime" type="date" />
              <Field :span="20" label="许可证过期日期" prop="endTime" v-model="form.endTime" type="date" />
              <Field :span="20" label="许可证状态" prop="licenseStatus" v-model="form.licenseStatus" type="select" :enumData="dict.licenseStatus" placeholder="请选择许可证状态"/>
              <Field :span="20" label="备注" prop="remark" v-model="form.remark" type="textarea" placeholder="请输入备注"/>

            </el-row>

            <form-buttons @submit='submitForm' v-if="pageInfo.type!='view'" noCancelBtn />
        </el-form>

    </el-drawer>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    export default {
        name: "LicenseDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("license")
        },
        data() {
            return {
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "许可证",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                ],
                toDate:[
                    "startTime",
                    "endTime",
                ],
                // 表单校验
                rules: {
                    startTime: [
                        {required: true,message: "请选择许可证颁发日期" },
                    ],
                    endTime: [
                        {required: true,message: "请选择许可证过期日期" },
                    ],
                    licenseStatus: [
                        {required: true,message: "请输入许可证状态", trigger: "blur" },
                    ],

                    name: [
                        {required: true,message: "请输入项目名称", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],

                     ip: [
                    { required: true, message: '请输入正确的IP地址', validator: this.validateIP, trigger: 'blur' }
                  ],
                  mac: [
                    { required: true, message: '请输入正确的MAC地址', validator: this.validateMac, trigger: 'blur' }
                  ]
                }
            };
        },

        methods: {
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="license/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改许可证";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "license/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增许可证";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="license/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "许可证详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    mac : "",
                    ip : "",
                    startTime : null,
                    endTime : null,
                    licenseStatus : "有效",
                    remark : null,
                    name : "",
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
          validateIP(rule, value, callback) {
            if (value === '' || typeof value === 'undefined' || value == null) {
              callback(new Error('请输入正确的IP地址'))
            } else {
              const reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
              if ((!reg.test(value)) && value !== '') {
                callback(new Error('请输入正确的IP地址'))
              } else {
                callback()
              }
            }
          },

          validateMac (rule, value, callback) {
            let temp = /[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}-[A-Fa-f0-9]{2}/;
            if (!temp.test(value)) {
              callback(new Error('请输入xx-xx-xx-xx-xx-xx形式的MAC地址！'));
            } else {
              callback();
            }
          }
          },
    };
</script>
