<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="applyCode">
              <el-input v-model="queryParams.applyCode" placeholder="请输入申请单号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出差类型" prop="tripType">
              <el-select v-model="queryParams.tripType" placeholder="请选择出差类型" clearable>
                <el-option v-for="dict in trip_type" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="申请人姓名" prop="applicantName">
              <el-input v-model="queryParams.applicantName" placeholder="请输入申请人姓名" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请人部门" prop="deptName">
              <el-input v-model="queryParams.deptName" placeholder="请输入申请人部门" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="出差地点" prop="tripLocation">
              <el-input v-model="queryParams.tripLocation" placeholder="请输入出差地点" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="开始日期" prop="startTime">
              <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择开始日期" />
            </el-form-item>
            <el-form-item label="结束日期" prop="endTime">
              <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择结束日期" />
            </el-form-item>
            <el-form-item label="业务方向" prop="businessDirection">
              <el-input v-model="queryParams.businessDirection" placeholder="请输入业务方向" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="申请状态" prop="tripStatus">
              <el-select v-model="queryParams.tripStatus" placeholder="请选择申请状态" clearable>
                <el-option v-for="dict in trip_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </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="1.5">
            <el-dropdown trigger="click" @command="handleAdd">
              <el-button type="primary" plain icon="Plus" v-hasPermi="['oa/crm:businessTripApply:add']">
                新增<el-icon class="el-icon--right"><arrow-down /></el-icon>
              </el-button>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item v-for="dict in trip_type" :key="dict.value" :command="dict.value">
                    {{ dict.label }}
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['oa/crm:businessTripApply:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['oa/crm:businessTripApply:remove']"
              >删除</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['oa/crm:businessTripApply:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="businessTripApplyList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="申请ID" align="center" prop="tripId" v-if="columns[0].visible" />
        <el-table-column label="申请单号" align="center" prop="applyCode" v-if="columns[2].visible" />
        <el-table-column label="出差类型" align="center" prop="tripType" v-if="columns[3].visible">
          <template #default="scope">
            <dict-tag :options="trip_type" :value="scope.row.tripType" />
          </template>
        </el-table-column>
        <el-table-column label="申请人ID" align="center" prop="applicantId" v-if="columns[4].visible" />
        <el-table-column label="申请人" align="center" prop="applicantName" v-if="columns[5].visible" />
        <el-table-column label="申请人部门ID" align="center" prop="deptId" v-if="columns[6].visible" />
        <el-table-column label="申请人部门" align="center" prop="deptName" v-if="columns[7].visible" width="120" />
        <el-table-column label="出差地点" align="center" prop="tripLocation" v-if="columns[8].visible" />
        <el-table-column label="开始日期" align="center" prop="startTime" width="100" v-if="columns[9].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="结束日期" align="center" prop="endTime" width="100" v-if="columns[10].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="时长(天)" align="center" prop="durationDays" v-if="columns[11].visible" />
        <el-table-column label="出差事由" align="center" prop="tripReason" v-if="columns[12].visible" />
        <el-table-column label="项目ID" align="center" prop="projectId" v-if="columns[13].visible" />
        <!-- <el-table-column label="客户ID" align="center" prop="customerId" v-if="columns[14].visible" /> -->
        <el-table-column label="交流对象" align="center" prop="exchangeObject" v-if="columns[15].visible" />
        <el-table-column label="业务方向" align="center" prop="businessDirection" v-if="columns[16].visible" />
        <el-table-column label="交流目的" align="center" prop="exchangePurpose" v-if="columns[17].visible" />
        <el-table-column label="交流过程简述" align="center" prop="exchangeProcess" v-if="columns[18].visible" />
        <el-table-column label="会议/展会名称" align="center" prop="meetingName" v-if="columns[19].visible" />
        <el-table-column label="结果反馈" align="center" prop="feedback" v-if="columns[20].visible" />
        <el-table-column label="申请状态" align="center" prop="tripStatus" v-if="columns[21].visible">
          <template #default="scope">
            <dict-tag :options="trip_status" :value="scope.row.tripStatus" />
          </template>
        </el-table-column>
        <el-table-column label="流程状态" align="center" prop="flowStatus" v-if="columns[22].visible" />
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[23].visible" />
        <!-- <el-table-column label="附件ID" align="center" prop="ossId" v-if="columns[24].visible" /> -->
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip
              content="修改"
              placement="top"
              v-if="scope.row.flowStatus === 'draft' || scope.row.flowStatus === 'back' || !scope.row.flowStatus"
            >
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['oa/crm:businessTripApply:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="查看详情" placement="top" v-if="scope.row.flowStatus !== 'draft' && scope.row.flowStatus">
              <el-button link type="info" icon="DocumentChecked" @click="handleView(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['oa/crm:businessTripApply:remove']"
              ></el-button>
            </el-tooltip>
          </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-card>
  </div>
</template>

<script setup name="BusinessTripApply" lang="ts">
import { listBusinessTripApply, delBusinessTripApply } from '@/api/oa/crm/businessTripApply';
import { BusinessTripApplyVO, BusinessTripApplyQuery } from '@/api/oa/crm/businessTripApply/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { trip_status, trip_type } = toRefs<any>(proxy?.useDict('trip_status', 'trip_type'));

const businessTripApplyList = ref<BusinessTripApplyVO[]>([]);
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 router = useRouter();

const queryFormRef = ref<ElFormInstance>();

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `申请ID`, visible: false },
  { key: 1, label: `租户编号`, visible: false },
  { key: 2, label: `申请单号`, visible: true },
  { key: 3, label: `出差类型`, visible: true },
  { key: 4, label: `申请人ID`, visible: false },
  { key: 5, label: `申请人姓名`, visible: true },
  { key: 6, label: `申请人部门ID`, visible: false },
  { key: 7, label: `申请人部门名称`, visible: true },
  { key: 8, label: `出差地点`, visible: true },
  { key: 9, label: `开始日期`, visible: true },
  { key: 10, label: `结束日期`, visible: true },
  { key: 11, label: `时长(天)`, visible: true },
  { key: 12, label: `出差事由`, visible: true },
  { key: 13, label: `项目ID`, visible: false },
  { key: 14, label: `客户ID`, visible: false },
  { key: 15, label: `交流对象`, visible: true },
  { key: 16, label: `业务方向`, visible: true },
  { key: 17, label: `业务方向`, visible: true },
  { key: 18, label: `交流过程简述`, visible: true },
  { key: 19, label: `会议/展会名称`, visible: true },
  { key: 20, label: `结果反馈`, visible: true },
  { key: 21, label: `申请状态`, visible: true },
  { key: 22, label: `流程状态`, visible: true },
  { key: 23, label: `备注`, visible: true },
  { key: 24, label: `附件ID`, visible: false },
  { key: 25, label: `删除标志`, visible: true },
  { key: 26, label: `创建部门`, visible: true },
  { key: 27, label: `创建者`, visible: true },
  { key: 28, label: `创建时间`, visible: true },
  { key: 29, label: `更新者`, visible: true },
  { key: 30, label: `更新时间`, visible: true }
]);

