<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">
            <el-form-item label="发生日期" prop="dateOfOccurrence">
              <el-date-picker clearable v-model="queryParams.dateOfOccurrence" type="date" value-format="YYYY-MM-DD" placeholder="请选择发生日期" />
            </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">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="qualityConstructionLogList">
        <el-table-column label="序号" align="center" type="index" width="70" />

        <el-table-column label="生产情况" align="center" prop="condition" width="410">
          <template #default="scope">
            <el-tooltip placement="top" effect="dark">
              <template #content>
                <div class="w-670px">{{ scope.row.condition }}</div>
              </template>
              <el-text truncated>
                {{ scope.row.condition }}
              </el-text>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="技术质量安全工作" align="center" prop="technologyQuality" width="410">
          <template #default="scope">
            <el-tooltip placement="top" effect="dark">
              <template #content>
                <div class="w-670px">{{ scope.row.technologyQuality }}</div>
              </template>
              <el-text truncated>
                {{ scope.row.technologyQuality }}
              </el-text>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column label="发生日期" align="center" prop="dateOfOccurrence" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.dateOfOccurrence, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建者" align="center" prop="createdBy" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleUpdate(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>
    <!-- 详情对话框 -->
    <el-dialog title="施工日志详情" v-model="dialog.visible" width="60vw" append-to-body>
      <quality-construction-detail :qualityConstructionId="currentId" />
    </el-dialog>
  </div>
</template>

<script setup name="QualityConstructionLog" lang="ts">
import {
  listQualityConstructionLog,
  getQualityConstructionLog,
  delQualityConstructionLog,
  addQualityConstructionLog,
  updateQualityConstructionLog
} from '@/api/quality/qualityConstructionLog';
import { QualityConstructionLogVO, QualityConstructionLogQuery, QualityConstructionLogForm } from '@/api/quality/qualityConstructionLog/types';
import QualityConstructionDetail from './cpmponent/qualityConstructionDetail.vue';
import { useUserStoreHook } from '@/store/modules/user';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;

// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const qualityConstructionLogList = ref<QualityConstructionLogVO[]>([]);
const buttonLoading = ref(false);
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 queryFormRef = ref<ElFormInstance>();
const qualityConstructionLogFormRef = ref<ElFormInstance>();

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

const initFormData: QualityConstructionLogForm = {
  id: undefined,
  projectId: currentProject.value.goId,
  happenDate: undefined,
  productionStatus: undefined,
  technologyQuality: undefined,
  file: undefined,
  remark: undefined
};
const data = reactive<PageData<QualityConstructionLogForm, QualityConstructionLogQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value.goId,
    happenDate: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键id不能为空', trigger: 'blur' }],
    projectId: [{ required: true, message: '项目id不能为空', trigger: 'blur' }],
    happenDate: [{ required: true, message: '发生日期不能为空', trigger: 'blur' }]
  }
});

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

/** 查询质量-施工日志列表 */
const getList = async () => {
  loading.value = true;
  const res = await listQualityConstructionLog(queryParams.value);
  qualityConstructionLogList.value = res.data.list;
  total.value = res.data.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

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

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

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

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加质量-施工日志';
};

const currentId = ref<string | number>('');
/** 修改按钮操作 */
const handleUpdate = async (row?: QualityConstructionLogVO) => {
  currentId.value = row?.id;
  dialog.visible = true;
};

/** 提交按钮 */
const submitForm = () => {
  qualityConstructionLogFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateQualityConstructionLog(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addQualityConstructionLog(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: QualityConstructionLogVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除质量-施工日志编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delQualityConstructionLog(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

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

onUnmounted(() => {
  listeningProject();
});

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