<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item :label="t('admin.form.orderNumber')" prop="orderNumber">
        <el-input
          v-model="queryParams.orderNumber"
          :placeholder="
            t('admin.form.placeholder', { type: t('admin.form.orderNumber') })
          "
          clearable
          size="small"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="t('admin.table.createTime')">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          :start-placeholder="t('admin.table.startDate')"
          :end-placeholder="t('admin.table.endDate')"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="Search"
          size="small"
          @click="handleQuery"
        >
          {{ t("admin.common.search") }}
        </el-button>
        <el-button icon="Refresh" size="small" @click="resetQuery">
          {{ t("admin.common.reset") }}
        </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="t('admin.form.orderNumber')"
        align="center"
        prop="orderNumber"
      />
      <el-table-column
        :label="t('admin.form.orderType')"
        align="center"
        prop="orderType"
      >
        <template #default="{ row }">
          {{
            t(
              `dict.${
                orderTypeDict.find((item) => item.value === row.orderType).i18n
              }`
            )
          }}
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.form.amount')"
        align="center"
        prop="amount"
      />
      <el-table-column
        :label="t('admin.form.quantity')"
        align="center"
        prop="total"
      />
      <!-- <el-zh-column label="申请状态" align="center" prop="status">
      </el-zh-column> -->
      <el-table-column
        :label="t('admin.form.rechargeTime')"
        align="center"
        prop="payTime"
        width="180"
      >
        <template #default="{ row }">
          <span>{{ parseTime(row.payTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        :label="t('admin.common.operation')"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-button
            size="small"
            type="text"
            icon="Edit"
            @click="handleUpdate(row.id)"
            >{{ t("admin.table.applyInvoice") }}
          </el-button>
          <el-button
            size="small"
            type="text"
            icon="Delete"
            @click="handleDelete(row.id)"
          >
            {{ t("admin.common.delete") }}
          </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="
            t('admin.form.name', {
              type: t('admin.common.company'),
            })
          "
          prop="companyName"
        >
          <el-input
            v-model="form.companyName"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.form.name', {
                  type: t('admin.common.company'),
                }),
              })
            "
          />
        </el-form-item>
        <el-form-item
          :label="t('admin.form.companyTaxNumber')"
          prop="taxNumber"
        >
          <el-input
            v-model="form.taxNumber"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.form.companyTaxNumber'),
              })
            "
          />
        </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="t('webContact.phone')" prop="phone">
          <el-input
            v-model="form.phone"
            :placeholder="t('webContact.phonePlaceholder')"
          />
        </el-form-item>
        <el-form-item :label="t('admin.form.email')" prop="email">
          <el-input
            v-model="form.email"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.form.email'),
              })
            "
          />
        </el-form-item>
        <!--    其他联系方式-->
        <el-form-item :label="t('admin.form.otherContact')" prop="otherContact">
          <el-input v-model="form.otherContact"></el-input>
        </el-form-item>
        <el-form-item :label="t('admin.form.address')" prop="address">
          <el-input
            v-model="form.address"
            type="textarea"
            :placeholder="
              t('admin.form.placeholder', {
                type: t('admin.form.address'),
              })
            "
          />
        </el-form-item>
        <!-- <div style="margin-bottom: 22px" v-if="form.orderType == 2">
          <b>邮寄信息</b>
        </div>
        <el-form-item
          :label="t('admin.form.contact')"
          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"
            >{{ t("admin.common.confirm") }}
          </el-button>
          <el-button @click="cancel">{{ t("admin.common.cancel") }}</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import {
  casDealLogDetail,
  casDealLogList,
  deleteCasDealLog,
  updateCasDealLog,
} from "@/api/admin/enterprise";
import { ElMessage } from "element-plus";
import modal from "@/plugins/modal";
import { computed, reactive, ref, toRefs } from "vue";
import { useI18n } from "vue-i18n";
import { orderTypeDict } from "../../../../constant/dict";

const { t } = useI18n();
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: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.name", {
              type: t("admin.common.company"),
            }),
          })
        ),
        trigger: "blur",
      },
    ],
    dutyParagraph: [
      {
        required: true,
        /*"公司税号不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.companyTaxNumber"),
          })
        ),
        trigger: "blur",
      },
    ],
    /*    bank: [{ required: true, message: "开户行不能为空", trigger: "blur" }],
    bankAccount: [
      { required: true, message: "开户行账号不能为空", trigger: "blur" },
    ],
    bankPhone: [
      { required: true, message: "开户行电话不能为空", trigger: "blur" },
    ],*/
    email: [
      {
        required: true,
        /*"电子邮箱不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.email"),
          })
        ),
        trigger: "blur",
      },
    ],
    phone: [
      {
        required: true,
        /*"联系电话不能为空"*/
        message: computed(() => t("webContact.phoneEmpty")),
        trigger: "blur",
      },
    ],
    address: [
      {
        required: true,
        /*"地址不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.address"),
          })
        ),
        trigger: "blur",
      },
    ],
    username: [
      {
        required: true,
        /*"联系人不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.contact"),
          })
        ),
        trigger: "blur",
      },
    ],
    userPhone: [
      {
        required: true,
        /*"联系人电话不能为空"*/
        message: computed(() =>
          t("admin.validation.required", {
            type: t("admin.form.contactPhone"),
          })
        ),
        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 = t("admin.table.invoiceApplication");
};

/** 提交按钮 */
const submitForm = async () => {
  await formRef.value.validate();
  if (form.value.id) {
    await updateCasDealLog(form.value);
    ElMessage.success(t("admin.common.applicationSuccess"));
    showEditDialog.value = false;
    getList();
  } else {
  }
};

/** 删除按钮操作 */
const handleDelete = (id) => {
  const orderIds = id || ids.value;
  modal
    .confirm(
      t("admin.common.confirmAction", {
        action: t("admin.common.delete"),
        type: t("admin.form.order"),
        number: orderIds,
      })
    )
    .then(async () => {
      await deleteCasDealLog(id);
      getList();
      ElMessage.success(t("admin.common.deleteSuccess"));
    });
};

/** 导出按钮操作 */
// function handleExport() {
//   proxy.download(
//     "system/post/export",
//     {
//       ...queryParams.value,
//     },
//     `post_${new Date().getTime()}.xlsx`
//   );
// }

getList();
</script>