const data = reactive<PageData<any, BusinessTripApplyQuery>>({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    applyCode: undefined,
    tripType: undefined,
    applicantName: undefined,
    deptName: undefined,
    tripLocation: undefined,
    startTime: undefined,
    endTime: undefined,
    businessDirection: undefined,
    tripStatus: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams } = toRefs(data);

/** 查询出差申请列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBusinessTripApply(queryParams.value);
  businessTripApplyList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

/** 新增按钮操作 */
const handleAdd = (command: string) => {
  router.push({
    path: '/oa/crm/businessTripApply/edit',
    query: { type: 'add', tripType: command }
  });
};

/** 修改按钮操作 */
const handleUpdate = (row?: BusinessTripApplyVO) => {
  const _tripId = row?.tripId || ids.value[0];
  router.push({
    path: '/oa/crm/businessTripApply/edit',
    query: { id: _tripId, type: 'update' }
  });
};

/** 查看按钮操作 */
const handleView = (row: BusinessTripApplyVO) => {
  const _tripId = row?.tripId;
  router.push({
    path: '/oa/crm/businessTripApply/edit',
    query: { id: _tripId, type: 'view' }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: BusinessTripApplyVO) => {
  const _tripIds = row?.tripId || ids.value;
  await proxy?.$modal.confirm('是否确认删除出差申请编号为"' + _tripIds + '"的数据项？').finally(() => (loading.value = false));
  await delBusinessTripApply(_tripIds);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

onMounted(() => {
  getList();
});

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