<template>
  <el-row>
    <el-col :span="1.5">
      <el-button plain class="el-btn-margin" type="primary"
                 @click="OpenAddAndUpdateView('save',
                 new BaseData<ShopExperience>('',ShopCommand.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.Description" label="商品描述"></el-table-column>
    <el-table-column prop="data.Price" label="商品价格"></el-table-column>
    <el-table-column prop="data.Items" label="解锁技能条目">
      <template #default="scope">
        <template v-for="item in scope.row.data.Items">
          <!--          <el-text>{{ item.Blueprint }}</el-text>-->
          <ArkItemText sourceType="blueprint" targetType="label" :value="item.Blueprint"/>
          <br>
        </template>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180">
      <template #default="scope">
        <el-button type="primary" link @click="OpenAddAndUpdateView('save',
                new BaseData<ShopCommand>(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-col :span="1.5">
        <el-button plain class="el-btn-margin" type="primary"
                   @click="AddItems()">
          添加
        </el-button>
      </el-col>
      <el-col :span="24">
        <el-table :data="shopItemForm.data.Items" border stripe>
          <el-table-column prop="Blueprint" label="图纸" width="120">
            <template #default="scope">
              <ArkItemText :value="scope.row.Blueprint"/>
            </template>
          </el-table-column>
          <el-table-column prop="Blueprint" label="图纸"></el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="scope">
              <el-button type="danger" link @click="shopItemForm.data.Items.splice(scope.$index, 1)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-form>
  </el-dialog>

</template>

<script setup lang="ts">

import {ref} from "vue";
import {BaseData} from "../api/ConfigFile";
import {
  DeleteShopItem,
  GetShopItems,
  SaveShopItem,
  ShopCommand,
  ShopDino,
  ShopExperience,
  ShopUnlockengram,
  ShopUnlockengramItem,
  UpdateShopItem
} from "../api/ARKShopItemAPI";
import {ElMessage} from "element-plus";
import ArkItemText from "./item/ArkItemText.vue";

const type = ref("unlockengram")
let shopItems = ref<BaseData<ShopUnlockengram>[]>([])

let shopItemForm = ref<BaseData<ShopUnlockengram>>(new BaseData<ShopUnlockengram>("", ShopDino.Create()))

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

function replay() {
  GetShopItems("unlockengram").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 ShopUnlockengram))
      }
    }
  })
}

replay()

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

function SubmitForm() {
  switch (itemStatus.value) {
    case "save":
      SaveShopItem(shopItemForm.value.data, shopItemForm.value.Name, type.value).then(
          res => {
            if (res.code == 200) {
              ElMessage.success("保存成功")
            } else {
              ElMessage.error(res.message)
            }
          }
      )
      break
    case "update":
      UpdateShopItem(shopItemForm.value.data, shopItemForm.value.Name, type.value).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 AddItems() {
  shopItemForm.value.data.Items.push(new ShopUnlockengramItem("", ''))
}

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>