<template>
  <el-form ref="formRef" :model="formData" :class="{ 'view': isDetail, 'mobile-card': mobile }" :rules="formRules"
    label-width="120px" class="overflow-auto" v-loading="formLoading" :disabled="isDetail" style="overflow: hidden;">
    <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" v-if="openType != '新增'">
        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="请选择商户状态" class="w-1/1" clearable>
            <el-option label="使用中" :value="10" />
            <el-option label="过期" :value="20" />
            <el-option label="停用" :value="30" />
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="负责人" prop="contact">
          <el-input v-model="formData.contact" placeholder="请输入负责人" />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="所在地" prop="areaId">
          <el-cascader v-model="formData.areaId" :options="areaTree" :props="defaultProps" class="w-1/1" clearable
            placeholder="请选择所在地" filterable collapse-tags />
        </el-form-item>
        
      </el-col>
      
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="负责人手机号" prop="contactNumber">
          <el-input v-model="formData.contactNumber" placeholder="请输入负责人手机号" maxlength="11" onkeyup="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="标签">
          <el-input  disabled />
        </el-form-item>
      </el-col>
      <el-col :span="12" >
        <el-form-item label="办公地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入办公地址" />
        </el-form-item>
      </el-col>
      <el-col :span="12" style="display: none;">
        <el-form-item label="服务到期时间" prop="expireTime">
          <el-date-picker v-model="formData.expireTime" type="date" value-format="x" placeholder="选择服务到期时间" />
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="维权电话" prop="complaintsHotline">
          <el-input v-model="formData.complaintsHotline" placeholder="请输入维权电话"  maxlength="11" onkeyup="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="客服电话" prop="customerServiceHotline">
          <el-input v-model="formData.customerServiceHotline" placeholder="请输入客服电话"  maxlength="11" onkeyup="value=value.replace(/[^\d]/g,'')"/>
        </el-form-item>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="12">
        <el-form-item label="官网" prop="website">
          <el-input v-model="formData.website" placeholder="请输入官网" />
        </el-form-item>
      </el-col>

      <!-- 只有查看的时候才显示这个 -->
      <el-col :span="12" v-if="isDetail">
        <el-form-item label="创建时间" prop="customerServiceHotline">
          <el-date-picker v-model="formData.createTime" type="date" value-format="x" disabled />
        </el-form-item>
      </el-col>
    </el-row>
    <!-- {{ mobile }} -->
    <div v-if="mobile">
      <el-row>
        <el-col :span="12">
          <el-form-item style="border: none;" label="营业执照" prop="businessLicensePicture" />
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item prop="businessLicensePicture" class="businessLicensePicture">
            <UploadImg v-model="formData.businessLicensePicture" :limit="1" :disabled="isDetail" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item style="border: none;" label="简介" prop="description" />
        </el-col>
      </el-row>
      <el-col :span="24">
        <el-form-item prop="description" class="description">
          <div v-html="formData.description"></div>
        </el-form-item>
      </el-col>
    </div>
    <div v-else>
      <el-row>
        <el-col :span="24">
          <el-form-item label="营业执照" prop="businessLicensePicture">
            <UploadImg v-model="formData.businessLicensePicture" :limit="1" :disabled="isDetail" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="简介" prop="description">
            <Editor v-model="formData.description" height="150px" :disabled="isDetail" />
          </el-form-item>
        </el-col>
      </el-row>
    </div>
  </el-form>
</template>

<script setup lang="ts">
import { MerchantApi, MerchantVO } from '@/api/system/sale/merchant'
import { copyValueToTarget } from '@/utils'
import { formatDate } from '@/utils/formatTime'
import * as AreaApi from '@/api/system/area'
import { defaultProps } from '@/utils/tree'

// 从路由中引入store中的app仓库
import { useAppStore } from '@/store/modules/app'
const appStore = useAppStore()
const mobile = computed(() => appStore.getMobile)
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
// const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = reactive({
  id: undefined,
  name: undefined,
  status: undefined,
  description: undefined,
  contact: undefined,
  address: undefined,
  contactNumber: undefined,
  website: undefined,
  createTime: undefined,
  complaintsHotline: undefined,
  customerServiceHotline: undefined,
  email: undefined,
  businessLicensePicture: undefined,
  expireTime: undefined,
  areaId:undefined,
})
const formRules = reactive({
  name: [{ required: true, message: '商户名称不能为空', trigger: 'blur' }],
  // status: [{ required: true, message: '状态', trigger: 'blur' }],
  description: [{ required: true, message: '简介不能为空', trigger: 'blur' }],
  contact: [{ required: true, message: '负责人不能为空', trigger: 'blur' }],
  // address: [{ required: true, message: '办公地址不能为空', trigger: 'blur' }],
  contactNumber: [{ required: true, message: '负责人手机号不能为空', trigger: 'blur' }],
  // complaintsHotline: [{ required: true, message: '维权电话不能为空', trigger: 'blur' }],
  areaId: [{ required: true, message: '所在地不能为空', trigger: 'blur' }],
  customerServiceHotline: [{ required: true, message: '客服电话不能为空', trigger: 'blur' }],
  email: [{ required: true, message: '邮箱不能为空', trigger: 'blur' }],
  businessLicensePicture: [{ required: true, message: '营业执照不能为空', trigger: 'blur' }],
  // expireTime: [{ required: true, message: '服务到期时间不能为空', trigger: 'blur' }]
})
const formRef = ref()
const props = defineProps({
  id: {
    type: Number,
    default: 0
  },
  "isDetail": {
    type: Boolean,
    default: false
  },
  "openType": {
    type: String,
    default: "查看" 
  },
  propFormData: {
    type: Object,
    default: () => { }
  }
});
const areaTree = ref([])
onMounted(async () => {
  areaTree.value = await AreaApi.getAreaTree()
})



/** 将传进来的值赋值给 formData */
watch(
  () => props.propFormData,
  (data) => {
    if (!data) {
      return
    }
    copyValueToTarget(formData, data)
  },
  {
    immediate: true
  }
)

const emit = defineEmits(['update:activeName'])

const validate = async () => {
  if (!formRef) return
  try {
    await unref(formRef)?.validate()
    // 校验通过更新数据
    Object.assign(props.propFormData, formData)
    return true
  } catch (e) {
    message.error('基本信息不完善，请填写相关信息')
    emit('update:activeName', 'info')
    return false
    throw e // 目的截断之后的校验
  }
}
defineExpose({ validate })
const updateInitialData = inject('updateInitialData');

watch(formData, (data) => {
  try { copyValueToTarget(updateInitialData.value, formData) } catch {
    // console.log(data)
  }
},
  {
    immediate: true
  }
)
</script>

<style src="./MerchantComponents.css"  lang="scss" scoped></style>
