<template>
  <el-dialog :title="row.writeOffStoreStaffId ? '编辑员工' : '新增员工'" :visible.sync="show" width="600px" :before-close="handleClose">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px">
      <el-form-item label="员工姓名：" prop="writeOffName">
        <el-input class="w304" v-model.trim="form.writeOffName" placeholder="请输入内容" maxlength="10" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="writeOffPhone">
        <el-input :disabled="row.writeOffStoreStaffId" class="w303" v-model.trim="form.writeOffPhone">
          <template slot="prepend">中国 +86</template>
        </el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose" :loading="loadings">取 消</el-button>
      <el-button type="primary" @click="submit" :loading="loadings">保存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { setStraff, searchRePhone } from '@/service/api/cardApi.js';
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    row: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    //验证手机号的正则表达式
    let reg = /^(((13[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|(17[3-8]{1})|(18[0-9]{1})|(19[0-9]{1})|(14[5-7]{1}))+\d{8})$/;
    //验证方法
    let validatorPhone = async (writeOffPhone, value, callback) => {
      if (value === '') {
        callback(new Error('手机号不能为空'));
      } else if (!reg.test(value)) {
        callback(new Error('手机号格式错误'));
      } else if (value) {
        await searchRePhone({ writeOffPhone: value }).then(res => {
          if (res.errorCode == 0) {
            if (!res.result) {
              callback(new Error('手机号码已存在'));
            } else {
              callback();
            }
          }
        });
      }
      callback();
    };
    return {
      loadings: false,
      form: {
        writeOffName: '',
        writeOffPhone: '',
        storeId: '',
        writeOffStoreStaffId: '',
        storeCode: '',
        unionId: ''
      },
      rules: {
        writeOffName: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        writeOffPhone: [{ required: true, validator: validatorPhone, trigger: 'blur' }]
      }
    };
  },
  methods: {
    handleClose() {
      this.$refs.form.clearValidate();
      this.$nextTick(_ => {
        this.$emit('update:show', false);
        this.$emit('refresh');
      });
    },
    submit() {
      this.loadings = true;
      this.$refs.form.validate(valid => {
        if (valid) {
          if (!this.form.writeOffStoreStaffId) {
            delete this.form.writeOffStoreStaffId;
          }
          let param = {
            ...this.form
          };
          setStraff(param)
            .then(res => {
              this.$tips({ type: 'success', message: '操作成功' });
              this.loading = false;
              this.handleClose();
              return;
            })
            .catch(err => {
              this.$tips({ type: 'error', message: err.message || '操作失败' });
              this.loading = false;
            });
        } else {
          this.loadings = false;
          return false;
        }
        this.loadings = false;
      });
    }
  },
  watch: {
    row: {
      handler: function(val) {
        if (val.writeOffStoreStaffId) {
          this.form.writeOffName = val.writeOffName;
          this.form.writeOffPhone = val.writeOffPhone;
          this.form.storeId = val.storeId;
          this.form.writeOffStoreStaffId = val.writeOffStoreStaffId;
          this.form.storeCode = val.storeCode;
          this.form.unionId = val.unionId;
        } else {
          this.form.storeId = val.storeId;
          this.form.storeCode = val.storeCode;
          this.form.writeOffName = '';
          this.form.writeOffPhone = '';
          this.form.writeOffStoreStaffId = '';
        }
      },
      immediate: true
    }
  }
};
</script>
<style lang="scss" scoped>
.w304 {
  width: 304px;
}
.w303 {
  width: 303px;
}
</style>
