<template>
  <a-modal
    :visible="value"
    title="编辑二级销售人员"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-form
      :form="form"
    >
      <a-form-item
        label="账号"
        v-bind="formItemLayout"
      >
        <a-input
          disabled
          v-decorator="[
            'name',
            {
              initialValue:curentItem.name,
              rules: [{ required: true, message: '请输入账号' }]
            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item
        v-if="isMazu"
        label="真实姓名"
        v-bind="formItemLayout"
      >
        <a-input
          v-decorator="[
            'real_name',
            {
              initialValue:curentItem.real_name,
              rules: [{ required: true, message: '请输入真实姓名' }]
            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item
        v-if="isMazu"
        label="身份证号"
        v-bind="formItemLayout"
      >
        <a-input
          v-decorator="[
            'card_id',
            {
              initialValue:curentItem.card_id,
              rules: [{ required: true, message: '请输入身份证号' },
                      { pattern:/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, message:'请输入正确的证件号'}

              ]

            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item
        v-if="!isMazu"
        label="昵称"
        v-bind="formItemLayout"
      >
        <a-input
          v-decorator="[
            'nick_name',
            {
              initialValue:curentItem.nick_name,
              rules: [{ required: true, message: '请输入昵称' }]
            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item
        v-if="!isMazu"
        label="密码"
        v-bind="formItemLayout"
      >
        <a-input
          type="password"
          v-decorator="[
            'password',
            {

              rules: [

                { required: false, message: '请输入密码' },
                { validator: validateToNextPassword}
              ]
            }
          ]"
        ></a-input>
      </a-form-item>
      <a-form-item
        v-if="!isMazu"
        label="确认密码"
        v-bind="formItemLayout"
      >
        <a-input
          type="password"
          v-decorator="[
            'confirm',
            {
              rules: [
                { required: false, message: '请再次输入密码' },
                { validator: compareToFirstPassword}
              ]
            }
          ]"
          @blur="handleConfirmBlur"
        ></a-input>
      </a-form-item>
      <a-form-item
        label="电话号码"
        v-bind="formItemLayout"
      >
        <a-input
          type="phone"
          v-decorator="[
            'phone',
            {
              initialValue:curentItem.phone,
              rules: [
                { required: true, message: '请输入手机号' },
                { pattern:/^1(3|4|5|7|8|9)\d{9}$/, message:'请输入正确的手机号码'}
              ]
            }
          ]"
        ></a-input>
      </a-form-item>
    </a-form>
  </a-modal>
</template>

<script>
import { editSecondSaleList } from '@/api/marketingSetting/salesUser'
import salesUserMixin from './salesUserMixin.js'

export default {
  mixins: [salesUserMixin],
  props: {
    curentItem: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    isMazu () {
      return ['demo', 'YINXIANGMAZU'].includes(localStorage.mgt_project)
    }
  },
  methods: {
    handleOk (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          if (values.password && !values.confirm) return this.$message.error('请确认密码')
          values.id = this.curentItem.id
          if (this.isMazu) {
            values.password = '123456'
            values.nick_name = '用户'
          }
          editSecondSaleList(values).then(res => {
            this.$message.success('编辑成功。')
            this.form.resetFields()
            this.$emit('input', false)
            this.$emit('update')
          })
        }
      })
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  }
}
</script>

<style scoped>

</style>
