<template>
  <div class="__normal-page">
    <div class="__normal-tree">
      <div class="__normal-tree_filter">
        <el-input v-model="treeName" placeholder="请输入关键词进行搜索" clearable>
          <template #suffix>
            <img src="@/assets/images/cus/tree-search.png" alt=""/>
          </template>
        </el-input>
      </div>
      <div class="__normal-tree_content">
        <el-tree
            ref="ref_tree"
            :data="treeData"
            node-key="id"
            :props="{
              children: 'children',
              label: 'name',
            }"
            @node-click="handleNodeClick"
            :expand-on-click-node="false"
            highlight-current
            :filter-node-method="treeFilter"
        />
      </div>
    </div>
    <div class="__normal-content" v-if="currentSource">
      <CusForm labelWidth="100px" ref="ref_form">
        <div class="__cus-title_2">实时轨迹唯一标识(DATA.xxx形式，可任意拼接或使用字符串模板`POINT(${DATA.xxx} ${DATA.xxx})`或DATA.xxx + DATA.xxx)</div>
        <CusFormColumn
            :span="12"
            required
            label="轨迹"
            v-model:param="form.trackKey"/>
        <CusFormColumn
            :span="12"
            required
            label="轨迹点"
            v-model:param="form.trackPointKey"/>
        <CusFormColumn
            :span="12"
            required
            label="WKT坐标"
            v-model:param="form.trackWktKey"/>
        <CusFormColumn
            :span="12"
            required
            label="速度"
            v-model:param="form.trackSpeedKey"/>
        <CusFormColumn
            :span="12"
            required
            label="航向"
            v-model:param="form.trackCourseKey"/>
        <CusFormColumn
            :span="12"
            label="船艏向"
            v-model:param="form.trackHeadKey"/>
        <div class="__cus-title_2">历史轨迹唯一标识(DATA.xxx形式，可任意拼接或使用字符串模板`POINT(${DATA.xxx} ${DATA.xxx})`或DATA.xxx + DATA.xxx)</div>
        <CusFormColumn
            :span="12"
            required
            label="轨迹"
            v-model:param="form.historyTrackKey"/>
        <CusFormColumn
            :span="12"
            required
            label="轨迹点"
            v-model:param="form.historyTrackPointKey"/>
        <CusFormColumn
            :span="12"
            required
            label="WKT坐标"
            v-model:param="form.historyTrackWktKey"/>
        <CusFormColumn
            :span="12"
            required
            label="速度"
            v-model:param="form.historyTrackSpeedKey"/>
        <CusFormColumn
            :span="12"
            required
            label="航向"
            v-model:param="form.historyTrackCourseKey"/>
        <CusFormColumn
            :span="12"
            label="船艏向"
            v-model:param="form.historyTrackHeadKey"/>
      </CusForm>
      <div class="buttons">
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import {
  defineComponent,
  onMounted,
  ref,
  toRefs,
  reactive,
  watch,
  getCurrentInstance,
  ComponentInternalInstance,
  computed, nextTick,
} from "vue";
import { useStore } from "vuex";
import { useRouter, useRoute } from "vue-router";
import { ElMessage, ElMessageBox } from "element-plus";

export default defineComponent({
  name: "",
  components: {
  },
  setup(props, {emit}) {
    const store = useStore();
    const that = (getCurrentInstance() as ComponentInternalInstance).appContext.config.globalProperties;
    const state = reactive({
      //  加载中
      loading: false,
      treeData: [],
      treeName: '',
      currentSource: <any>null,
      form: <any>{}
    });
    const ref_form = ref()
    //  获取字典
    const initDictionary = () => {
    }
    const ref_tree = ref()
    const handleNodeClick = (data) => {
      state.currentSource = data
      initTrack()
    }
    const treeFilter = (value, data) => {
      if (!value) return true
      return data.name.includes(value)
    }
    watch(() => state.treeName, (n) => {
      ref_tree.value.filter(n)
    })
    const initSource = () => {
      that.$api.shipTestSourceList().then(res => {
        state.treeData = res.data
      })
      nextTick(() => {
        ref_form.value?.reset()
      })
    }
    const initTrack = () => {
      that.$api.shipTestTrackInfoBySourceId(state.currentSource.id).then(res => {
        if (res.code === 200) {
          if (res.data?.[0]) {
            state.form = res.data[0]
          } else {
            state.form = {
              sourceId: state.currentSource.id
            }
          }
        } else {
          ElMessage.error(res.msg)
        }
      })
    }
    const onSubmit = () => {
      ref_form.value.submit().then(() => {
        ElMessageBox.confirm("是否提交?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          state.loading = true
          const apiHandle = state.form.id ? that.$api.shipTestTrackEdit(state.form) : that.$api.shipTestTrackAdd(state.form)
          apiHandle.then(res => {
            if (res.code === 200) {
              ElMessage.success(state.form.id ? '编辑成功' : '新增成功')
              initTrack()
            } else {
              ElMessage.error(res.msg)
            }
            state.loading = false
          }).catch(() => {
            state.loading = false
          })
        }).catch(() => {})
      }).catch((e) => {
        ElMessage({
          message: e[0].message,
          grouping: true,
          type: 'warning',
        })
      })
    }
    onMounted(() => {
      initDictionary()
      initSource()
    })
    return {
      ref_form,
      ...toRefs(state),
      handleNodeClick,
      treeFilter,
      ref_tree,
      onSubmit
    }
  },
});
</script>
<style scoped lang="scss">
.__normal-content {
  background-color: #FFFFFF;
  padding: 20px;
  .__cus-title_2 {
    margin-bottom: 10px;
  }
  .buttons {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
