<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="客户手机号" prop="phone">
        <el-input v-model="formData.phone" :disabled="!!formData.buyerUserId" maxlength="11" clearable />
      </el-form-item>
      <el-form-item label="客户昵称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入客户昵称" maxlength="10" show-word-limit />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-radio-group v-model="formData.sex">
          <el-radio label="男">男</el-radio>
          <el-radio label="女">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="formData.birthday"
          size="medium"
          type="date"
          placeholder="选择日期"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="标签" prop="ids">
        <el-select v-model="formData.ids" multiple size="mini" placeholder="请选择标签">
          <el-option v-for="item in tagList" :key="item.labelId" :label="item.labelName" :value="item.labelId" />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" maxlength="520" :rows="3" show-word-limit />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="mini" @click="handleClose">取 消</el-button>
      <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addCustomer, updateCustomer, getCustomerDetail, getLabelData } from '@/api/customer'

export default {
  name: 'EditModal',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '820px',
        title: ''
      },
      visible: false,
      formData: {
        buyerUserId: '',
        phone: '',
        name: '',
        time: '',
        source: '',
        sex: '',
        birthday: '',
        remark: '',
        total: '',
        frequency: '',
        lastTime: '',
        refundMoney: '',
        refunds: '',
        labelName: '',
        ids: []
      },
      formRules: {
        phone: [
          { required: true, message: '缺少客户手机号' },
          { pattern: /^1[3456789]\d{9}$/, message: '目前只支持中国大陆的手机号码' }
        ],
        name: [
          { required: true, message: '请输入客户昵称' }
        ]
      },
      tagList: []
    }
  },
  watch: {
    'formData.buyerUserId': {
      deep: true,
      handler(val) {
        if (val) {
          this.formRules.phone = [
            { required: true, message: '缺少客户手机号' }
          ]
        } else {
          this.formRules.phone = [
            { required: true, message: '缺少客户手机号' },
            { pattern: /^1[3456789]\d{9}$/, message: '目前只支持中国大陆的手机号码' }
          ]
        }
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    async getTagList() {
      const res = await getLabelData()
      this.tagList = res.data
    },
    initList() {
      this.getTagList()
    },
    handleOpen(params = {}) {
      this.modalOptions.title = params.buyerUserId ? '编辑客户基本信息' : '添加客户基本信息'
      this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      if (params.buyerUserId) {
        this.getInfo(params.buyerUserId)
      } else {
        this.$refs.formData && this.$refs.formData.resetFields()
      }
    },
    async getInfo(id) {
      const loading = this.$loading({ text: '加载中' })
      try {
        const res = await getCustomerDetail({ buyerUserId: id })
        this.formData = Object.assign(this.$options.data().formData, res.data, {
          buyerUserId: res.data.buyerUserId || '',
          phone: res.data.phone || '',
          name: res.data.name || '',
          time: res.data.time || '',
          source: res.data.source || '',
          sex: res.data.sex,
          birthday: res.data.birthday || '',
          remark: res.data.remark || '',
          total: res.data.total,
          frequency: res.data.frequency,
          lastTime: res.data.lastTime || '',
          refundMoney: res.data.refundMoney,
          refunds: res.data.refunds,
          labelName: res.data.labelName || '',
          ids: res.data.ids || []
        })
        this.$nextTick(() => {
          this.$refs.formData && this.$refs.formData.validate()
        })
      } finally {
        loading.close()
      }
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '提交中，请稍候……' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            this.formData.buyerUserId ? await updateCustomer(params) : await addCustomer(params)
            loading.close()
            this.$message({ message: `${this.formData.buyerUserId ? '编辑' : '添加'}成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

