<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 label="操作" width="180">
      <template #default="scope">
        <el-button type="success" link @click="OpenShow(scope.row.data.Items)">查看物品</el-button>
        <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="open" width="1000px">
    <el-table :data="shopCommandItem" border stripe>
      <el-table-column prop="Command" label="命令"></el-table-column>
      <el-table-column prop="DisplayAs" label="显示为"></el-table-column>
    </el-table>
  </el-dialog>

  <el-dialog title="添加/修改命令" v-model="openView" width="800px">
    <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="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="Command" label="命令">
              <template #default="scope">
                <el-input v-model="scope.row.Command" label="命令"/>
              </template>
            </el-table-column>
            <el-table-column prop="DisplayAs" label="显示为">
              <template #default="scope">
                <el-input v-model="scope.row.DisplayAs" label="显示为"/>
              </template>
            </el-table-column>
          </el-table>
        </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,
  ShopCommand,
  ShopCommandItem,
  ShopExperience,
  UpdateShopItem
} from "../api/ARKShopItemAPI";
import {ElMessage} from "element-plus";

const type = ref("command")
let shopItems = ref<BaseData<ShopCommand>[]>([])
let open = ref<boolean>(false)
let shopCommandItem = ref<ShopCommandItem[]>([])

let openView = ref<boolean>(false)
let itemStatus = ref<string>("")
let shopItemForm = ref<BaseData<ShopCommand>>(
    new BaseData<ShopCommand>("", ShopCommand.Create()))

function OpenShow(item: ShopCommandItem[]) {
  open.value = true
  shopCommandItem.value = item
  console.log(open.value, item)
}

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

replay()

function OpenAddAndUpdateView(status: string, shopItem: BaseData<ShopCommand>) {
  openView.value = true
  itemStatus.value = status
  console.log(shopItem)
  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 ShopCommandItem("", ''))
}

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>