<template>
  <el-dialog
    :title="title"
    :center="true"
    align="left"
    top="12vh"
    width="900px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    append-to-body
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            label-position="right"
            label-width="90px"
            :model="ruleForm"
            :rules="rulesThis"
            ref="ruleForm"
          >
            <el-row>
              <el-col :span="12">
                <el-form-item label="联系人：" prop="contactor">
                  <el-input
                    v-model="ruleForm.contactor"
                    placeholder="联系人"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="电话：" prop="commobile">
                  <el-input
                    v-model="ruleForm.commobile"
                    placeholder="电话"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="职位：" prop="position">
                  <el-input
                    v-model="ruleForm.position"
                    placeholder="职位"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="部门：" prop="comdepart">
                  <el-input
                    v-model="ruleForm.comdepart"
                    placeholder="部门"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="12">
                <el-form-item label="微信号：" prop="wxaccount">
                  <el-input
                    v-model="ruleForm.wxaccount"
                    placeholder="微信号"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="QQ：" prop="qqaccount">
                  <el-input
                    v-model="ruleForm.qqaccount"
                    placeholder="QQ"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="邮箱：" prop="email">
                  <el-input
                    v-model="ruleForm.email"
                    placeholder="邮箱"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12" style="text-align: right">
                <el-button type="primary" @click="submitForm">保 存 </el-button>
                <el-button @click="showModelThis = false">{{
                  id == "007" ? "关 闭" : "取 消"
                }}</el-button>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
   <script>
import { isWeixin, isMobile, validEmail, isQQ } from "@/utils/validate";
export default {
  name: "goodsBack",
  props: ["showModel", "index", "sitem"],
  data() {
    const validateEmail = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (!validEmail(value)) {
          callback(new Error("邮箱格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validatemobile = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("电话不能为空！"));
      } else {
        if (!isMobile(value)) {
          callback(new Error("电话格式不正确！"));
        } else {
          callback();
        }
      }
    };

    const validateWX = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        if (!isWeixin(value)) {
          callback(new Error("微信号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    const validateQQ = (rule, value, callback) => {
      if (value === "") {
        callback();
      } else {
        console.log(value);
        if (!isQQ(value)) {
          callback(new Error("QQ号格式不正确！"));
        } else {
          callback();
        }
      }
    };
    return {
      loading: false,
      title: "添加联系人",
      showModelThis: this.showModel,
      ruleForm: {},
      rulesThis: this.rules,
      organName: "",
      rules: {
        contactor: [
          {
            required: true,
            message: "联系人不能为空！",
            trigger: "change",
          },
        ],
        position: [
          {
            required: true,
            message: "职位不能为空",
            trigger: "blur",
          },
        ],
        comdepart: [
          {
            required: true,
            message: "部门不能为空",
            trigger: "blur",
          },
        ],
        commobile: [
          {
            required: true,
            validator: validatemobile,
            trigger: "blur",
          },
        ],
        wxaccount: [
          {
            required: false,
            validator: validateWX,
            trigger: "blur",
          },
        ],
        qqaccount: [
          {
            required: false,
            validator: validateQQ,
            trigger: "blur",
          },
        ],
        email: [
          {
            required: false,
            validator: validateEmail,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      console.log(this.index);
      console.log(this.sitem);
      this.resetFormData();
      this.rulesThis = this.rules;
      if (this.index + "" === "-1") {
        this.title = "添加联系人";
      } else {
        this.title = "修改联系人";
      }
      await this.resetForm();
      this.loading = false;
    },
    async resetForm() {
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },
    resetFormData() {
      const {
        id,
        contactor,
        is_del,
        commobile,
        position,
        wxaccount,
        qqaccount,
        email,
        comdepart,
        status,
      } = this.sitem;
      this.ruleForm = {
        id: id || "",
        index: this.index || -1,
        contactor: contactor || "",
        position: position || "",
        wxaccount: wxaccount || "",
        qqaccount: qqaccount || "",
        email: email || "",
        comdepart: comdepart || "",
        status: status || "",
        is_del: is_del || "0",
        commobile: commobile || "",
      };
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.showModelThis = false;
          let item = JSON.parse(JSON.stringify(this.ruleForm));
          item.index = this.index;
          // 刷新
          this.$emit("refresh", item);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.storeAnomaly {
  // width: calc( 100% - 110px);
}
</style>
   