<template>
  <el-row>
    <el-col :span="1.5">
      <el-button plain class="el-btn-margin" type="primary"
                 @click="OpenAddAndUpdateView('save',
                 new BaseData<ShopBeacon>('', ShopBeacon.Create()))">
        添加信标
      </el-button>
    </el-col>
  </el-row>

  <el-table :data="shopItems" border stripe>
    <el-table-column prop="Name" label="商品名称"></el-table-column>
    <el-table-column prop="data.Type" label="类型">
      <template #default="scope">
        {{ getTypeNameByType(scope.row.data.Type) }}
      </template>
    </el-table-column>
    <el-table-column prop="data.Description" label="商品描述"></el-table-column>
    <el-table-column prop="data.Price" label="商品价格"></el-table-column>
    <el-table-column prop="data.ClassName" label="类名"/>
    <el-table-column label="操作" width="180">
      <template #default="scope">
        <el-button type="primary" link
                   @click="OpenAddAndUpdateView('save',
                   new BaseData<ShopBeacon>(scope.row.Name,scope.row.data))">
          修改
        </el-button>
        <el-button type="danger" link @click="DeleteItem(scope.row.Name)">删除
        </el-button>
      </template>
    </el-table-column>
  </el-table>

  <el-dialog title="查看恐龙列表" v-model="openView" width="1100px">
    <el-form :model="shopItemForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-text type="primary" label="类型">
              {{ getTypeNameByType(shopItemForm.data.Type) }}
            </el-text>
          </el-form-item>
        </el-col>
        <el-col :span="12"></el-col>
        <el-col :span="8">
          <el-form-item label="名称">
            <el-input v-model="shopItemForm.Name" label="名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="价格">
            <el-input-number v-model="shopItemForm.data.Price" label="价格"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述">
            <el-input type="textarea" v-model="shopItemForm.data.Description" label="描述"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-row>
        <el-col :span="8">
          <el-form-item label="等级">
            <el-input v-model="shopItemForm.data.ClassName" label="类名"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="SubmitForm()">确 定</el-button>
        <el-button @click="openView=false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">

import {ref} from "vue";
import {BaseData} from "../api/ConfigFile";
import {DeleteShopItem, GetShopItems, SaveShopItem, ShopBeacon, ShopDino, UpdateShopItem} from "../api/ARKShopItemAPI";
import {ElMessage} from "element-plus";

let shopItems = ref<BaseData<ShopBeacon>[]>([])
let shopItemForm = ref<BaseData<ShopBeacon>>(new BaseData<ShopBeacon>("", ShopBeacon.Create()))

let itemStatus = ref<string>("")
let openView = ref(false)

function replay() {
  shopItems.value = []
  GetShopItems("beacon").then(res => {
    if (res.code == 200) {
      console.log(res.data)
      for (let item in res.data) {
        shopItems.value.push(new BaseData(item, res.data[item] as ShopBeacon))
      }
    }
  })
}

replay()


function OpenAddAndUpdateView(status: string, item: BaseData<ShopBeacon>) {
  openView.value = true
  itemStatus.value = status
  shopItemForm.value = item
}

function SubmitForm() {
  switch (itemStatus.value) {
    case "save":
      SaveShopItem(shopItemForm.value.data, shopItemForm.value.Name, "beacon").then(
          res => {
            if (res.code == 200) {
              ElMessage.success("保存成功")
            } else {
              ElMessage.error(res.message)
            }
          }
      )
      break
    case "update":
      UpdateShopItem(shopItemForm.value.data, shopItemForm.value.Name, "beacon").then(res => {
            if (res.code == 200) {
              ElMessage.success("修改成功")
            } else {
              ElMessage.error(res.message)
            }
          }
      )
      break
  }
  replay()
  openView.value = false
}

function DeleteItem(itemName: string) {
  DeleteShopItem(itemName).then(res => {
    if (res.code == 200) {
      ElMessage.success("删除成功")
    } else {
      ElMessage.error(res.message)
    }
  })
  replay()
}

function getTypeNameByType(type: string): string {
  switch (type) {
    case "item":
      return "物品"
    case "dino":
      return "恐龙"
    case "beacon":
      return "信标"
    case "experience":
      return "经验"
    case "unlockengram":
      return "技能点"
    case "command":
      return "命令"
    default:
      return ""
  }

}

</script>

<style scoped lang="scss">

</style>