<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="订单号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入订单号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
<!--      <el-form-item label="单号" prop="outTradeNo">-->
<!--        <el-input-->
<!--          v-model="queryParams.outTradeNo"-->
<!--          placeholder="请输入传微信单号"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="二维码标识" prop="qrcodeId">-->
<!--        <el-input-->
<!--          v-model="queryParams.qrcodeId"-->
<!--          placeholder="请输入二维码标识"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="电子卡" prop="cardId">-->
<!--        <el-input-->
<!--          v-model="queryParams.cardId"-->
<!--          placeholder="请输入电子卡"-->
<!--          clearable-->
<!--          @keyup.enter="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="卡号" prop="cardNo">
        <el-input
          v-model="queryParams.cardNo"
          placeholder="请输入卡号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年份" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="年份"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择状态"
          clearable
          style="width: 180px"
        >
          <el-option
            v-for="dict in order_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)"
          />
        </el-select>
      </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-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['dz:orders:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dz:orders:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dz:orders:remove']"
        >删除</el-button>
      </el-col> -->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="Download"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['dz:orders:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
<!--      <el-table-column label="订单号" align="center" prop="code" />-->
<!--      <el-table-column label="二维码标识" align="center" prop="qrcodeId" />-->
<!--      <el-table-column label="${comment}" align="center" prop="type" />-->
      <el-table-column label="卡号" align="center" prop="cardNo">
        <template #default="scope">
          <span>{{ scope.row.cardId ? `${scope.row.cardId}-${scope.row.cardNo || ''}` : (scope.row.cardNo || '-') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="年份" align="center" prop="year" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      
      <el-table-column label="用户ID" align="center" prop="customerCode" />
<!--      <el-table-column label="失效期" align="center" prop="outTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.outTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="单价" align="center" prop="price" />-->
<!--      <el-table-column label="数量" align="center" prop="num" />-->
<!--      <el-table-column label="金额" align="center" prop="fee" />-->
<!--      <el-table-column label="应付金额" align="center" prop="totalFee" />-->
      <el-table-column label="金额(分)" align="center" prop="payFee" />
      <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" prop="creator" />-->
<!--      <el-table-column label="描述" align="center" prop="body" />-->
<!--      <el-table-column label="详情" align="center" prop="detail" />-->
<!--      <el-table-column label="附加数据" align="center" prop="attach" />-->
<!--      <el-table-column label="微信预支付标识" align="center" prop="prepayId" />-->
<!--      <el-table-column label="支付者" align="center" prop="payer" />-->
<!--      <el-table-column label="支付时间" align="center" prop="payTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="支付回调结果" align="center" prop="feedBack" />-->
<!--      <el-table-column label="确认时间" align="center" prop="syncTime" width="180">-->
<!--        <template #default="scope">-->
<!--          <span>{{ parseTime(scope.row.syncTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="outTradeNo" />
      <el-table-column label="微信单号" align="center" prop="transactionId" />
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template #default="scope">-->
<!--          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['dz:orders:edit']">修改</el-button>-->
<!--          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['dz:orders:remove']">删除</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>
      <el-form ref="ordersRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单号" prop="code">
          <el-input v-model="form.code" placeholder="请输入订单号" />
        </el-form-item>
        <el-form-item label="传微信单号" prop="outTradeNo">
          <el-input v-model="form.outTradeNo" placeholder="请输入传微信单号" />
        </el-form-item>
        <el-form-item label="二维码标识" prop="qrcodeId">
          <el-input v-model="form.qrcodeId" placeholder="请输入二维码标识" />
        </el-form-item>
        <el-form-item label="电子卡" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入电子卡" />
        </el-form-item>
        <el-form-item label="账号" prop="cardNo">
          <el-input v-model="form.cardNo" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="价目标识" prop="year">
          <el-input v-model="form.year" placeholder="请输入价目标识" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input v-model="form.phonenumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="微信订单号" prop="transactionId">
          <el-input v-model="form.transactionId" placeholder="请输入微信订单号" />
        </el-form-item>
        <el-form-item label="用户" prop="customerCode">
          <el-input v-model="form.customerCode" placeholder="请输入用户" />
        </el-form-item>
        <el-form-item label="失效期" prop="outTime">
          <el-date-picker clearable
            v-model="form.outTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择失效期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="金额" prop="fee">
          <el-input v-model="form.fee" placeholder="请输入金额" />
        </el-form-item>
        <el-form-item label="应付金额" prop="totalFee">
          <el-input v-model="form.totalFee" placeholder="请输入应付金额" />
        </el-form-item>
        <el-form-item label="实付金额" prop="payFee">
          <el-input v-model="form.payFee" placeholder="请输入实付金额" />
        </el-form-item>
        <el-form-item label="创建者" prop="creator">
          <el-input v-model="form.creator" placeholder="请输入创建者" />
        </el-form-item>
        <el-form-item label="描述" prop="body">
          <el-input v-model="form.body" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="详情" prop="detail">
          <el-input v-model="form.detail" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="附加数据" prop="attach">
          <el-input v-model="form.attach" placeholder="请输入附加数据" />
        </el-form-item>
        <el-form-item label="微信预支付标识" prop="prepayId">
          <el-input v-model="form.prepayId" placeholder="请输入微信预支付标识" />
        </el-form-item>
        <el-form-item label="支付者" prop="payer">
          <el-input v-model="form.payer" placeholder="请输入支付者" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable
            v-model="form.payTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="支付回调结果" prop="feedBack">
          <el-input v-model="form.feedBack" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="确认时间" prop="syncTime">
          <el-date-picker clearable
            v-model="form.syncTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态" clearable style="width: 100%">
            <el-option
              v-for="dict in order_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Orders">
import { listOrders, getOrders, delOrders, addOrders, updateOrders } from "@/api/dz/orders"

const { proxy } = getCurrentInstance()

// 获取字典数据
const { order_status } = proxy.useDict("order_status")

const ordersList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    code: null,
    outTradeNo: null,
    qrcodeId: null,
    type: null,
    cardId: null,
    cardNo: null,
    year: null,
    phonenumber: null,
    transactionId: null,
    customerCode: null,
    outTime: null,
    price: null,
    num: null,
    fee: null,
    totalFee: null,
    payFee: null,
    createTime: null,
    creator: null,
    body: null,
    detail: null,
    attach: null,
    prepayId: null,
    payer: null,
    payTime: null,
    feedBack: null,
    syncTime: null,
    status: null
  },
  rules: {
    code: [
      { required: true, message: "订单号不能为空", trigger: "blur" }
    ],
    year: [
      { required: true, message: "价目标识不能为空", trigger: "blur" }
    ],
    price: [
      { required: true, message: "单价不能为空", trigger: "blur" }
    ],
    num: [
      { required: true, message: "数量不能为空", trigger: "blur" }
    ],
    fee: [
      { required: true, message: "金额不能为空", trigger: "blur" }
    ],
    totalFee: [
      { required: true, message: "应付金额不能为空", trigger: "blur" }
    ],
    payFee: [
      { required: true, message: "实付金额不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态不能为空", trigger: "change" }
    ]
  }
})

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

/** 查询订单列表 */
function getList() {
  loading.value = true
  listOrders(queryParams.value).then(response => {
    ordersList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

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

// 表单重置
function reset() {
  form.value = {
    id: null,
    code: null,
    outTradeNo: null,
    qrcodeId: null,
    type: null,
    cardId: null,
    cardNo: null,
    year: null,
    phonenumber: null,
    transactionId: null,
    customerCode: null,
    outTime: null,
    price: null,
    num: null,
    fee: null,
    totalFee: null,
    payFee: null,
    createTime: null,
    creator: null,
    body: null,
    detail: null,
    attach: null,
    prepayId: null,
    payer: null,
    payTime: null,
    feedBack: null,
    syncTime: null,
    status: null
  }
  proxy.resetForm("ordersRef")
}

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

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

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加订单"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getOrders(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改订单"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["ordersRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addOrders(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除订单编号为"' + _ids + '"的数据项？').then(function() {
    return delOrders(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('dz/orders/export', {
    ...queryParams.value
  }, `orders_${new Date().getTime()}.xlsx`)
}

getList()
</script>
