<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          size="small"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="small" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="text">
          <b style="color: #000">总订单金额：￥2044</b>
        </el-button>
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="订单号" align="center" prop="orderNumber" />
      <el-table-column label="订单类型" align="center" prop="orderType">
        <template #default="{ row }">
          {{ orderTypeDic[row.orderType] }}
        </template>
      </el-table-column>
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="数量" align="center" prop="total" />
      <!-- <el-table-column label="申请状态" align="center" prop="status">
      </el-table-column> -->
      <el-table-column
        label="充值时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="Edit"
            @click="handleUpdate(row.id)"
            >发票申请</el-button
          >
          <el-button
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
            >删除</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="showEditDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="公司名称:" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司税号:" prop="taxNumber">
          <el-input v-model="form.taxNumber" placeholder="请输入公司税号" />
        </el-form-item>
        <!-- <el-form-item label="开户行:" prop="bank" v-if="form.orderType == 2">
          <el-input v-model="form.bank" placeholder="请输入开户行" />
        </el-form-item> -->
        <!-- <el-form-item
          label="开户行账号:"
          prop="bankAccount"
          v-if="form.orderType == 2"
        >
          <el-input v-model="form.bankAccount" placeholder="请输入开户行账号" />
        </el-form-item>
        <el-form-item
          label="开户行电话:"
          prop="bankPhone"
          v-if="form.orderType == 2"
        >
          <el-input v-model="form.bankPhone" placeholder="请输入开户行电话" />
        </el-form-item> -->
        <el-form-item label="电子邮箱:" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="联系电话:" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址:" prop="address">
          <el-input
            v-model="form.address"
            type="textarea"
            placeholder="请输入地址"
          />
        </el-form-item>
        <!-- <div style="margin-bottom: 22px" v-if="form.orderType == 2">
          <b>邮寄信息</b>
        </div>
        <el-form-item
          label="联系人:"
          prop="username"
          v-if="form.orderType == 2"
        >
          <el-input v-model="form.username" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item
          label="联系人电话:"
          prop="userPhone"
          v-if="form.orderType == 2"
        >
          <el-input v-model="form.userPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item
          label="邮寄地址:"
          prop="userAddress"
          v-if="form.orderType == 2"
        >
          <el-input
            v-model="form.userAddress"
            type="textarea"
            placeholder="请输入地址"
          />
        </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="Post">
import {
  casDealLogList,
  casDealLogDetail,
  updateCasDealLog,
  deleteCasDealLog,
} from "@/api/admin/enterprise";
import { ElMessage, ElMessageBox } from "element-plus";
import { reactive } from "vue";

const dataList = ref([]);
const showEditDialog = 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 dateRange = ref([]);
const orderTypeDic = reactive({
  1: "创新币充值",
  2: "活动报名",
});
const queryRef = ref();
const formRef = ref();
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    name: [{ required: true, message: "公司名称不能为空", trigger: "blur" }],
    dutyParagraph: [
      { required: true, message: "公司税号不能为空", trigger: "blur" },
    ],
    bank: [{ required: true, message: "开户行不能为空", trigger: "blur" }],
    bankAccount: [
      { required: true, message: "开户行账号不能为空", trigger: "blur" },
    ],
    bankPhone: [
      { required: true, message: "开户行电话不能为空", trigger: "blur" },
    ],
    email: [{ required: true, message: "电子邮箱不能为空", trigger: "blur" }],
    phone: [{ required: true, message: "联系电话不能为空", trigger: "blur" }],
    address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
    username: [{ required: true, message: "联系人不能为空", trigger: "blur" }],
    userPhone: [
      { required: true, message: "联系人电话不能为空", trigger: "blur" },
    ],
    userAddress: [
      { required: true, message: "邮寄地址不能为空", trigger: "blur" },
    ],
  },
});

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

/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const resp = await casDealLogList(queryParams.value);
  dataList.value = resp.rows;
  total.value = resp.total;
  loading.value = false;
};
/** 取消按钮 */
function cancel() {
  showEditDialog.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    // id: undefined,
    // type: "1",
    // name: undefined,
    // dutyParagraph: undefined,
    // bank: undefined,
    // bankAccount: undefined,
    // bankPhone: undefined,
    // email: undefined,
    // phone: undefined,
    // address: undefined,
    // username: undefined,
    // userPhone: undefined,
    // userAddress: undefined,
  };
  if (formRef.value) {
    formRef.value.resetFields();
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  if (queryRef.value) {
    queryRef.value.resetFields();
  }
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  showEditDialog.value = true;
  title.value = "";
}
/** 修改按钮操作 */
const handleUpdate = async (id) => {
  reset();
  const resp = await casDealLogDetail(id);
  form.value = resp.data;
  showEditDialog.value = true;
  title.value = "发票申请";
};
/** 提交按钮 */
const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id) {
    await updateCasDealLog(form.value);
    ElMessage.success("申请成功");
    showEditDialog.value = false;
    getList();
  } else {
  }
};
/** 删除按钮操作 */
const handleDelete = (id) => {
  const orderIds = id || ids.value;
  ElMessageBox.confirm('是否确认删除订单号为"' + orderIds + '"的数据项？').then(
    async () => {
      await deleteCasDealLog(id);
      getList();
      ElMessage.success("删除成功");
    }
  );
};
/** 导出按钮操作 */
// function handleExport() {
//   proxy.download(
//     "system/post/export",
//     {
//       ...queryParams.value,
//     },
//     `post_${new Date().getTime()}.xlsx`
//   );
// }

getList();
</script>
