<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="110px">
            <el-form-item label="申请单编号" prop="formNo">
              <el-input v-model="queryParams.formNo" placeholder="请输入申请单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工程名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入工程名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="卷册号" prop="volumeNo">
              <el-input v-model="queryParams.volumeNo" placeholder="请输入卷册号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery" v-hasPermi="['design:designChange:list']">搜索</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-button type="primary" plain icon="ChatRound" @click="handleAdd" v-hasPermi="['design:designChange:add']">下发变更通知</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="designChangeList">
        <el-table-column label="序号" type="index" width="60" align="center" />
        <el-table-column label="申请单编号" align="center" prop="formNo" width="150" />
        <el-table-column label="工程名称" align="center" prop="projectName" width="150" />
        <el-table-column label="提出单位" align="center" prop="submitUnit" width="150" />
        <el-table-column label="专业" align="center" prop="specialtyName" width="120" />
        <el-table-column label="提出日期" align="center" prop="submitDate" width="150">
          <template #default="scope">
            <span>{{ parseTime(scope.row.submitDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="卷册号" align="center" prop="volumeNo" width="150" />
        <el-table-column label="流程状态" align="center">
          <template #default="scope">
            <dict-tag v-if="scope.row.fileId != null" :options="wf_business_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="变更文件" align="center" width="150">
          <template #default="scope">
            <el-button
              link
              type="primary"
              v-if="scope.row.ossVoList && scope.row.ossVoList.length > 0"
              icon="View"
              @click="handleDesignView(scope.row)"
              >查看文件</el-button
            >
          </template>
        </el-table-column>
        <el-table-column label="变更原因" align="center" prop="changeReason" width="150">
          <template #default="scope">
            <dict-tag :options="design_change_reason_type" :value="scope.row.changeReason ? scope.row.changeReason.split(',') : []" />
          </template>
        </el-table-column>
        <el-table-column label="变更内容" align="center" prop="changeContent" width="150" />
        <el-table-column label="创建时间" align="center" prop="createTime" width="150" />
        <el-table-column label="备注" align="center" prop="remark" width="200" />
        <el-table-column label="操作" align="center" fixed="right" width="300">
          <template #default="scope">
            <el-button
              type="primary"
              link
              icon="Upload"
              @click="handleAddChange(scope.row)"
              v-if="(scope.row.status == 'finish' || scope.row.costEstimation == '0') && scope.row.auditStatus == 'draft'"
              >上传</el-button
            >
            <el-button
              type="success"
              link
              icon="View"
              v-if="scope.row.auditStatus != 'draft'"
              v-hasPermi="['design:designChange:query']"
              @click="handleViewInfo(scope.row)"
              >查看</el-button
            >
            <el-button type="success" link icon="View" v-hasPermi="['design:designChange:query']" @click="handleViewDetail(scope.row)"
              >通知单</el-button
            >
            <el-button
              type="warning"
              link
              icon="View"
              v-hasPermi="['design:designChange:query']"
              v-if="scope.row.status != 'draft'"
              @click="handleViewHistory(scope.row)"
              >查看单据</el-button
            >
          </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>
    <wordDetial ref="wordDetialRef"></wordDetial>
    <el-dialog draggable title="文件列表" v-model="viewVisible" width="500px">
      <el-table v-if="fileList.length > 0" :data="fileList" style="width: 100%" border>
        <el-table-column prop="fileName" label="文件名称" align="center">
          <template #default="scope">
            <el-link :key="scope.row.fileId" :href="scope.row.fileUrl" target="_blank" type="primary" :underline="false">
              {{ scope.row.originalName }}
            </el-link>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170" align="center">
          <template #default="scope">
            <el-button type="success" link icon="View" @click="handleDownload(scope.row)"> 查看 </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-else class="empty-list text-center">暂无文件</div>
      <template #footer>
        <span>
          <el-button type="primary" @click="viewVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog title="单据" v-model="viewDetailVisible" width="800px">
      <histroy ref="histroyRef"></histroy>
    </el-dialog>
  </div>
</template>

<script setup name="DesignChange" lang="ts">
import { listDesignChange, delDesignChange } from '@/api/design/designChange';
import { DesignChangeVO } from '@/api/design/designChange/types';
import { useUserStoreHook } from '@/store/modules/user';
import wordDetial from '@/components/wordDetial/index';
import histroy from '../volumeCatalog/comm/histroy.vue';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { design_change_reason_type } = toRefs<any>(proxy?.useDict('design_change_reason_type'));
// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const designChangeList = ref<DesignChangeVO[]>([]);
const wordDetialRef = ref<InstanceType<typeof wordDetial>>();
const histroyRef = ref<InstanceType<typeof histroy>>();
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const { wf_business_status } = toRefs<any>(proxy?.useDict('wf_business_status'));
const queryFormRef = ref<ElFormInstance>();
const uploadUrl = computed(() => {
  return `/design/collectFile/upload`;
});
const viewVisible = ref(false);
const fileList = ref([]);
const ossid = ref(null);
const viewDetailVisible = ref(false);
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,
    formNo: undefined,
    projectName: undefined,
    submitUnit: undefined,
    specialty: undefined,
    submitDate: undefined,
    volumeName: undefined,
    volumeNo: undefined,
    changeReason: undefined,
    status: undefined,
    params: {}
  }
});

const { queryParams } = toRefs(data);

/** 查询设计变更管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDesignChange(queryParams.value);
  designChangeList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 下发通知 */
const handleAdd = () => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/approval/designChange/indexEdit`,
    query: {
      type: 'add'
    }
  });
};
/** 查看详情 */
const handleViewDetail = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/approval/designChange/indexEdit`,
    query: {
      id: row.id,
      type: 'view'
    }
  });
};
// 上传变更图纸
const handleAddChange = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/approval/drawing/indexEdit`,
    query: {
      id: row.id,
      type: 'add'
    }
  });
};
/** 查看流程操作 */
const handleViewInfo = (row) => {
  proxy.$tab.closePage(proxy.$route);
  proxy.$router.push({
    path: `/approval/drawing/indexEdit`,
    query: {
      id: row.id,
      costEstimation: row.costEstimation,
      type: 'view'
    }
  });
};
/** 删除按钮操作 */
const handleDesignView = async (row?: DesignChangeVO) => {
  fileList.value = row.ossVoList;
  viewVisible.value = true;
};
const handleViewHistory = async (row) => {
  // 查看历史流程记录
  viewDetailVisible.value = true;
  nextTick(() => {
    histroyRef.value?.getList(row.id);
  });
};
// 预览
const onOpen = (path: string) => {
  window.open(path, '_blank');
};
const handleDownload = (row: any) => {
  window.open(row.url, '_blank');
};

onMounted(() => {
  getList();
});
//监听项目id刷新数据
const listeningProject = watch(
  () => currentProject.value?.id,
  (nid, oid) => {
    queryParams.value.projectId = nid;
    getList();
  }
);

onUnmounted(() => {
  listeningProject();
});
</script>
