<script setup>
import { reactive, ref, toRefs } from "vue";
import Pagination from "@/components/Pagination/index.vue";
import { applyBill, brokerDealLogList } from "@/api/admin/agent/order";
import { invoiceStatusDict, orderTypeDict } from "@/constant/dict";
import dayjs from "dayjs";
import { ElMessage } from "element-plus";
import DictTag from "@/components/DictTag/index.vue";
import RightToolbar from "@/components/RightToolbar/index.vue";

const showSearch = ref(true);
const tableData = ref([]); // 表格数据
const total = ref(0); // 总条数
const loading = ref(false); // 遮罩层
const dialogVisible = ref(false); // 是否显示弹出层
const dialogTitle = ref(""); // 弹出层标题
const formRef = ref(null);
const queryRef = ref(null);
const ids = ref([]); // 批量删除的id集合
const data = reactive({
  form: {}, // 表单数据
  queryParams: {
    // 查询条件
    pageSize: 10,
    pageNum: 1,
    timeRange: [],
  },
  rules: {
    // 表单验证规则
    orderNumber: [{ required: true, message: "请输入订单号", trigger: "blur" }],
    companyName: [
      { required: true, message: "请输入公司名称", trigger: "blur" },
    ],
    // 税号
    taxNumber: [{ required: true, message: "请输入公司税号", trigger: "blur" }],
    email: [
      { required: true, message: "请输入电子邮箱", trigger: "blur" },
      // email 正则验证
      {
        pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/,
        message: "请输入正确的邮箱",
        trigger: "blur",
      },
    ],
    phone: [{ required: true, message: "请输入电话", trigger: "blur" }],
  },
});
const { form, queryParams, rules } = toRefs(data); // 解构响应式对象

/**
 * 获取列表数据
 */
const getList = () => {
  brokerDealLogList(queryParams.value).then((resp) => {
    tableData.value = resp.rows;
    total.value = resp.total;
  });
};

/**
 * 重置查询条件
 */
const resetQuery = () => {
  queryRef.value.resetFields();
  handleQuery();
};

/**
 * 查询列表数据
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/**
 * 申请开票
 * @param row
 */
const handleApplyInvoice = (row) => {
  reset();
  form.value.id = row.id;
  console.log(row.value);
  dialogTitle.value = "申请开票";
  dialogVisible.value = true;
};

/**
 * 重置表单数据
 */
const reset = () => {
  form.value = {};
  formRef.value?.resetFields();
};

/**
 * 删除一条记录
 * @param id
 */
const handleDelete = (id) => {
  // const ids = id || ids.value
};

/**
 * 取消
 * @param id
 */
const handleCancel = (id) => {
  reset();
  dialogVisible.value = false;
};

/**
 * 提交表单
 */
const submit = () => {
  formRef.value.validate().then((valid) => {
    if (valid) {
      applyBill(form.value).then(() => {
        dialogVisible.value = false;
        ElMessage.success("申请成功");
        getList();
      });
    }
  });
};

const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
};

getList();
</script>

<template>
  <div class="app-container">
    <!--   搜索 订单号 查询  -->
    <el-form
      v-show="showSearch"
      ref="queryRef"
      :model="queryParams"
      class="query-form"
      inline
      @submit.prevent
      @keydown="handleQuery"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row class="mb8">
      <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
    </el-row>
    <!--    表格数据-->
    <el-table size="large" v-loading="loading" :data="tableData">
      <el-table-column
        align="center"
        label="订单号"
        prop="orderNumber"
      ></el-table-column>
      <el-table-column align="center" label="订单类型" prop="orderType">
        <template #default="{ row }">
          {{
            orderTypeDict.find((item) => item.value === row.orderType)?.label ??
            "-"
          }}
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="金额"
        prop="amount"
      ></el-table-column>
      <el-table-column
        align="center"
        label="数量"
        prop="total"
      ></el-table-column>
      <el-table-column align="center" label="充值时间">
        <template #default="{ row }">
          {{ dayjs(row.payTime).format("YYYY-MM-DD HH:mm:ss") }}
        </template>
      </el-table-column>
      <!--      状态-->
      <el-table-column align="center" label="状态" prop="status">
        <template #default="{ row }">
          <dict-tag :options="invoiceStatusDict" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="{ row }">
          <el-button
            :disabled="row.status !== '0'"
            type="text"
            @click="handleApplyInvoice(row)"
            >发票申请
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      v-model:limit="queryParams.pageSize"
      v-model:page="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!--    申请开票dialog-->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      append-to-body
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="form"
        :rules="rules"
        label-position="left"
        label-width="100px"
      >
        <el-form-item label="公司名称" prop="companyName">
          <el-input v-model="form.companyName"></el-input>
        </el-form-item>
        <el-form-item label="公司税号" prop="taxNumber">
          <el-input v-model="form.taxNumber"></el-input>
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="default" @click="handleCancel">取消</el-button>
          <el-button type="primary" @click="submit">提交</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss" scoped>
//:deep(.el-table--large) {
//font-size: 18px;
//th {
//font-size: 18px;
//}
//}
</style>
