<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="100">
            <!-- <el-form-item label="项目编号：" prop="productSn">
              <el-input v-model="queryParams.productSn" placeholder="请输入项目编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="项目名称：" prop="productName">
              <el-input v-model="queryParams.productName" placeholder="请输入项目名称" clearable @keyup.enter="handleQuery" />
            </el-form-item> -->
            <el-form-item label="评审阶段：" prop="productType">
              <el-select v-model="queryParams.reviewSn" placeholder="请选择评审阶段" clearable>
                <el-option v-for="dict in item_stage" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="评审时间：" prop="productType">
              <el-date-picker value-format="YYYY-MM-DD HH:mm:ss" @change="changeRange" v-model="reviewTime"
                type="datetimerange" :shortcuts="shortcuts" range-separator="~" start-placeholder="起始时间"
                end-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">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport">导出</el-button>
          </el-col>


          <div class="search-all">
            <el-input @clear="getList" clearable v-model="queryParams.search" placeholder="请输入项目名称、项目编号关键字搜索">
              <template #prefix>
                <el-icon class="el-input__icon">
                  <search />
                </el-icon>
              </template>
            </el-input>
            <el-button @click="getList" plain type="primary">查询</el-button>
          </div>

          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="项目编号" align="center" prop="productSn" />
        <el-table-column label="项目名称" align="center" prop="productName">
          <template #default="scope">
            <span @click="jumpToDetail(scope.row)" style="color: #36d;cursor: pointer;">{{ scope.row.productName
              }}</span>
          </template>
        </el-table-column>
        <el-table-column label="项目单位" align="center" prop="productUnit" />
        <el-table-column label="联系人" align="center" prop="unitBy" />
        <el-table-column label="联系电话" align="center" prop="unitConcat" />
        <el-table-column label="评审阶段" align="center" prop="reviewSn">
          <template #default="scope">
            <dict-tag :options="item_stage" :value="scope.row.reviewSn"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="评审时间" align="center" prop="reviewTime" />

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="jumpToDetail(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>
  </div>
</template>

<script setup name="Project" lang="ts">
import { listProject, getProject } from '@/api/project/review';
import { ProjectVO, ProjectQuery, ProjectForm } from '@/api/project/review/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { item_stage } = toRefs<any>(proxy?.useDict('item_stage'));


const projectList = ref<ProjectVO[]>([]);
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 reviewTime = ref([]);
const shortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setDate(start.getDate() - 7)
      return [start, end]
    },
  },
  {
    text: '最近一月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setMonth(start.getMonth() - 1)
      return [start, end]
    },
  },
  {
    text: '最近三月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setMonth(start.getMonth() - 3)
      return [start, end]
    },
  },
]
const queryFormRef = ref<ElFormInstance>();
const projectFormRef = ref<ElFormInstance>();

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

const initFormData: ProjectForm = {
  revision: undefined,
  productSn: undefined,
  productName: undefined,
  productType: undefined,
  productFund: undefined,
  productOpenplace: undefined,
  productOpentime: undefined,
  productConcatuser: undefined,
  productConcatphone: undefined,
  productUniformcode: undefined,
  productOwnername: undefined,
  productTransactmode: undefined,
  productTotalamount: undefined,
  createdBy: undefined,
  createdTime: undefined,
  productId: undefined,
}
const data = reactive<PageData<ProjectForm, ProjectQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    revision: undefined,
    productSn: undefined,
    productName: undefined,
    productType: undefined,
    productFund: undefined,
    productOpenplace: undefined,
    productOpentime: undefined,
    productConcatuser: undefined,
    productConcatphone: undefined,
    productUniformcode: undefined,
    productOwnername: undefined,
    productTransactmode: undefined,
    productTotalamount: undefined,
    createdBy: undefined,
    createdTime: undefined,
    params: {
    }
  },
  rules: {
    revision: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productSn: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productName: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productType: [
      { required: true, message: "$comment不能为空", trigger: "change" }
    ],
    productFund: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productOpenplace: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productOpentime: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productConcatuser: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productConcatphone: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productUniformcode: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productOwnername: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productTransactmode: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    productTotalamount: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    createdBy: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    createdTime: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
    id: [
      { required: true, message: "$comment不能为空", trigger: "blur" }
    ],
  }
});

const router = useRouter();
const route = useRoute();
const { queryParams, form, rules } = toRefs(data);

/** 查询【请填写功能名称】列表 */
const getList = async () => {

  console.log(`reviewTime.value`, reviewTime.value.length);

  console.log(`getList---`, reviewTime.value);

  if (reviewTime.value.length != 0) {
    queryParams.value.startTime = reviewTime.value[0];
    queryParams.value.endTime = reviewTime.value[1];
  }

  loading.value = true;
  const res = await listProject(queryParams.value);
  projectList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

const changeRange = (value) => {
  console.log(`value----`, value);
  console.log(`value----`, value[0]);

}

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

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

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

/** 重置按钮操作 */
const resetQuery = () => {
  reviewTime.value.splice(0, 2);
  queryParams.value.startTime = '';
  queryParams.value.endTime = '';
  queryFormRef.value?.resetFields();
  handleQuery();
}

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

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('/zjk/reviewPhase/review/export', {
    ...queryParams.value
  }, `product_${new Date().getTime()}.xlsx`)
}

/** 查询详情 */
const jumpToDetail = async (row?: ProjectVO) => {
  router.push({ path: '/project/reviewDetail', query: { id: row.productId || ids.value[0] } });
}


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


<style scoped lang="scss">
.search-all {
  width: 25vw;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: none;
  margin: 0;

  .el-input {
    width: 20vw;
  }

  .el-button {
    margin: 0 1vw 0 1vw;
  }
}
</style>
