<script setup lang="ts">
import { h, ref } from "vue";
import { PureTableBar } from "@/components/RePureTableBar";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { addDialog } from "@/components/ReDialog";
import { useCollaborationRecordHook } from "./utils/hook";
import { CollaborationRecordPageResponse } from "@/api/collaboration/record";
import RecordFormModal from "./record-form-modal.vue";

import AddFill from "@iconify-icons/ri/add-circle-line";
import Delete from "@iconify-icons/ep/delete";
import EditPen from "@iconify-icons/ep/edit-pen";
import Refresh from "@iconify-icons/ep/refresh";
import Search from "@iconify-icons/ep/search";

defineOptions({
  name: "CollaborationRecord"
});

const tableRef = ref();
const searchFormRef = ref();

const {
  searchFormParams,
  pageLoading,
  columns,
  dataList,
  pagination,
  defaultSort,
  deadlineRange,
  purchaseRange,
  optionMap,
  creatorOptions,
  isAdmin,
  getUserDisplayName,
  multipleSelection,
  onSearch,
  onSortChanged,
  getRecordList,
  resetForm,
  handleDelete,
  handleBulkDelete
} = useCollaborationRecordHook();

const recordFormRef = ref();

function openDialog(
  type: "add" | "update",
  row?: CollaborationRecordPageResponse
) {
  addDialog({
    title: type === "add" ? "新增合作记录" : "编辑合作记录",
    props: {
      type,
      row,
      optionMap
    },
    width: "1180px",
    top: "6vh",
    draggable: true,
    fullscreenIcon: true,
    closeOnClickModal: false,
    contentRenderer: () => h(RecordFormModal as any, { ref: recordFormRef }),
    beforeSure: async done => {
      const isSuccess = await recordFormRef.value.handleConfirm();
      if (isSuccess) {
        done();
        onSearch(tableRef);
      }
    }
  });
}
</script>

<template>
  <div class="main">
    <el-form
      ref="searchFormRef"
      :inline="true"
      :model="searchFormParams"
      class="search-form bg-bg_color w-[99/100] pl-8 pt-[12px]"
    >
      <el-form-item label="品牌" prop="brand">
        <el-input
          v-model="searchFormParams.brand"
          placeholder="请输入品牌"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item label="物品" prop="goods">
        <el-input
          v-model="searchFormParams.goods"
          placeholder="请输入物品"
          clearable
          class="!w-[180px]"
        />
      </el-form-item>
      <el-form-item v-if="isAdmin" label="创建者" prop="creatorId">
        <el-select
          v-model="searchFormParams.creatorId"
          placeholder="请选择创建者"
          clearable
          filterable
          class="!w-[160px]"
        >
          <el-option
            v-for="item in creatorOptions"
            :key="item.userId"
            :label="getUserDisplayName(item)"
            :value="item.userId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="合作平台" prop="cooperationPlatform">
        <el-select
          v-model="searchFormParams.cooperationPlatform"
          placeholder="请选择合作平台"
          clearable
          class="!w-[160px]"
        >
          <el-option
            v-for="item in optionMap.cooperationPlatform"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="预完成日期">
        <el-date-picker
          v-model="deadlineRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-[240px]"
        />
      </el-form-item>
      <el-form-item label="购入日期">
        <el-date-picker
          v-model="purchaseRange"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="!w-[240px]"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          :icon="useRenderIcon(Search)"
          :loading="pageLoading"
          @click="onSearch(tableRef)"
        >
          搜索
        </el-button>
        <el-button
          :icon="useRenderIcon(Refresh)"
          @click="resetForm(searchFormRef, tableRef)"
        >
          重置
        </el-button>
      </el-form-item>
    </el-form>

    <PureTableBar
      title="合作记录"
      :columns="columns"
      @refresh="onSearch(tableRef)"
    >
      <template #buttons>
        <el-button
          type="primary"
          :icon="useRenderIcon(AddFill)"
          @click="openDialog('add')"
        >
          新增合作
        </el-button>
        <el-button
          type="danger"
          :icon="useRenderIcon(Delete)"
          @click="handleBulkDelete(tableRef)"
        >
          批量删除
        </el-button>
      </template>
      <template v-slot="{ size, dynamicColumns }">
        <pure-table
          border
          ref="tableRef"
          align-whole="center"
          showOverflowTooltip
          table-layout="auto"
          :loading="pageLoading"
          :size="size"
          adaptive
          :data="dataList"
          :columns="dynamicColumns"
          :default-sort="defaultSort"
          :pagination="pagination"
          :paginationSmall="size === 'small'"
          :header-cell-style="{
            background: 'var(--el-table-row-hover-bg-color)',
            color: 'var(--el-text-color-primary)'
          }"
          @page-size-change="getRecordList"
          @page-current-change="getRecordList"
          @sort-change="onSortChanged"
          @selection-change="
            rows => (multipleSelection = rows.map(item => item.recordId))
          "
        >
          <template #operation="{ row }">
            <el-button
              class="reset-margin"
              link
              type="primary"
              :size="size"
              :icon="useRenderIcon(EditPen)"
              @click="openDialog('update', row)"
            >
              编辑
            </el-button>
            <el-popconfirm
              :title="`是否确认删除编号为${row.recordId}的合作记录`"
              @confirm="handleDelete(row)"
            >
              <template #reference>
                <el-button
                  class="reset-margin"
                  link
                  type="danger"
                  :size="size"
                  :icon="useRenderIcon(Delete)"
                >
                  删除
                </el-button>
              </template>
            </el-popconfirm>
          </template>
        </pure-table>
      </template>
    </PureTableBar>
  </div>
</template>

<style scoped lang="scss">
.search-form {
  :deep(.el-form-item) {
    margin-bottom: 12px;
  }
}
</style>
