<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="订单号" prop="postCode">
        <el-input
          v-model="queryParams.postCode"
          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="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" size="mini" @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="postList">
      <el-table-column label="订单号" align="center" prop="postCode" />
      <el-table-column label="订单类型" align="center" prop="postSort" />
      <el-table-column label="金额" align="center" prop="postSort" />
      <el-table-column label="数量" align="center" prop="postSort" />
      <el-table-column label="申请状态" align="center" prop="status">
      </el-table-column>
      <el-table-column
        label="充值时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            >发票申请</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="Delete"
            @click="handleDelete(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 :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="开票类型:" prop="type">
          <el-select v-model="form.type">
            <el-option label="普票" value="1"></el-option>
            <el-option label="专票" value="2"></el-option>
          </el-select>
        </el-form-item>
        <div style="margin-bottom: 22px"><b>开票信息</b></div>
        <el-form-item label="公司名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="公司税号:" prop="dutyParagraph">
          <el-input v-model="form.dutyParagraph" placeholder="请输入公司税号" />
        </el-form-item>
        <el-form-item label="开户行:" prop="bank" v-if="form.type == 2">
          <el-input v-model="form.bank" placeholder="请输入开户行" />
        </el-form-item>
        <el-form-item
          label="开户行账号:"
          prop="bankAccount"
          v-if="form.type == 2"
        >
          <el-input v-model="form.bankAccount" placeholder="请输入开户行账号" />
        </el-form-item>
        <el-form-item
          label="开户行电话:"
          prop="bankPhone"
          v-if="form.type == 2"
        >
          <el-input v-model="form.bankPhone" placeholder="请输入开户行电话" />
        </el-form-item>
        <el-form-item label="电子邮箱:" prop="email" v-if="form.type == 1">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="联系电话:" prop="phone" v-if="form.type == 1">
          <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.type == 2">
          <b>邮寄信息</b>
        </div>
        <el-form-item label="联系人:" prop="username" v-if="form.type == 2">
          <el-input v-model="form.username" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item
          label="联系人电话:"
          prop="userPhone"
          v-if="form.type == 2"
        >
          <el-input v-model="form.userPhone" placeholder="请输入联系人电话" />
        </el-form-item>
        <el-form-item
          label="邮寄地址:"
          prop="userAddress"
          v-if="form.type == 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 {
  listPost,
  addPost,
  delPost,
  getPost,
  updatePost,
} from "@/api/system/post";

const { proxy } = getCurrentInstance();

const postList = 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 dateRange = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
  },
  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);

/** 查询列表 */
function getList() {
  // console.log(proxy.addDateRange(queryParams.value, dateRange.value));
  postList.value = [1];
  total.value = 15;
  loading.value = false;
  // loading.value = true;
  // listPost(proxy.addDateRange(queryParams.value, dateRange.value)).then(
  //   (response) => {
  //     postList.value = response.rows;
  //     total.value = response.total;
  //     loading.value = false;
  //   }
  // );
}
/** 取消按钮 */
function cancel() {
  open.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,
  };
  proxy.resetForm("postRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.postId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  form.value.id = row.id;
  open.value = true;
  title.value = "发票申请";
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate((valid) => {
    if (valid) {
      console.log(form.value);
      if (form.value.id != undefined) {
        // updatePost(form.value).then((response) => {
        //   proxy.$modal.msgSuccess("修改成功");
        //   open.value = false;
        //   getList();
        // });
      } else {
        // addPost(form.value).then((response) => {
        //   proxy.$modal.msgSuccess("新增成功");
        //   open.value = false;
        //   getList();
        // });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value;
  proxy.$modal
    .confirm('是否确认删除订单号为"' + postIds + '"的数据项？')
    .then(function () {
      return delPost(postIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "system/post/export",
    {
      ...queryParams.value,
    },
    `post_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
