<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="100px">
            <el-form-item label="分配前平台" prop="platformBefore">
              <el-select v-model="queryParams.platformBefore" clearable>
                <el-option v-for="dict in sys_platform_code" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="分配后平台" prop="platformAfter">
              <el-select v-model="queryParams.platformAfter" clearable>
                <el-option v-for="dict in sys_platform_code" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="分配时间" prop="dateRange">
              <el-date-picker v-model="dateRange" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8"> <el-col :span="18"> 订单分配记录 </el-col></el-row>
      </template>

      <el-table v-loading="loading" border :data="orderAssignmentList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" :resizable="false" />
        <el-table-column label="订单编号" align="center" prop="orderNo" :resizable="false" />
        <el-table-column label="分配前平台" align="center" prop="platformBefore" :resizable="false" />
        <el-table-column label="分配后平台" align="center" prop="platformAfter" :resizable="false" />
        <el-table-column label="分配时间" align="center" prop="assignTime" width="180" :resizable="false">
          <template #default="scope">
            <span>{{ scope.row.assignTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分配类型" align="center" prop="assignType" :resizable="false">
          <template #default="scope">
            <dict-tag :options="order_assign_type" :value="scope.row.assignType" />
          </template>
        </el-table-column>
        <el-table-column label="分配原因" align="center" prop="remark" :resizable="false" />
      </el-table>

      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改订单分配记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body> </el-dialog>
  </div>
</template>

<script setup name="OrderAssignment" lang="ts">
import { listOrderAssignment, getOrderAssignment, addOrderAssignment, updateOrderAssignment } from '@/api/order/orderAssignmentLog';
import { OrderAssignmentVO, OrderAssignmentQuery, OrderAssignmentForm } from '@/api/order/orderAssignmentLog/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_platform_code, order_assign_type } = toRefs<any>(proxy?.useDict('sys_platform_code', 'order_assign_type'));
const orderAssignmentList = ref<OrderAssignmentVO[]>([]);
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 dateRange = ref<[DateModelType, DateModelType]>(['', '']);
const queryFormRef = ref<ElFormInstance>();
const orderAssignmentFormRef = ref<ElFormInstance>();

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

const initFormData: OrderAssignmentForm = {
  id: undefined,
  orderId: undefined,
  platformBefore: undefined,
  platformAfter: undefined,
  assignedBy: undefined,
  assignTime: undefined,
  assignType: undefined,
  status: undefined,
  remark: undefined
};
const data = reactive<PageData<OrderAssignmentForm, OrderAssignmentQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderId: undefined,
    platformBefore: undefined,
    platformAfter: undefined,
    assignedBy: undefined,
    assignTime: undefined,
    assignType: undefined,
    status: undefined,
    platformCode: undefined,
    params: {}
  },
  rules: {
    platformAfter: [{ required: true, message: '分配后平台不能为空', trigger: 'blur' }]
  }
});

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

/** 查询订单分配记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOrderAssignment(proxy?.addDateRange(queryParams.value, dateRange.value));
  orderAssignmentList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: OrderAssignmentVO[]) => {
  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?: OrderAssignmentVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getOrderAssignment(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改订单分配记录';
};

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

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

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

onMounted(() => {
  getList();
});
</script>
