<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="projectType">
              <el-select v-model="queryParams.projectType" value-key="" placeholder="请选择模板类型" clearable filterable @change="">
                <el-option v-for="item in projectTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
              </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-button type="primary" plain icon="Plus" :disabled="addSingle" @click="handleAdd" v-hasPermi="['quality:qualityInspection:add']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['quality:qualityInspection:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['quality:qualityInspection:remove']"
              >删除</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['quality:qualityInspection:export']">导出</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <!-- card body -->
      <Contactform v-if="queryParams.projectType == '0'" @selection-change="handleSelectionChange"></Contactform>
      <EngineeringChangeApplicationForm
        v-if="queryParams.projectType == '1'"
        @selection-change="handleSelectionChange"
      ></EngineeringChangeApplicationForm>
    </el-card>

    <el-dialog title="新增模板" v-model="dialogVisible" width="800">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="110px">
        <!-- <el-form-item label="模板类型" prop="projectType">
          <el-select v-model="form.projectType" value-key="" placeholder="请选择模板类型" clearable filterable @change="">
            <el-option v-for="item in projectTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
        </el-form-item> -->

        <div v-if="queryParams.projectType == '0'">
          <el-form-item label="工程名称" prop="projectName">
            <el-input v-model="form.projectName" placeholder="请输入工程名称" />
          </el-form-item>
          <el-form-item label="编号" prop="serialNumber">
            <el-input v-model="form.serialNumber" placeholder="请输入编号" />
          </el-form-item>
          <el-form-item label="致" prop="to">
            <el-input v-model="form.to" placeholder="致：" />
          </el-form-item>
          <el-form-item label="主题" prop="subject">
            <el-input v-model="form.subject" placeholder="请输入主题" />
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input v-model="form.content" type="textarea" :rows="6" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="附件" prop="attachments">
            <file-upload v-model="form.attachments" :limit="1" :file-type="['pdf', 'png', 'jpg', 'jpeg', 'gif', 'bmp']"></file-upload>
          </el-form-item>
          <el-divider class="mb-10! mt-10!">施工项目部</el-divider>
          <el-form-item label="项目负责人" prop="contractorLeader">
            <el-input v-model="form.contractorLeader" placeholder="请输入负责人姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="contractorDate">
            <el-date-picker v-model="form.contractorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">项目监理机构</el-divider>

          <el-form-item label="总监理工程师" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入总监理工程师姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">建设单位</el-divider>
          <el-form-item label="业主代表" prop="ownerRep">
            <el-input v-model="form.ownerRep" placeholder="请输入业主代表" />
          </el-form-item>
          <el-form-item label="日期" prop="ownerDate">
            <el-date-picker v-model="form.ownerDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
        </div>

        <!-- <el-form-item>
        <el-button type="primary" @click="submitForm">提交</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item> -->
        <div v-if="queryParams.projectType === '1'">
          <el-form-item label="工程名称">
            <el-input v-model="form.projectName" />
          </el-form-item>

          <el-form-item label="提出单位">
            <el-input v-model="form.unitName" />
          </el-form-item>

          <el-form-item label="专业">
            <el-input v-model="form.profession" />
          </el-form-item>

          <el-form-item label="提出日期">
            <el-date-picker v-model="form.applyDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>

          <el-form-item label="卷册名称">
            <el-input v-model="form.bookName" />
          </el-form-item>

          <el-form-item label="卷册号">
            <el-input v-model="form.bookNo" />
          </el-form-item>

          <el-form-item label="附图">
            <image-upload v-model="form.hasAttachment"></image-upload>
          </el-form-item>

          <el-form-item label="变更原因">
            <el-checkbox-group v-model="form.changeReasons">
              <el-checkbox label="设计漏项" class="w45%" />
              <el-checkbox label="设计改进" />
              <el-checkbox label="设计差错" class="w45%" />
              <el-checkbox label="接口差错" />
              <el-checkbox label="业主要求" class="w45%" />
              <el-checkbox label="施工承包商要求" />
              <el-checkbox label="外部资料不符" class="w45%" />
              <el-checkbox label="材料代用或其他" />
            </el-checkbox-group>
          </el-form-item>

          <el-form-item label="变更内容">
            <el-input v-model="form.changeContent" />
          </el-form-item>

          <el-form-item label="变更费用估算">
            <el-input v-model="form.costEstimate" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">施工承包单位</el-divider>
          <el-form-item label="项目经理" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入项目经理姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">总承包单位</el-divider>
          <el-form-item label="项目技术负责人" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入项目技术负责人姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">设计单位</el-divider>
          <el-form-item label="设计代表" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入设计代表姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">项目监理单位</el-divider>
          <el-form-item label="监理工程师" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入监理工程师姓名" />
          </el-form-item>
          <el-form-item label="总监理工程师" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入总监理工程师姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
          <el-divider class="mb-10! mt-10!">建设单位</el-divider>
          <el-form-item label="负责人" prop="supervisorLeader">
            <el-input v-model="form.supervisorLeader" placeholder="请输入负责人姓名" />
          </el-form-item>
          <el-form-item label="日期" prop="supervisorDate">
            <el-date-picker v-model="form.supervisorDate" type="date" placeholder="选择日期" style="width: 100%" />
          </el-form-item>
        </div>
      </el-form>
      <template #footer>
        <span>
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { useUserStoreHook } from '@/store/modules/user';
import type { FormInstance, FormRules } from 'element-plus';
import Contactform from './components/contactform.vue';
import EngineeringChangeApplicationForm from './components/engineeringChangeApplicationForm.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);

