<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="applicantId">
              <el-select v-model="queryParams.applicantId" placeholder="请选择申请人" clearable filterable style="width: 200px">
                <el-option v-for="user in userList" :key="user.userId" :label="user.nickName" :value="user.userId" />
              </el-select>
            </el-form-item>
            <el-form-item label="申请人部门" prop="deptId">
              <el-select v-model="queryParams.deptId" placeholder="请选择部门" clearable filterable style="width: 200px">
                <el-option v-for="dept in deptList" :key="dept.deptId" :label="dept.deptName" :value="dept.deptId" />
              </el-select>
            </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-select v-model="queryParams.businessDirection" placeholder="请选择业务方向" clearable filterable style="width: 200px">
                <el-option v-for="dict in business_direction" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </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" width="80" v-if="columns[0].visible" />
        <el-table-column label="申请单号" align="center" prop="applyCode" width="130" v-if="columns[2].visible" />
        <el-table-column label="出差类型" align="center" prop="tripType" width="100" 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" width="80" v-if="columns[4].visible" />
        <el-table-column label="申请人" align="center" prop="applicantName" width="100" v-if="columns[5].visible" />
        <el-table-column label="申请人部门ID" align="center" prop="deptId" width="100" 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" width="150" 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" width="80" v-if="columns[11].visible" />
        <el-table-column label="出差事由" align="center" prop="tripReason" width="200" show-overflow-tooltip v-if="columns[12].visible" />
        <el-table-column label="项目ID" align="center" prop="projectId" width="80" 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="customerName" width="150" show-overflow-tooltip v-if="columns[15].visible" />
        <el-table-column label="业务方向" align="center" prop="businessDirection" width="120" v-if="columns[16].visible">
          <template #default="scope">
            <dict-tag :options="business_direction" :value="scope.row.businessDirection" />
          </template>
        </el-table-column>
        <el-table-column label="交流目的" align="center" prop="exchangePurpose" width="150" show-overflow-tooltip v-if="columns[17].visible">
          <template #default="scope">
            <dict-tag :options="exchange_purpose" :value="scope.row.exchangePurpose" />
          </template>
        </el-table-column>
        <el-table-column label="会议/展会名称" align="center" prop="meetingName" width="150" show-overflow-tooltip v-if="columns[19].visible" />
        <el-table-column
          label="交流过程与反馈"
          align="center"
          prop="exchangeFeedback"
          width="200"
          show-overflow-tooltip
          v-if="columns[20].visible && hasTripCommViewRole"
        >
          <template #default="scope">
            <span v-if="canViewRowFeedback(scope.row)">{{ scope.row.exchangeFeedback }}</span>
            <span v-else style="color: #c0c4cc">-</span>
          </template>
        </el-table-column>
        <el-table-column label="申请状态" align="center" prop="tripStatus" width="100" 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" width="100" v-if="columns[22].visible" />
        <el-table-column label="备注" align="center" prop="remark" width="150" show-overflow-tooltip 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" width="200">
          <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" v-if="scope.row.tripStatus === '3'">
              <el-button
                link
                type="primary"
                icon="Tickets"
                @click="handleExportTripApplyPdf(scope.row)"
                v-hasPermi="['oa/crm:businessTripApply:export']"
              ></el-button>
            </el-tooltip>
            <!--            <el-tooltip-->
            <!--              content="填写反馈"-->
            <!--              placement="top"-->
            <!--              v-if="scope.row.tripStatus === '3' && (scope.row.tripType === '2' || scope.row.tripType === '3') && canViewRowFeedback(scope.row)"-->
            <!--            >-->
            <!--              <el-button-->
            <!--                link-->
            <!--                type="success"-->
            <!--                icon="ChatDotRound"-->
            <!--                @click="handleFeedback(scope.row)"-->
            <!--                v-hasPermi="['oa/crm:businessTripApply:edit']"-->
            <!--              ></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>

    <!-- 出差反馈对话框 -->
    <el-dialog v-model="feedbackDialogVisible" title="填写出差反馈" width="600px" append-to-body>
      <el-form ref="feedbackFormRef" :model="feedbackForm" :rules="feedbackRules" label-width="120px">
        <el-form-item label="申请单号">
          <el-input v-model="feedbackForm.applyCode" disabled />
        </el-form-item>
        <el-form-item label="出差类型">
          <dict-tag :options="trip_type" :value="feedbackForm.tripType" />
        </el-form-item>
        <el-form-item label="交流过程与反馈" prop="exchangeFeedback">
          <el-input v-model="feedbackForm.exchangeFeedback" type="textarea" placeholder="达成某种共识或初步合作意向或会议成果" :rows="4" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="feedbackDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitFeedback" :loading="feedbackLoading">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BusinessTripApply" lang="ts">
