<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="form.writeOffName" placeholder="请输入内容" maxlength="10" show-word-limit></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="writeOffPhone" :error="errorRe">
        <el-select class="w116" :disabled="row.writeOffStoreStaffId" v-model="typeSe" placeholder="请选择">
          <el-option v-for="item in typeList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
        </el-select>
        <el-input :disabled="row.writeOffStoreStaffId" class="w178" v-model="form.writeOffPhone" @blur="phoneChange"></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 = (writeOffPhone, value, callback) => {
      if (value === '') {
        callback(new Error('手机号不能为空'));
      } else if (!reg.test(value)) {
        callback(new Error('手机号格式错误'));
      } else {
        callback();
      }
    };
    return {
      loadings: false,
      form: {
        writeOffName: '',
        writeOffPhone: '',
        storeId: '',
        writeOffStoreStaffId: '',
        storeCode: ''
      },
      rules: {
        writeOffName: [{ required: true, message: '请输入员工姓名', trigger: 'blur' }],
        writeOffPhone: [{ required: true, validator: validatorPhone, trigger: 'blur' }]
      },
      errorRe: '',
      typeSe: '1',
      typeList: [
        {
          value: '1',
          label: '中国    +86'
        },
        {
          value: '2',
          label: '澳门   +853'
        },
        {
          value: '3',
          label: '香港   +852'
        },
        {
          value: '4',
          label: '台湾   +886'
        }
      ]
    };
  },
  methods: {
    async phoneChange() {
      await searchRePhone({ writeOffPhone: this.form.writeOffPhone }).then(res => {
        if (res.errorCode == 0) {
          // this.rePhone = res.result;
          // console.log(this.rePhone, 1);
          if (!res.result) {
            console.log(res.result, 33);
            this.errorRe = '手机号码已存在';
          }
        }
      });
    },
    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;
          }
          setStraff(this.form)
            .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 {
          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;
        } else {
          this.form.storeId = val.storeId;
          this.form.storeCode = val.storeCode;
          this.form.writeOffName = '';
          this.form.writeOffPhone = '';
          this.form.writeOffStoreStaffId = '';
        }
      },
      immediate: true
    }
    // rePhone: {
    //   handler: function(val) {
    //     this.rePhone = val;
    //     console.log(this.rePhone, 2);
    //   },
    //   immediate: true
    // }
  }
};
</script>
<style lang="scss" scoped>
.w304 {
  width: 304px;
}
.w178 {
  width: 178px;
}
.w116 {
  width: 116px;
}
.mr9 {
  margin-right: 9px;
}
</style>