const formRef = ref<FormInstance>();
const dialogVisible = ref<boolean>(false);
const showSearch = ref(true);
const multiple = ref<boolean>(true);
const addSingle = ref<boolean>(false);
const single = ref<boolean>(true);
const ids = ref<Array<string | number>>([]);

const projectTypeOptions = [
  {
    value: '0',
    label: '联系单'
  },
  {
    value: '1',
    label: '工程变更申请单'
  },
  {
    value: '2',
    label: '通知单'
  },
  {
    value: '3',
    label: '变更单'
  },
  {
    value: '4',
    label: '签证单'
  }
];

const initFormData = {
  id: undefined,
  projectId: currentProject.value?.id,
  projectType: '',
  projectName: '',
  serialNumber: '',
  to: '',
  subject: '',
  content: '',
  attachments: '',
  contractorLeader: '',
  contractorDate: '',
  supervisorLeader: '',
  supervisorDate: '',
  ownerRep: '',
  ownerDate: '',
  unitName: '',
  profession: '',
  applyDate: '',
  bookName: '',
  bookNo: '',
  hasAttachment: '',
  changeReasons: [],
  changeContent: '',
  costEstimate: ''
};
const data = reactive<PageData<any, any>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,
    inspectionType: undefined,
    inspectionStatus: undefined,
    teamId: undefined,
    params: {},
    projectType: '0'
  },
  rules: {
    projectName: [{ required: true, message: '请输入工程名称', trigger: 'blur' }],
    projectType: [{ required: true, message: '请选择模板类型', trigger: 'blur' }],
    serialNumber: [{ required: true, message: '请输入编号', trigger: 'blur' }],
    to: [{ required: true, message: '请输入接收方', trigger: 'blur' }],
    subject: [{ required: true, message: '请输入主题', trigger: 'blur' }],
    content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
  }
});

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

const submitForm = () => {
  formRef.value?.validate((valid) => {
    if (valid) {
      console.log('提交表单：', form);
    }
  });
};

const handleAdd = () => {
  dialogVisible.value = true;
};

const handleQuery = () => {};
const resetQuery = () => {};
const getList = () => {};
const handleDelete = () => {};
const handleUpdate = () => {};

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

const resetForm = () => {
  formRef.value?.resetFields();
};
</script>
