<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="资源类型" prop="category">
        <el-select
            v-model="queryParams.categoryId"
            placeholder="请选择资源类型"
            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="price">-->
      <!--        <el-input-->
      <!--            v-model="queryParams.price"-->
      <!--            placeholder="请输入算力金额"-->
      <!--            clearable-->
      <!--            @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="spuName"/>
      <el-table-column label="订单编号" align="center" prop="orderNo"/>
      <!--<el-table-column label="商品数量" align="center" prop="productCount"/>-->
      <el-table-column label="费用" align="center" prop="paymentPrice">
        <template #default="scope">
          {{ scope.row.paymentPrice / 100 }}
        </template>
      </el-table-column>
      <el-table-column label="资源类型" align="center" prop="categoryName"/>

      <el-table-column label="CPU" align="center" prop="cpu"/>
      <el-table-column label="GPU" align="center" prop="gpu"/>
      <el-table-column label="内存" align="center" prop="ram"/>
      <el-table-column label="存储" align="center" prop="storage"/>
      <el-table-column label="所在地" align="center" prop="location"/>
      <el-table-column label="订单状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="compute_resource_order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
<!--      <el-table-column label="发票链接" align="center" prop="invoiceUrl"/>-->
      <el-table-column label="开票状态" align="center" prop="invoiceStatus">
        <template #default="scope">
          <dict-tag :options="trade_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.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 发票预览弹窗 -->
    <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.orderNo }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">费用</div>
            <div class="value">{{ form.paymentPrice ? (form.paymentPrice / 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">
            <div class="label">CPU</div>
            <div class="value">{{ form.cpu || '-' }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">GPU</div>
            <div class="value">{{ form.gpu || '-' }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">内存</div>
            <div class="value">{{ form.ram || '-' }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">存储</div>
            <div class="value">{{ form.storage || '-' }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">服务器IP</div>
            <div class="value">{{ form.ip || '-' }}</div>
          </div>
          <div class="info-item flex-align-center flex-space-between">
            <div class="label">服务器所在地</div>
            <div class="value">{{ form.location || '-' }}</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="MyOrder">
import {
  listOrder
} from '@/api/computility/order'
import {orderInvoiceRequest, getCategories} from '@/api/computility/order'
import {parseTime} from "../../utils/ruoyi.js";
import {getResources} from "@/api/computility/resources.js";
import {ElMessage} from "element-plus";
import {InvoiceRequestEnum} from "@/utils/constants.js";

const {proxy} = getCurrentInstance()
const {trade_invoice_status, compute_resource_order_status} = proxy.useDict('trade_invoice_status', 'compute_resource_order_status')
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: {
    pageNum: 1,
    pageSize: 10,
    orderNo: null,
    categoryId: null,
    model: null,
    cpu: null,
    gpu: null,
    memory: null,
    storage: null,
    price: null,
    orderStatus: null,
    networkId: null,
    ipNum: null,
    vmStorage: null,
    submitTime: null,
    applyUser: null,
    useTime: 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 getList() {
  loading.value = true
  listOrder(queryParams.value).then((response) => {
    orderList.value = response.data.list
    total.value = response.data.total
    loading.value = false
  })
}


const applicationCategory = ref([])

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

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    orderNo: null,
    categoryId: null,
    model: null,
    cpu: null,
    gpu: null,
    memory: null,
    storage: null,
    price: null,
    orderStatus: null,
    networkId: null,
    ipNum: null,
    vmStorage: null,
    submitTime: null,
    applyUser: null,
    useTime: null,
    createTime: null,
    createBy: null,
    updateBy: null,
    updateTime: null,
    remark: null,
    delFlag: null
  }
  proxy.resetForm('orderRef')
}

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

/** 重置按钮操作 */
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) {
  orderInvoiceRequest(row).then((response) => {
    getList();
    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
}


getList()
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;
  }
}

/* Limit properties column to 200px width and 2 lines */
.properties-cell {
  width: 400px;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line: 2;
  -webkit-box-orient: vertical;
  white-space: normal;
}
</style>
