<template>
  <div class="p-2">
    <el-card shadow="never" class="order-set-card">
      <template #header>
        <div class="card-header">
          <span class="title">订单设置</span>
        </div>
      </template>

      <div class="order-set-container">
        <!-- 表单 -->
        <el-form ref="orderSetFormRef" :model="form" :rules="rules" label-width="140px" class="order-form">
          <div class="form-item-row">
            <el-form-item label="秒杀订单超过：" prop="seckillTimeout" class="form-item-input">
              <el-input v-model="form.seckillTimeout" placeholder="请输入" />
            </el-form-item>
            <div class="form-item-desc">未付款，订单自动关闭</div>
          </div>

          <div class="form-item-row">
            <el-form-item label="普通订单超过：" prop="normalTimeout" class="form-item-input">
              <el-input v-model="form.normalTimeout" placeholder="请输入" />
            </el-form-item>
            <div class="form-item-desc">未付款，订单自动关闭</div>
          </div>

          <div class="form-item-row">
            <el-form-item label="发货超过：" prop="shippingTimeout" class="form-item-input">
              <el-input v-model="form.shippingTimeout" placeholder="请输入" />
            </el-form-item>
            <div class="form-item-desc">未收货，订单自动完成</div>
          </div>

          <div class="form-item-row">
            <el-form-item label="订单完成超过：" prop="completeTimeout" class="form-item-input">
              <el-input v-model="form.completeTimeout" placeholder="请输入" />
            </el-form-item>
            <div class="form-item-desc">自动结束交易，不能申请售后</div>
          </div>

          <div class="form-item-row">
            <el-form-item label="订单评价超时：" prop="evaluationTimeout" class="form-item-input">
              <el-input v-model="form.evaluationTimeout" placeholder="请输入" />
            </el-form-item>
            <div class="form-item-desc">自动五星好评</div>
          </div>
        </el-form>
        <el-button type="primary" @click="submitForm" class="submit-btn">保存</el-button>
      </div>
    </el-card>
  </div>
</template>

<script setup name="OrderSet" lang="ts">
import { listOrderSet, getOrderSet, delOrderSet, addOrderSet, updateOrderSet, getOrderSetData } from '@/api/order/orderSet';
import { OrderSetVO, OrderSetQuery, OrderSetForm } from '@/api/order/orderSet/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const orderSetList = ref<OrderSetVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const orderSetFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: OrderSetForm = {
  id: undefined,
  seckillTimeout: undefined,
  normalTimeout: undefined,
  shippingTimeout: undefined,
  completeTimeout: undefined,
  evaluationTimeout: undefined,
  status: undefined,
  remark: undefined
};
const data = reactive<PageData<OrderSetForm, OrderSetQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    seckillTimeout: undefined,
    normalTimeout: undefined,
    shippingTimeout: undefined,
    completeTimeout: undefined,
    evaluationTimeout: undefined,
    status: undefined,
    platformCode: undefined,
    params: {}
  },
  rules: {}
});

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

/** 查询订单超时设置列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrderSet(queryParams.value);
  orderSetList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrderSetVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加订单超时设置';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: OrderSetVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getOrderSet(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改订单超时设置';
};

/** 提交按钮 */
const submitForm = () => {
  orderSetFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateOrderSet(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addOrderSet(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: OrderSetVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除订单超时设置编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delOrderSet(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/orderSet/export',
    {
      ...queryParams.value
    },
    `orderSet_${new Date().getTime()}.xlsx`
  );
};

onMounted(async () => {
  const res = await getOrderSetData();
  if (res && res.data) {
    Object.assign(form.value, res.data);
  }
});
</script>

<style scoped lang="scss">
.order-set-card {
  :deep(.el-card__header) {
    padding: 20px;
    border-bottom: 1px solid #ebeef5;
  }
}

.order-set-container {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 10px 0;

  .order-form {
    :deep(.el-form-item) {
      margin-bottom: 0;
    }

    :deep(.el-form-item__label) {
      font-weight: 500;
      color: #333;
    }

    :deep(.el-input__wrapper) {
      background-color: #fafafa;
      border: 1px solid #dcdfe6;
      border-radius: 4px;

      &:hover {
        border-color: #b1b3b9;
      }
    }

    :deep(.el-input.is-focus .el-input__wrapper),
    :deep(.el-input__wrapper.is-focus) {
      border-color: #409eff;
      background-color: #fff;
    }
  }

  .form-item-row {
    display: flex;
    align-items: center;
    gap: 40px;
    margin-bottom: 20px;

    .form-item-input {
      flex: 0 0 auto;
      width: 400px;

      :deep(.el-form-item__content) {
        line-height: 32px;
      }
    }

    .form-item-desc {
      flex: 1;
      color: #666;
      font-size: 14px;
      line-height: 32px;
    }
  }

  .submit-btn {
    margin-top: 10px;
    padding: 8px 24px;
    font-size: 14px;
    width: fit-content;
    margin-left: 140px;
  }
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .title {
    font-size: 16px;
    font-weight: 600;
    color: #333;
  }
}
</style>
