<template>
  <div>
    <BaseDialog
      width="580px"
      :isShow.sync="isShow"
      :title="merId ? '编辑商户' : '添加商户'"
      @submit="submitForm"
      @close="close"
    >
      <el-form
        :model="formData"
        :rules="rules"
        ref="formData"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="合作商户：" prop="merName">
          <el-input
            clearable
            v-model="formData.merName"
            placeholder="请输入合作商户"
          ></el-input>
        </el-form-item>
        <el-form-item label="联系人：" prop="contacts">
          <el-input
            clearable
            v-model="formData.contacts"
            placeholder="请输入联系人"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号码：" prop="telphone">
          <el-input
            clearable
            v-model="formData.telphone"
            placeholder="请输入手机号码"
          ></el-input>
        </el-form-item>
        <el-form-item label="用户名：">
          <el-input
            disabled
            clearable
            v-model="formData.merAccount"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码：" v-if="merId === 0">
          <el-input
            disabled
            clearable
            v-model="formData.initPwd"
            placeholder="请输入密码"
          ></el-input> </el-form-item
        ><el-form-item label="状态：" prop="status">
          <el-radio-group v-model="formData.status">
            <el-radio :label="1">启用</el-radio
            ><el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
    </BaseDialog>
  </div>
</template>

<script>
export default {
  name: "",
  props: {
    dialogVisible: {
      type: Boolean,
      default: false,
    },
    merId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    var checkTel = (rule, value, callback) => {
      var reg = /^1(3|4|5|6|7|8|9)\d{9}$/;
      if (!value) {
        return callback(new Error("请输入手机号码"));
      } else if (!reg.test(value)) {
        return callback(new Error("请输入正确手机号码"));
      } else {
        if (this.merId === 0) {
          this.$set(
            this.formData,
            "initPwd",
            value.substr(value.length - 6, value.length)
          );
        }
        callback();
      }
    };
    return {
      formData: {
        merName: "",
        contacts: "",
        telphone: "",
        merAccount: "",
        initPwd: "",
        status: 1,
      },
      rules: {
        merName: [
          { required: true, message: "请输入合作商户", trigger: "blur" },
        ],
        contacts: [
          { required: true, message: "请输入联系人", trigger: "blur" },
        ],
        merAccount: [
          { required: false, message: "请输入用户名", trigger: "blur" },
        ],
        initPwd: [{ required: false, message: "请输入密码", trigger: "blur" }],
        telphone: [{ required: true, validator: checkTel, trigger: "blur" }],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
    };
  },

  methods: {
    init() {
      if (this.merId) {
        this.$api.mallmerchantdata(this.merId).then((res) => {
          this.formData = res.data;
          this.formData.merAccount = res.data.userName;
        });
      } else {
        this.formData = {
          merName: "",
          contacts: "",
          telphone: "",
          merAccount: "manager",
          initPwd: "",
          status: 1,
        };
      }
    },
    close() {
      this.$refs["formData"].resetFields();
    },
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          this.$api[this.merId ? "editmallmerchant" : "addmallmerchant"](
            this.formData
          ).then((res) => {
            this.$message.success("成功");
            this.isShow = false;
            this.$store.commit("MERLIST");
            this.$emit("search");
          });
        } else {
          return false;
        }
      });
    },
  },
  computed: {
    isShow: {
      get() {
        if (this.dialogVisible) {
          this.init();
        }
        return this.dialogVisible;
      },
      set(val) {
        this.$emit("update:dialogVisible", false);
      },
    },
  },
};
</script>

<style lang="scss" scoped>
.el-input {
  width: 220px;
  margin-right: 10px;
}
.el-select {
  width: 220px;
  margin-right: 10px;
}
</style>
