<template>
  <el-dialog title="账号管理" :close-on-click-modal="false" :visible.sync="visible" width="600px">
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
      style="width:500px;"
    >
      <el-form-item label="店铺名称" prop="shopName" style="width:500px;">
        <el-input v-model="dataForm.shopName" disabled></el-input>
      </el-form-item>
      <br />
      <el-form-item label="店铺账号" prop="mobile" style="width:580px;">
        <el-row style="width:100%">
          <el-col :span="20">
            <el-input
              v-model="dataForm.mobile"
              maxlength="50"
              show-word-limit
              placeholder="请输入账号（账号默认为店主的手机号码）"
              :disabled="!editMobile"
            ></el-input>
          </el-col>
          <el-col :span="4">
            <el-button
              icon="el-icon-edit"
              type="text"
              size="small"
              v-if="!editMobile"
              @click="changeEditMobile()"
            >修改账号</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <br />
      <el-form-item label="登陆密码" prop="password" style="width:500px;">
        <el-input type="password" v-model="dataForm.password" maxlength="50" show-word-limit></el-input>
      </el-form-item>
      <br />
      <el-form-item label="确认密码" prop="checkPassword" style="width:500px;">
        <el-input type="password" v-model="dataForm.checkPassword" maxlength="50" show-word-limit></el-input>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import ImgsUpload from '@/components/imgs-upload'
export default {
  data () {
    var validateMobile = (rule, value, callback) => {
      var reg = /[^\d]/
      console.log('aaa', reg.test(value), value)
      if (reg.test(value)) {
        callback(new Error('账号只能为数字'))
      } else {
        this.$http({
          url: this.$http.adornUrl('/shop/shopAuditing/checkMobile'),
          method: 'get',
          params: this.$http.adornParams({
            mobile: this.dataForm.mobile,
            shopId: this.dataForm.shopId
          })
        }).then(({ data }) => {
          if (data) {
            callback()
          } else {
            callback(new Error('该账号已存在，请重新输入'))
          }
        })
      }
    }
    var validatePassword = (rule, value, callback) => {
      var reg = /[^\w]/
      if (reg.test(value)) {
        callback(new Error('密码只能为数字或字母'))
      } else {
        callback()
      }
    }
    var validateCheckPassword = (rule, value, callback) => {
      var reg = /[^\w]/
      if (reg.test(value)) {
        callback(new Error('密码只能为数字或字母'))
      } else if (this.dataForm.password !== this.dataForm.checkPassword) {
        callback(new Error('两次输入的密码不相同，请重新输入'))
      } else {
        callback()
      }
    }
    return {
      visible: false,
      editMobile: false,
      dataForm: {
        shopId: 0,
        mobile: '',
        password: '',
        checkPassword: ''
      },
      dataRule: {
        mobile: [
          { required: true, message: '店铺账号不能为空', trigger: 'blur' },
          { validator: validateMobile, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { validator: validatePassword, trigger: 'blur' }
        ],
        checkPassword: [
          { required: true, message: '确认密码不能为空', trigger: 'blur' },
          { validator: validateCheckPassword, trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    ImgsUpload
  },
  methods: {
    init (shopId) {
      this.dataForm.shopId = shopId || 0
      this.visible = true
      this.show = false
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.$refs.dataForm.resetFields()
        this.editMobile = false
        this.dataForm.mobile = ''
        this.dataForm.password = ''
        this.dataForm.checkPassword = ''
        this.getDataInfo()
      })
    },
    changeEditMobile () {
      this.editMobile = true
    },
    // 获取店铺详情数据
    getDataInfo () {
      this.$http({
        url: this.$http.adornUrl('/shop/shopAuditing/shopDetail/' + this.dataForm.shopId),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        this.dataForm = data
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          let mobile = this.editMobile ? this.dataForm.mobile : null
          this.$http({
            url: this.$http.adornUrl('/shop/shopAuditing/updatePasswordOrMobile'),
            method: 'put',
            data: this.$http.adornData({
              shopId: this.dataForm.shopId,
              password: this.dataForm.password,
              mobile: mobile
            })
          }).then(({ data }) => {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          })
        }
      })
    },
    message (msg) {
      this.$message({
        message: msg,
        type: 'error',
        duration: 1500
      })
    }
  }
}
</script>

