<template>
  <el-form ref="form" label-width="84px">
    <el-form-item label="案件名称">
      <el-input
        v-model="bindExample.caseTitle"
        maxlength="300"
        placeholder="请输入案件名称"
      />
    </el-form-item>

    <el-form-item label="案件编号">
      <el-input
        v-model="bindExample.caseNum"
        maxlength="300"
        placeholder="请输入案件编号"
      />
    </el-form-item>

    <el-form-item label="案件类别">
      <!-- <el-input
        v-model="bindExample.caseCategory"
        maxlength="300"
        placeholder="请输入案件类别"
      /> -->
      <el-select
        v-model="bindExample.caseCategory"
        placeholder="请输入案件类别"
        style="width: 40%"
      >
        <el-option v-for="item in criminalType" :label="item" :value="item" />
      </el-select>
    </el-form-item>

    <el-form-item label="案件区域">
      <!-- <el-input
        v-model="bindExample.caseRegion"
        maxlength="300"
        placeholder="案件区域"
      /> -->
      <el-cascader
        size="large"
        style="width: 100%"
        :props="selectSetting"
        :options="(geoData as any as CascaderOption[])"
        v-model="bindExample.caseRegion"
      ></el-cascader>
    </el-form-item>
    <el-form-item label="案件地点">
      <el-input
        v-model="bindExample.mapUrl"
        placeholder="输入名称搜索"
        clearable
        disabled
      >
        <template #append>
          <el-button :icon="Search" @click="searchAMapAddress" />
        </template>
      </el-input>
    </el-form-item>

    <!-- <el-form-item label="案件地点">
      <el-input
        v-model="bindExample.caseAddress"
        maxlength="300"
        placeholder="案件地点"
      />
    </el-form-item> -->
    <!-- class="mandatory" -->
    <div class="el-form-item">
      <el-col :span="12">
        <el-form-item label="是否命案">
          <el-radio-group v-model="bindExample.homicideCase">
            <el-radio :value="1">是</el-radio>
            <el-radio :value="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="是否刑案">
          <el-radio-group v-model="bindExample.criminalCase">
            <el-radio :value="1">是</el-radio>
            <el-radio :value="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </div>

    <el-form-item label="经纬度">
      <el-input
        v-model="bindExample.latAndLong"
        maxlength="50"
        placeholder="经纬度"
      />
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { Example, setExample, addExample } from "@/app/criminal/store/example";
import { ElMessage, CascaderOption, CascaderProps } from "element-plus";
import { QuiskExpose } from "@/helper/mount";
import { Search } from "@element-plus/icons-vue";
import { selectMapImage } from "@/view/case/quisk";
// import geoData from "province-city-china/dist/level.json";
import { geoData } from "./getGeo";
import { criminalType } from "./criminalType";

console.log("geoData", geoData);
const selectSetting = ref<CascaderProps>({
  value: "name",
  label: "name",
});

interface CriminalExample extends Example {
  caseNum: string; //案件编号
  caseCategory: string; //案件类别
  caseRegion: string; //案发区域
  caseAddress: string; //案发地址
  homicideCase: number; //是否命案，0否，1是
  criminalCase: number; //是否刑案，0否，1是
}

const props = defineProps<{ example?: CriminalExample }>();

const bindExample = ref<CriminalExample>(
  props.example ? { ...props.example } : ({} as CriminalExample)
);

defineExpose<QuiskExpose>({
  async submit() {
    if (!bindExample.value.caseTitle || !bindExample.value.caseTitle.trim()) {
      ElMessage.error("案件名称不能为空");
      throw "案件名称不能为空";
    } else if (
      !bindExample.value.latAndLong ||
      !bindExample.value.latAndLong.trim()
    ) {
      ElMessage.error("详细地址不能为空");
      throw "详细地址不能为空！";
    }
    await (bindExample.value.caseId
      ? setExample(bindExample.value)
      : addExample(bindExample.value));
  },
});

const searchAMapAddress = async () => {
  const data = await selectMapImage({});
  if (!data?.search) return;
  bindExample.value.mapUrl = data.search.text;
  bindExample.value.latAndLong = `${data.search.lat},${data.search.lng}`;
};
</script>
