import request from '@/utils/request'
import { parseStrEmpty } from "@/utils/ruoyi";
import { createUniqueString } from "@/utils/index";

const tempTypeList = [
  { value: 'point', label: '点' },
	{ value: 'polyline', label: '线' },
  { value: 'polygon', label: '面' },
  { value: 'billboard', label: '图标' },
  { value: 'label', label: '标签' },
]
const tempList = []

// 查询样式类型信息
export function getStyleType() {
  return Promise.resolve(tempTypeList)
}

// 查询样式列表
export function listStyle(query) {
	const list = tempList.filter(item => {
    if(query.styleName && item.styleName.indexOf(query.styleName) === -1) {
      return false
    }
    if(query.styleType && item.styleType.indexOf(query.styleType) === -1) {
      return false
    }
    return true
	})
  return Promise.resolve({
		rows: list,
		total: list.length,
	})
}

// 查询样式详细
export function getStyle(styleId) {
  return Promise.resolve({
    data: tempList.find(item => item.id === parseStrEmpty(styleId))
  })
}

// 新增样式
export function addStyle(data) {
	data.id = createUniqueString()
  tempList.push(data)
  return Promise.resolve({
    code: 200,
    msg: '新增成功',
    data: data
  })
}

// 修改样式
export function updateStyle(data) {
  for (const v of tempList) {
    if (v.id === data.id) {
      v.styleName = data.styleName
      v.styleType = data.styleType
      v.style = data.style
      break
    }
  }
  return Promise.resolve({
    code: 200,
    msg: '修改成功',
  })
}

// 删除样式
export function delStyle(styleId) {
  let idList = styleId
  if(typeof styleId === 'string') {
    idList = [styleId]
  }
  for (let index = tempList.length - 1; index >= 0; index--) {
    if(idList.includes(tempList[index].id)) {
      tempList.splice(index, 1)
    }
  }
  return Promise.resolve({
    code: 200,
    msg: '删除成功',
  })
}