<script setup>
import RPC from '@/utils/rpc'
import MapSelector from '@/components/MapSelector.vue'
import UploadFile from '@/components/UploadFile.vue'
const props = defineProps({
  type: {
    type: String,
    required: true,
  },
  row: [Object],
  categoryList: {
    type: Array,
    default: [],
  },
})

const emit = defineEmits(['closed', 'onSuccess'])

const dialogVisible = ref(false)

const isAdd = computed(() => props.type === 'add')

onMounted(() => {
  dialogVisible.value = true
})

const formRef = ref(null)
const formField = ref({})
const formRules = reactive({
  store_name: [{ required: true, message: '不可为空', trigger: 'change' }],
  latitude: [{ required: true, message: '不可为空', trigger: 'change' }],
  longitude: [{ required: true, message: '不可为空', trigger: 'change' }],
})

const loading = ref(false)
const showMapSelector = ref(false)

function closed() {
  emit('closed')
}

const submitForm = async () => {
  await formRef.value.validate()
  const params = { store: { ...formField.value }, category_ids: formField.value.category_ids }
  RPC.post('/store/create', params)
}

if (!isAdd.value) {
  formField.value = props.row
}

const chooseCoordinates = () => {
  showMapSelector.value = true
}

RPC.post('/store/detail', { id: props.row.id })
</script>

<template>
  <el-dialog
    align-center
    :title="isAdd ? '新增' : '修改'"
    width="80%"
    v-model="dialogVisible"
    @closed="closed"
    :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      label-position="right"
      label-suffix=":"
      :model="formField"
      :rules="formRules"
      v-loading="loading"
      label-width="100"
    >
      <el-form-item prop="store_name" label="门店名称">
        <el-input placeholder="请输入" v-model="formField.store_name" />
      </el-form-item>
      <el-form-item prop="address" label="详细地址">
        <el-input placeholder="请输入" v-model="formField.address" />
      </el-form-item>
      <el-form-item prop="location" label="地址">
        <el-input placeholder="请输入" v-model="formField.location" />
      </el-form-item>
      <el-form-item prop="business_hours" label="工作时间">
        <el-input placeholder="请输入" v-model="formField.business_hours" />
      </el-form-item>
      <el-form-item prop="contact_phone" label="联系电话">
        <el-input placeholder="请输入" v-model="formField.contact_phone" />
      </el-form-item>
      <el-form-item prop="tags" label="分类标签">
        <el-input placeholder="请输入,多个以,分隔" v-model="formField.tags" />
      </el-form-item>
      <el-form-item prop="category_ids" label="分类">
        <el-select
          clearable
          default-first-option
          filterable
          multiple
          placeholder="请选择"
          v-model="formField.category_ids"
        >
          <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="main_image" label="门店主图">
        <img
          :src="formField.main_image"
          alt=""
          class="main_image_box"
          v-if="formField.main_image"
        />
        <UploadFile v-else @on-success="set_main_image"></UploadFile>
      </el-form-item>
      <el-form-item prop="longitude" label="经度">
        <el-input-number
          style="width: 200px"
          :controls="false"
          placeholder="请输入"
          v-model="formField.longitude"
        />
      </el-form-item>
      <el-form-item prop="latitude" label="纬度">
        <el-input-number
          style="width: 200px"
          :controls="false"
          placeholder="请输入"
          v-model="formField.latitude"
        />
      </el-form-item>
      <el-form-item prop="" label="">
        <el-button type="primary" @click="chooseCoordinates">选择坐标</el-button>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closed"> 取 消 </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 地图坐标选择器 -->
  <MapSelector
    v-model="showMapSelector"
    :longitude="formField.longitude"
    :latitude="formField.latitude"
    @confirm="handleCoordinateConfirm"
  />
</template>

<style lang="scss" scoped>
.main_image_box {
  width: 370px;
  height: 130px;
}
</style>
