<template>
  <div class="app-container">
    <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
    >
      <!--      <el-form-item label="订单编号" prop="orderNo">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.orderNo"-->
      <!--            placeholder="请输入订单编号"-->
      <!--            clearable-->
      <!--            @keyup.enter="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="API类型" prop="category">
        <el-select
            v-model="queryParams.category"
            placeholder="请选择API类型"
            clearable
        >
          <el-option
              v-for="dict in application_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="型号" prop="model">-->
      <!--        <el-select-->
      <!--            v-model="queryParams.model"-->
      <!--            placeholder="请选择型号"-->
      <!--            clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--              v-for="dict in model"-->
      <!--              :key="dict.value"-->
      <!--              :label="dict.label"-->
      <!--              :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="CPU" prop="cpu">-->
      <!--        <el-select v-model="queryParams.cpu" placeholder="请选择CPU" clearable>-->
      <!--          <el-option-->
      <!--              v-for="dict in cpu_type"-->
      <!--              :key="dict.value"-->
      <!--              :label="dict.label"-->
      <!--              :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="GPU" prop="gpu">-->
      <!--        <el-select v-model="queryParams.gpu" placeholder="请选择GPU" clearable>-->
      <!--          <el-option-->
      <!--              v-for="dict in gpu_type"-->
      <!--              :key="dict.value"-->
      <!--              :label="dict.label"-->
      <!--              :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="内存" prop="memory">-->
      <!--        <el-select-->
      <!--            v-model="queryParams.memory"-->
      <!--            placeholder="请选择内存"-->
      <!--            clearable-->
      <!--        >-->
      <!--          <el-option-->
      <!--              v-for="dict in memory_type"-->
      <!--              :key="dict.value"-->
      <!--              :label="dict.label"-->
      <!--              :value="dict.value"-->
      <!--          />-->
      <!--        </el-select>-->
      <!--      </el-form-item>-->
            <el-form-item label="高级查询" prop="searchQuery">
              <el-input
                  v-model="queryParams.searchQuery"
                  placeholder="请输入api名称/订单编号"
                  clearable
                  style="width: 200px"
                  @keyup.enter="handleQuery"
              />
            </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
        v-loading="loading"
        :data="orderList"
        :max-height="620"
    >
      <!--<el-table-column type="selection" width="45" align="center"/>-->
      <!--<el-table-column label="序号" align="center" prop="id"/>-->
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <el-table-column label="api名称" align="center" prop="apiName"/>
      <el-table-column label="资源包" align="center" prop="packageName"/>
      <el-table-column label="使用次数" align="center" prop="packageTimes"/>
      <el-table-column label=有效期 align="center" prop="packageValidDays"/>
      <el-table-column label="实付金额" align="center" prop="costPrice">
        <template #default="scope">
          {{ scope.row.costPrice / 100 }}
        </template>
      </el-table-column>
<!--      <el-table-column label="商品类别" align="center" prop="category">-->
<!--        <template #default="scope">-->
<!--          <dict-tag-->
<!--              :options="application_category"-->
<!--              :value="scope.row.category"-->
<!--          />-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--          v-for="(item, index) in orderList[0]?.properties.length!==0?orderList[0]?.properties.slice(0,4):orderList[0]?.properties"-->
<!--          :key="index"-->
<!--          :label="item.propertyName"-->
<!--      >-->
<!--        <template #default="{ row }">-->
<!--          {{ row.properties[index].valueName }}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="订单状态" align="center" prop="statusName"/>
      <el-table-column label="下单时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
          label="操作"
          align="center"
          fixed="right"
          class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              link
              type="primary"
              @click="handleDetails(scope.row)"
          >
            详情
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改订单管理-需求单管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <div>
        <div class="info-block">
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">商品名称</div>
            <div class="value">{{ form.spuName }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">订单编号</div>
            <div class="value">{{ form.no }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">费用</div>
            <div class="value">{{ form.payPrice ? (form.payPrice / 100) : '-' }}</div>
          </div>

          <div class="info-item  flex-align-center flex-space-between">
            <div class="label">商品类别</div>
            <div class="value">{{ form.categoryName }}</div>
          </div>

        </div>

        <div class="info-block">
          <div class="info-item flex-align-center flex-space-between" v-for="(i,index) in form.properties"
               :key="i.index">
            <div class="label">{{ i.propertyName }}</div>
            <div class="value">{{ i.valueName }}</div>
          </div>
        </div>

        <div class="info-item  flex-align-center flex-space-between">
          <div class="label">订单状态</div>
          <div class="value">{{ form.statusName }}</div>
        </div>
        <div class="info-item  flex-align-center flex-space-between">
          <div class="label">下单时间</div>
          <div class="value">{{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</div>
        </div>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="cancel">关闭</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="ApiOrder">
import {
  getApiOrder
} from '@/api/apiorder.js'
import {parseTime} from "../../utils/ruoyi.js";

const {proxy} = getCurrentInstance()

const orderList = ref([])
const open = ref(false)
const sOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const total = ref(0)
const title = ref('')

const data = reactive({
  form: {},
  queryParams: {
    pageNo: 1,
    pageSize: 10,
    searchQuery: null,
    status: null
  },
  rules: {
    category: [
      {required: true, message: '商品类别不能为空', trigger: 'change'}
    ],
    model: [{required: true, message: '型号不能为空', trigger: 'change'}],
    cpu: [{required: true, message: 'CPU不能为空', trigger: 'change'}],
    gpu: [{required: true, message: 'GPU不能为空', trigger: 'change'}],
    memory: [{required: true, message: '内存不能为空', trigger: 'change'}],
    storage: [
      {required: true, message: '存储硬盘不能为空', trigger: 'change'}
    ],
    orderStatus: [
      {required: true, message: '状态不能为空', trigger: 'change'}
    ],
    networkId: [
      {required: true, message: '网络带宽不能为空', trigger: 'blur'}
    ],
    submitTime: [
      {required: true, message: '申请时间不能为空', trigger: 'blur'}
    ],
    applyUser: [{required: true, message: '应用人不能为空', trigger: 'blur'}],
    useTime: [{required: true, message: '使用期限不能为空', trigger: 'blur'}],
    createTime: [
      {required: true, message: '创建时间不能为空', trigger: 'blur'}
    ]
  }
})

const {queryParams, form, rules} = toRefs(data)

/** 查询订单 */
function loadList() {
  loading.value = true
  getApiOrder(queryParams.value).then((response) => {
    orderList.value = response.data.list
    total.value = response.data.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    searchQuery: null,
    status: null
  }
  proxy.resetForm('orderRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  loadList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}

function handleDetails(row) {
  const _id = row.id || ids.value
  // getResources({id: _id}).then(response => {
  //   form.value = response.data
  open.value = true
  title.value = '查看订单'
  // })
}

loadList()
</script>


<style lang="scss" scoped>
.el-form {
  padding: 20px 20px 0 20px;
  border-radius: 4px;
  background-color: #FFFFFF;
  margin-bottom: 20px;
}

.pagination-container {
  background-color: transparent;
}

.info-block {
  background-color: #ffffff;
  padding: 4px 0;
  //margin-bottom: 14px;
}

.info-item {
  padding: 12px 20px;

  .label {
    font-weight: 400;
    font-size: 16px;
    color: #626566;
  }

  .value {
    font-weight: bold;
    font-size: 16px;
    color: #303233;
  }
}
</style>
