<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="量表" prop="scaleNames">
            <el-input v-model="queryParams.scaleNames" placeholder="请输入量表名称 以逗号隔开" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item label="场次名称" prop="sessionName">
            <el-input v-model="queryParams.sessionName" placeholder="请输入场次名称" clearable style="width: 240px"
              @keyup.enter="handleQuery" />
          </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>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="scalePublishList">
        <el-table-column label="场次名称" align="center" prop="sessionName" />
        <el-table-column label="量表名称 以逗号隔开" align="center" prop="scaleNames" />

        <el-table-column label="班级名称/用户名称" align="center" prop="deptNames">
          <template #default="scope">
            <span>
              {{ scope.row.deptNames.length > 0 ? scope.row.deptNames.join(',') : scope.row.userNames.join(',') }}
            </span>
          </template>
        </el-table-column>

        <el-table-column label="截止日期" align="center" prop="expireTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'danger'" disable-transitions>
              {{ scope.row.status === 1 ? '正常' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260">
          <template #default="scope">
            <el-tooltip content="查看测评结果" placement="top">
              <el-button link type="primary" icon="View" @click="handleUpdate(scope.row)"
                v-hasPermi="['publish:evaluation:pageList']">
                <router-link :to="`/activity/index/2/${scope.row.batchNo}/${scope.row.sessionName}`"
                  class="link-type">
                  <span>查看测评结果</span>
                </router-link>
              </el-button>
            </el-tooltip>

            <el-tooltip v-if="scope.row.publishType === 1" content="团体报告导出" placement="top">
              <el-button link type="primary" icon="Download" @click="handlePublishExport(scope.row)"
                v-hasPermi="['scale:publish:export']">
                <span>团体报告导出</span>
              </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="ScalePublish" lang="ts">
import { listScalePublish, getScalePublish, getExportCheck } from '@/api/scale/scalePublish';
import { ScalePublishVO, ScalePublishQuery, ScalePublishForm } from '@/api/scale/scalePublish/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const scalePublishList = ref<ScalePublishVO[]>([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const total = ref(0);

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

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

const initFormData: ScalePublishForm = {
  batchNo: undefined,
  scaleIds: undefined,
  scaleNames: undefined,
  sessionName: undefined,
  deptId: undefined,
  partNum: undefined,
  expireTime: undefined,
  status: undefined,
  allowQueryResult: undefined,
}
const data = reactive<PageData<ScalePublishForm, ScalePublishQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    scaleIds: undefined,
    scaleNames: undefined,
    sessionName: undefined,
    deptId: undefined,
    partNum: undefined,
    expireTime: undefined,
    status: undefined,
    allowQueryResult: undefined,
    params: {
    }
  },
  rules: {
    batchNo: [
      { required: true, message: "不能为空", trigger: "blur" }
    ],
    scaleIds: [
      { required: true, message: "量表id，以逗号隔开不能为空", trigger: "blur" }
    ],
    scaleNames: [
      { required: true, message: "量表名称 以逗号隔开不能为空", trigger: "blur" }
    ],
    sessionName: [
      { required: true, message: "场次名称不能为空", trigger: "blur" }
    ],
    deptId: [
      { required: true, message: "部门分组id不能为空", trigger: "blur" }
    ],
    partNum: [
      { required: true, message: "应参评人数不能为空", trigger: "blur" }
    ],
    expireTime: [
      { required: true, message: "截止日期不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "状态 0:正常；1:停用不能为空", trigger: "change" }
    ],
    allowQueryResult: [
      { required: true, message: "0:不允许，1：允许不能为空", trigger: "blur" }
    ],
  }
});

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

/** 查询量发布列表 */
const getList = async () => {
  loading.value = true;
  const res = await listScalePublish(queryParams.value);
  scalePublishList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

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

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

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

/** 修改按钮操作 */
const handleUpdate = async (row?: any) => {
  reset();
  const batchNo = row?.batchNo
  const res = await getScalePublish(batchNo)
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改量发布";
}

const handlePublishExport = async (row: any) => {
  const { batchNo, sessionName } = row;
  const res = await getExportCheck(batchNo)
  if (res.code == 500) {
    proxy?.$modal.msgError(res.msg);
    return
  }

  proxy?.download('scale/publish/export', {
    batchNo, sessionName
  }, `团队报告-${sessionName}.pdf`)
}


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