<template>
  <el-row>
    <el-col :span="1.5">
      <el-button plain class="el-btn-margin" type="primary"
                 @click="OpenAddAndUpdateView('save',
                 new BaseData<ShopDino>('', ShopDino.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.Level" label="等级"/>
    <el-table-column prop="data.MinLevel" label="最低等级"/>
    <el-table-column prop="data.MaxLevel" label="最高等级"/>
    <el-table-column prop="data.Neutered" label="是否被绝育"/>
    <el-table-column prop="data.Gender" label="性别"/>
    <el-table-column prop="data.SaddleBlueprint" label=" 鞍蓝图">
      <template #default="scope">
        <ArkItemText sourceType="blueprint" targetType="label" :value="scope.row.data.SaddleBlueprint"/>
      </template>
    </el-table-column>
    <el-table-column prop="data.Blueprint" label="蓝图">
      <template #default="scope">
        <ArkItemText sourceType="blueprint" targetType="label" :value="scope.row.data.Blueprint"/>
      </template>
    </el-table-column>
    <el-table-column label="操作" width="180">
      <template #default="scope">
        <el-button type="primary" link
                   @click="OpenAddAndUpdateView('save',
                   new BaseData<ShopDino>(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="shopDinoForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-text type="primary" label="类型">
              {{ getTypeNameByType(shopDinoForm.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="shopDinoForm.Name" label="名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="价格">
            <el-input-number v-model="shopDinoForm.data.Price" label="价格"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="描述">
            <el-input type="textarea" v-model="shopDinoForm.data.Description" label="描述"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-row>
        <el-col :span="8">
          <el-form-item label="等级">
            <el-input-number v-model="shopDinoForm.data.Level" label="等级"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="最低等级">
            <el-input-number v-model="shopDinoForm.data.MinLevel" label="最低等级"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="最高等级">
            <el-input-number v-model="shopDinoForm.data.MaxLevel" label="最高等级"/>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="是否被绝育">
            <el-switch v-model="shopDinoForm.data.Neutered" label="是否被绝育"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="性别">
            <el-select v-model="shopDinoForm.data.Gender" label="性别">
              <el-option label="雄性" value="male"/>
              <el-option label="雌性" value="female"/>
              <el-option label="随机" value="random"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="鞍蓝图">
            <ArkItemSelect keyType="blueprint" valueType="blueprint" labelType="label" v-model="shopDinoForm.data.SaddleBlueprint"/>
<!--            <el-input type="textarea" rows="3" v-model="shopDinoForm.data.SaddleBlueprint" label="鞍蓝图"/>-->
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="蓝图">
            <ArkDinoSelect keyType="blueprint" valueType="blueprint" labelType="label" v-model="shopDinoForm.data.Blueprint"/>
<!--            <el-input type="textarea" rows="3" v-model="shopDinoForm.data.Blueprint" 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, ShopDino, UpdateShopItem} from "../api/ARKShopItemAPI";
import {ElMessage} from "element-plus";
import ArkItemSelect from "./item/ArkItemSelect.vue";
import ArkDinoSelect from "./item/ArkDinoSelect.vue";
import ArkItemText from "./item/ArkItemText.vue";

let shopItems = ref<BaseData<ShopDino>[]>([])

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

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


function replay() {
  shopItems.value = []
  GetShopItems("dino").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 ShopDino))
      }
    }
  })
}

replay()

function OpenAddAndUpdateView(status: string, shopDino: BaseData<shopDino>) {
  openView.value = true
  itemStatus.value = status
  shopDinoForm.value = shopDino
}

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