<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible" width="70%">
    <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="120px"
      style="padding: 10px;" :disabled="isDetail">
      <el-row>
        <el-col :span="12">
          <el-form-item label="店铺名" prop="name">
            <el-input v-model="formData.name" placeholder="请输入店铺名" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="店铺域名" prop="website">
            <el-input v-model="formData.website" placeholder="请输入店铺域名" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="首页图片">
            <UploadImg v-model="formData.logoUrl" :limit="1" :is-show-tip="false" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
    <el-button :disabled="formLoading" type="danger" @click="deleteStore">删除</el-button>
    <el-button :disabled="formLoading" type="primary" @click="submitForm">保存</el-button>
  </template>
  </Dialog>
  
</template>
<script lang="ts" setup>
import { ShopApi } from '@/api/system/sale/shop/index'
import { FormRules } from 'element-plus'
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
defineOptions({ name: 'SystemUserForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  name: '',
  id: undefined,
  status: 1,
  merchantId: 0,
  logoUrl: '',
  website:''
})
const formRules = reactive<FormRules>({
  name: [{ required: true, message: '店铺名不能为空', trigger: 'blur' }],
  logoUrl: [{ required: true, message: '店铺名不能为空', trigger: 'blur' }],
})
const isDetail = ref(false)
const openType = ref('create')
const id = ref(0)
/** 打开弹窗 */
const open = async (type: string, obj: object, merchantId: number) => {
  openType.value = type
  dialogTitle.value = t('action.' + type)
  if (type == "view") {
    dialogTitle.value = "查看"
  }
  // 判断打开状态，如果是view那就只能查看 否则可以编辑
  if ('view' == type) {
    isDetail.value = true
  } else {
    isDetail.value = false
  }
  dialogVisible.value = true
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (obj.id) {
    formLoading.value = true
    try {
      ShopApi.getShop(obj.id).then((res)=>{
        formData.value = res
      })
    }finally {
      formLoading.value = false
    }
  }
  formData.value.merchantId = merchantId
  formData.value.id = obj.id

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗
const formRef = ref() // 表单 Ref


/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ShopApi.UserVO
    if (formType.value === 'create') {
      await ShopApi.createShop(data)
      message.success(t('common.createSuccess'))
    } else {
      await ShopApi.updateShop(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}
const deleteStore = async() => {
  await message.confirm('是否确认删除'+formData.value.name+'？')
  try{
    await ShopApi.deleteShop(formData.value.id as number)
  }catch{
    formLoading.value = false
  }
  dialogVisible.value = false
  message.success('删除成功')
  emit('success')

}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    name: '',
    id: undefined,
    status: 1,
    merchantId: 0,
    logoUrl: ''
  }
  formRef.value?.resetFields()
}

</script>
<style src="@/styles/PcComponents.css"  lang="scss" scoped></style>
