<template>
  <el-dialog
    :title="isEdit ? '编辑商户' : '新增商户'"
    :visible.sync="dialogVisible"
    width="600px"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px"
      class="merchant-form"
    >
      <el-form-item label="商户名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入商户名称" />
      </el-form-item>
      <el-form-item label="联系人" prop="contactName">
        <el-input v-model="form.contactName" placeholder="请输入联系人" />
      </el-form-item>
      <el-form-item label="联系电话" prop="contactPhone">
        <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
      </el-form-item>
      <el-form-item label="联系邮箱" prop="contactEmail">
        <el-input v-model="form.contactEmail" placeholder="请输入联系邮箱" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input v-model="form.address" placeholder="请输入地址" />
      </el-form-item>
      <el-form-item label="服务到期时间" prop="expireAt" v-if="isEdit">
        <el-date-picker
          v-model="form.expireAt"
          type="datetime"
          placeholder="请选择服务到期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" v-if="isEdit">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">正常</el-radio>
          <el-radio :label="2">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { merchantUpdate, merchantCreate } from '@/api/merchant'

export default {
  name: 'MerchantEdit',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    merchantData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      isEdit: false,
      form: {
        name: '',
        contactName: '',
        contactPhone: '',
        contactEmail: '',
        address: '',
        expireAt: '',
        status: 1
      },
      rules: {
        name: [
          { required: true, message: '请输入商户名称', trigger: 'blur' },
          { min: 2, max: 50, message: '长度在 2 到 50 个字符', trigger: 'blur' }
        ],
        contactName: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ],
        contactEmail: [
          { required: true, message: '请输入联系邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        address: [
          { required: true, message: '请输入地址', trigger: 'blur' }
        ],
        expireAt: [
          { required: true, message: '请选择服务到期时间', trigger: 'change' }
        ]
      }
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
      if (val) {
        this.initForm()
      }
    }
  },
  methods: {
    initForm() {
      this.isEdit = !!this.merchantData.id
      if (this.isEdit) {
        this.form = { ...this.merchantData }
      } else {
        this.form = {
          name: '',
          contactName: '',
          contactPhone: '',
          contactEmail: '',
          address: '',
          expireAt: '',
          status: 1
        }
      }
      this.$nextTick(() => {
        this.$refs.form && this.$refs.form.clearValidate()
      })
    },
    handleClose() {
      this.$emit('update:visible', false)
    },
    async handleSubmit() {
      try {
        await this.$refs.form.validate()
        this.loading = true
        const api = this.isEdit ? merchantUpdate : merchantCreate
        const res = await api(this.form)
        if (res.code === 0) {
          this.$message.success(this.isEdit ? '编辑成功' : '新增成功')
          this.$emit('success')
          this.handleClose()
        } else {
          this.$message.error(res.message || '操作失败')
        }
      } catch (error) {
        console.error(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.merchant-form {
  padding: 20px 0;
  ::v-deep .el-form-item__label {
    font-weight: 500;
  }
  ::v-deep .el-input__inner {
    height: 40px;
    line-height: 40px;
  }
  ::v-deep .el-date-editor.el-input {
    width: 100%;
  }
}
.dialog-footer {
  text-align: right;
  padding-top: 20px;
}
</style>
