<template>
  <el-row>
    <el-col :span="1.5">
      <el-button plain class="el-btn-margin" type="primary"
                 @click="OpenAddAndUpdateView('save',
                 new BaseData<ShopItem>('',ShopItem.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<ShopItem>(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="showItem" border stripe>
      <el-table-column prop="Amount" label="数量" width="70"></el-table-column>
      <el-table-column prop="Blueprint" label="名称" width="100">
        <template #default="scope">
          <ArkItemText sourceType="blueprint" targetType="label" :value="scope.row.Blueprint"/>
        </template>
      </el-table-column>
      <el-table-column prop="Blueprint" label="蓝图"></el-table-column>
      <el-table-column prop="Fixed" label="固定" width="70"></el-table-column>
      <el-table-column prop="ForceBlueprint" label="强制" width="70"></el-table-column>
      <el-table-column prop="Quality" label="品质" width="70"></el-table-column>
    </el-table>
  </el-dialog>

  <el-dialog title="添加商品" v-model="openView" width="1100px">
    <el-form :model="shopItemsForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="类型">
            <el-text type="primary" label="类型">
              {{ getTypeNameByType(shopItemsForm.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="shopItemsForm.Name" label="名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="价格">
            <el-input-number v-model="shopItemsForm.data.Price" label="价格"/>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="描述">
            <el-input type="textarea" v-model="shopItemsForm.data.Description" label="描述"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-divider/>
      <el-form-item label="物品列表">
        <el-row>
          <el-col>
            <el-button class="el-btn-margin" type="primary" plain @click="AddItems()">添加物品</el-button>
          </el-col>
        </el-row>
        <el-table :data="shopItemsForm.data.Items" border stripe>
          <el-table-column prop="Amount" label="数量" width="175px">
            <template #default="scope">
              <el-input-number v-model="scope.row.Amount" label="数量"/>
            </template>
          </el-table-column>
          <el-table-column prop="Fixed" label="固定" width="85px">
            <template #default="scope">
              <el-switch v-model="scope.row.ForceBlueprint" label="强制">
                <template #open>
                  是
                </template>
                <template #close>
                  否
                </template>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="ForceBlueprint" label="强制" width="85px">
            <template #default="scope">
              <el-switch v-model="scope.row.ForceBlueprint" label="强制">
                <template #open>
                  是
                </template>
                <template #close>
                  否
                </template>
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column prop="Quality" label="品质" width="175px">
            <template #default="scope">
              <el-input-number v-model="scope.row.Quality" label="品质"/>
            </template>
          </el-table-column>
          <el-table-column prop="Blueprint" label="蓝图">
            <template #default="scope">
              <ArkItemSelect v-model="scope.row.Blueprint"/>
              <!--              <el-input v-model="scope.row.Blueprint" label="蓝图"/>-->
            </template>
          </el-table-column>
          <el-table-column label="删除">
            <template #default="scope">
              <el-button @click="shopItemsForm.data.Items.splice(scope.$index, 1)" type="danger" link>删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </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 {BaseData, Item} from "../api/ConfigFile";
import {DeleteShopItem, GetShopItems, SaveShopItem, ShopItem, UpdateShopItem} from "../api/ARKShopItemAPI";
import {ref} from "vue";
import {ElMessage} from "element-plus";
import ArkItemSelect from "./item/ArkItemSelect.vue";
import ArkItemText from "./item/ArkItemText.vue";

let shopItems = ref<BaseData<ShopItem>[]>([])
let open = ref<boolean>(false)
let showItem = ref<Item[]>([])
//表单更新，保存新数据
let itemStatus = ref<string>("")
let openView = ref<boolean>(false)

let shopItemsForm = ref<BaseData<ShopItem>>(new BaseData<ShopItem>("", ShopItem.Create()))

function OpenShow(item: Item[]) {
  open.value = true
  showItem.value = item
}

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

replay()

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

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

  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() {
  shopItemsForm.value.data.Items.push(new Item(0, '', false, false, 0))
}

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>