import {
  listBusinessTripApply,
  delBusinessTripApply,
  updateBusinessTripApply,
  exportBusinessTripApplyPdf
} from '@/api/oa/crm/businessTripApply';
import { BusinessTripApplyVO, BusinessTripApplyQuery } from '@/api/oa/crm/businessTripApply/types';
import { getUserList, listUser } from '@/api/system/user';
import { UserQuery, UserVO } from '@/api/system/user/types';
import { allListDept, listDept } from '@/api/system/dept';
import { DeptVO } from '@/api/system/dept/types';
import { useUserStore } from '@/store/modules/user';
import { getInfo } from '@/api/login';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const userRoles = ref<string[]>([]);
// "市场交流反馈查看"角色
const hasTripCommViewRole = computed(() => userRoles.value?.some((role) => role === 'tripCommView'));

// 拥有角色 或 当前用户是该行数据的创建人
const canViewRowFeedback = (row: BusinessTripApplyVO): boolean => {
  if (hasTripCommViewRole.value) return true;
  const currentUserId = String(useUserStore().userId);
  if (row.createBy && currentUserId === String(row.createBy)) return true;
  return false;
};
const { trip_status, trip_type, business_direction, exchange_purpose } = toRefs<any>(
  proxy?.useDict('trip_status', 'trip_type', 'business_direction', 'exchange_purpose')
);

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 userList = ref<UserVO[]>([]);
const deptList = ref<DeptVO[]>([]);

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: false },
  { key: 19, label: `会议/展会名称`, visible: true },
  { key: 20, label: `交流过程与反馈`, visible: true },
  { key: 21, label: `申请状态`, visible: true },
  { key: 22, label: `流程状态`, visible: false },
  { 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: '/tripapply/businessTripApply/edit',
    query: { type: 'add', tripType: command }
  });
};

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

/** 查看按钮操作 */
const handleView = (row: BusinessTripApplyVO) => {
  const _tripId = row?.tripId;
  router.push({
    path: '/tripapply/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 handleExportTripApplyPdf = async (row: BusinessTripApplyVO) => {
  try {
    const res = await exportBusinessTripApplyPdf(row.tripId);
    const blob = new Blob([res as unknown as BlobPart], { type: 'application/pdf' });
    const url = window.URL.createObjectURL(blob);
    const link = document.createElement('a');
    link.href = url;
    link.download = `出差申请单_${row.applyCode || row.tripId}.pdf`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
    proxy?.$modal.msgSuccess('申请单导出成功');
  } catch {
    proxy?.$modal.msgError('申请单导出失败，请确认出差申请已审批完成');
  }
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'oa/crm/businessTripApply/export',
    {
      ...queryParams.value,
      // 如果选中了数据，则只导出选中的数据
      tripIds: ids.value.length > 0 ? ids.value.join(',') : undefined
    },
    `businessTripApply_${new Date().getTime()}.xlsx`
  );
};

onMounted(async () => {
  // 获取当前用户角色
  try {
    const infoRes = await getInfo();
    userRoles.value = infoRes.data?.roles || [];
  } catch (e) {
    userRoles.value = [];
  }
  // 加载用户列表
  const userQuery = ref<UserQuery>();
  const userRes = await getUserList(userQuery.value);
  userList.value = userRes.data;
  // 加载部门列表
  const deptRes = await allListDept({ deptCategory: '03' } as any);
  deptList.value = deptRes.data;
  // 加载数据列表
  getList();
});

// ============ 出差反馈相关 ============
const feedbackDialogVisible = ref(false);
const feedbackLoading = ref(false);
const feedbackFormRef = ref<ElFormInstance>();
const feedbackForm = ref<{
  tripId?: number | string;
  applyCode?: string;
  tripType?: string;
  applicantId?: number | string;
  tripLocation?: string;
  startTime?: string;
  endTime?: string;
  durationDays?: number;
  exchangeFeedback?: string;
}>({});
const feedbackRules = {
  exchangeFeedback: [{ required: true, message: '请填写交流过程与反馈', trigger: 'blur' }]
};

// /** 填写反馈按钮操作 */
// const handleFeedback = (row: BusinessTripApplyVO) => {
//   feedbackForm.value = {
//     tripId: row.tripId,
//     applyCode: row.applyCode,
//     tripType: row.tripType,
//     applicantId: row.applicantId,
//     tripLocation: row.tripLocation,
//     startTime: row.startTime,
//     endTime: row.endTime,
//     durationDays: row.durationDays,
//     exchangeFeedback: row.exchangeFeedback || ''
//   };
//   feedbackDialogVisible.value = true;
// };

/** 提交反馈 */
const submitFeedback = async () => {
  await feedbackFormRef.value?.validate();
  feedbackLoading.value = true;
  try {
    await updateBusinessTripApply({
      tripId: String(feedbackForm.value.tripId),
      tripType: feedbackForm.value.tripType,
      applicantId: feedbackForm.value.applicantId,
      tripLocation: feedbackForm.value.tripLocation,
      startTime: feedbackForm.value.startTime,
      endTime: feedbackForm.value.endTime,
      durationDays: feedbackForm.value.durationDays,
      exchangeFeedback: feedbackForm.value.exchangeFeedback
    });
    proxy?.$modal.msgSuccess('反馈提交成功');
    feedbackDialogVisible.value = false;
    await getList();
  } catch (e) {
    console.error(e);
  } finally {
    feedbackLoading.value = false;
  }
};
</script>
