<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="categoryId">
        <el-select
            v-model="queryParams.categoryId"
            placeholder="请选择API类型"
            clearable
            class="custom-select"

        >
          <el-option
              v-for="item in applicationCategory"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          />
        </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="请输入应用名称/订单编号"
                  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="应用名称" 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">
        <template #default="scope">
          {{ (scope.row.packageValidDays) }}天
        </template>
      </el-table-column>
      <el-table-column label="实付金额" align="center" prop="costPrice">
        <template #default="scope">
          {{ (scope.row.costPrice / 100).toFixed(2) }}元
        </template>
      </el-table-column>
      <el-table-column label="资源类别" align="center" prop="categoryName"/>
<!--      <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="invoiceStatusName">
        <template #default="scope">
          <!--          <dict-tag :type="invoice_status" :value="scope.row.invoiceStatus" />-->
        </template>
      </el-table-column>
      <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
              v-if="scope.row.invoiceStatus === InvoiceRequestEnum.UNINVOICED.type"
              link
              type="primary"
              @click="invoiceRequest(scope.row)"
          >
            申请开票
          </el-button>

          <el-button
              v-if="scope.row.invoiceUrl"
              link
              type="primary"
              @click="viewInvoice(scope.row)"
          >
            查看发票
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNo"
        v-model:limit="queryParams.pageSize"
        @pagination="loadList"
    />
    <!-- 发票预览弹窗 -->
    <el-dialog
        v-model="pdfDialogVisible"
        title="发票预览"
        width="80%"
        top="5vh"
    >
      <iframe
          v-if="invoiceUrl"
          :src="invoiceUrl"
          style="width: 100%; height: 80vh; border: none;"
      />
    </el-dialog>

    <!-- 添加或修改订单管理-需求单管理对话框 -->
    <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";
import {InvoiceRequestEnum} from "@/utils/constants.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
  })
}

import { getApiCategories } from '@/api/apicategory.js'
import {appOrderInvoiceRequest} from "@/api/apiorder.js";

const applicationCategory = ref([])

function loadCategories() {
  getApiCategories().then(res => {
    applicationCategory.value = res.data || []
  })
}


// 取消按钮
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 = '查看订单'
  // })
}


function invoiceRequest(row) {
  appOrderInvoiceRequest(row).then((response) => {
    loadList();
    proxy.$modal.msgSuccess("已成功申请");
  }).catch((r) => {
      }
  )
}


const pdfDialogVisible = ref(false)
const invoiceUrl = ref('')

function viewInvoice(row) {
  invoiceUrl.value = import.meta.env.VITE_APP_BASE_API+'/infra/file/preview?url=' + row.invoiceUrl // 从接口带回的 pdf 地址
  pdfDialogVisible.value = true
}


loadList()
loadCategories()
</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;
  }
}

/* 控制下拉列表文字左侧间距 */
.custom-select .el-select-dropdown__item {
  padding-left: 12px; /* 调整数值即可 */
}

</style>
