<template>
  <div>
    <el-row>
      <el-col :span="4">
        <el-form-item label="项目名称">
          <el-input
            v-model="filterProjects.name"
            placeholder="请输入项目名称"
          />
        </el-form-item>
      </el-col>
      <el-col
        :span="4"
        :offset="1"
      >
        <el-form-item label="立项时间">
          <el-date-picker
            v-model="filterProjects.time"
            type="month"
            placeholder="请选择立项时间"
            format="YYYY-MM"
            value-format="YYYY-MM"
            clearable
          />
        </el-form-item>
      </el-col>
      <el-col
        :span="4"
        :offset="1"
      >
        <el-form-item label="项目状态">
          <el-select
            v-model="currentlyState"
            clearable
            @change="changeState"
            @clear="clearableState"
          >
            <el-option
              v-for="item in stateList"
              :key="item.ID"
              :label="item.name"
              :value="item.ID"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col
        :span="5"
        style="margin-left: 40px"
      >
        <el-row justify="space-around">
          <el-button
            type="primary"
            :icon="Search"
            @click="queryProject"
          >查询
          </el-button>
          <el-button
            type="primary"
            :icon="DocumentCopy"
            @click="jumpProjectApproval"
          >立项
          </el-button>
          <el-button
            type="primary"
            :icon="RefreshLeft"
            @click="listData(initialCondition)"
          >重置
          </el-button>
        </el-row>
      </el-col>
    </el-row>
    <el-row>
      <el-col :span="23">
        <el-row justify="space-between">
          <el-col :span="5">
            <div
              class="bg-white h-24"
              style="box-shadow: -7px 0 7px #e5e5e5"
            >
              <el-row
                style="height: 40%"
                align="middle"
              >
                <el-col :offset="2">
                  <el-text
                    style="font-family: 黑体;"
                  >项目总数
                  </el-text>
                </el-col>
              </el-row>
              <el-row
                style="height: 60%"
                align="middle"
              >
                <el-col :offset="2">
                  <span class="dataStyle">{{ screenData.totalProject }}</span>
                </el-col>
              </el-row>
            </div>
          </el-col>
          <el-col :span="5">
            <div
              class="bg-white h-24"
              style="box-shadow: -7px 0 7px #e5e5e5"
            >
              <el-row
                style="height: 40%"
                align="middle"
              >
                <el-col :offset="2">
                  <el-text
                    style="font-family: 黑体;"
                  >执行中项目
                  </el-text>
                </el-col>
              </el-row>
              <el-row
                style="height: 60%"
                align="middle"
              >
                <el-col :offset="2">
                  <span class="dataStyle">{{ screenData.unCompleteProject }}</span>
                </el-col>
              </el-row>
            </div>
          </el-col>
          <el-col :span="5">
            <div
              class="bg-white h-24"
              style="box-shadow: -7px 0 7px #e5e5e5"
            >
              <el-row
                style="height: 40%"
                align="middle"
              >
                <el-col :offset="2">
                  <el-text
                    style="font-family: 黑体;"
                  >已完成项目
                  </el-text>
                </el-col>
              </el-row>
              <el-row
                style="height: 60%"
                align="middle"
              >
                <el-col :offset="2">
                  <span class="dataStyle">{{ screenData.completeProject }}</span>
                </el-col>
              </el-row>
            </div>
          </el-col>
          <el-col :span="5">
            <div
              class="bg-white h-24"
              style="box-shadow: -7px 0 7px #e5e5e5"
            >
              <el-row
                style="height: 40%"
                align="middle"
              >
                <el-col :offset="2">
                  <el-text
                    style="font-family: 黑体;"
                  >项目总金额
                  </el-text>
                </el-col>
              </el-row>
              <el-row
                style="height: 60%"
                align="middle"
              >
                <el-col :offset="2">
                  <span class="dataStyle">{{ screenData.sum }}元</span>
                </el-col>
              </el-row>
            </div>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-form>
      <el-row style="margin-top: 20px;">
        <el-col
          :span="23"
        >
          <el-table
            :data="projectListData"
            height="580"
            stripe
            border
          >
            <el-table-column
              prop="code"
              label="项目编码"
              align="center"
            />
            <el-table-column
              prop="name"
              label="项目名称"
              align="center"
            />
            <el-table-column
              prop="firstPrincipal"
              label="甲方负责人"
              align="center"
            />
            <el-table-column
              prop="projectPrice"
              label="项目金额"
              align="center"
            />
            <el-table-column
              prop="CreatedAt"
              label="立项日期"
              align="center"
            />
            <el-table-column
              prop="projectState.name"
              label="状态"
              align="center"
            />
            <el-table-column
              label="项目操作"
              align="center"
            >
              <template #default="scope">
                <el-button
                  @click="jumpProjectDetails(scope.row)"
                >
                  详情
                </el-button>
                <el-button
                  type="danger"
                  @click="projectDelete(scope.$index, scope.row)"
                >
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-col
        :span="23"
        style="display: flex;justify-content: end"
      >
        <el-pagination
          v-model:current-page="filterProjects.pageInfo.page"
          :page-size="10"
          background
          layout="prev, pager, next"
          :total="listTotal"
          @change="changePage"
        />
      </el-col>
    </el-row>
  </div>
</template>

<script setup>
import { DocumentCopy, RefreshLeft, Search } from '@element-plus/icons-vue'
import { getProjectList, projectDataScreen, deleteProject, queryProjectState } from '@/api/project'
import { reactive, ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
const router = useRouter()
// eslint-disable-next-line no-unused-vars
const route = useRoute()
defineOptions({
  name: 'ProjectList'
})
const screenData = reactive({})
const projectListData = reactive([])
const listTotal = ref(0)
const stateList = reactive([])
const initialCondition = {
  pageInfo: {
    page: 1,
    pageSize: 10
  },
  name: '',
  time: '',
  state: 0
}
const filterProjects = reactive({
  pageInfo: {
    page: 1,
    pageSize: 10
  },
  name: '',
  time: '',
  state: 0
}
)

const currentlyState = ref('')
const changeState = (value) => {
  filterProjects.state = value
  stateList.forEach(item => {
    if (value === item.ID) {
      currentlyState.value = item.name
    }
  })
}

const clearableState = () => {
  currentlyState.value = ''
  filterProjects.state = 0
}
const jumpProjectDetails = (row) => {
  const { href } = router.resolve({
    path: '/layout/projectManage/projectDetails', // 路径
    query: {
      code: row.code,
    }// 传参
  })
  location.href = (href)
}
const projectDelete = (index, row) => {
  ElMessageBox.confirm(
    '确定要删除该项目吗?',
    '项目删除',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(() => {
      deleteProject(row.code).then(res => {
        if (res.code === 0) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          listData(initialCondition)
          dataScreen()
        } else {
          ElMessage({
            type: 'error',
            message: res.msg,
          })
        }
      })
    })
    .catch(() => {
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
onMounted(() => {
  // 项目数据总览
  dataScreen()
  // 获取项目状态列表
  queryProjectState().then(res => {
    if (res.code === 0) {
      stateList.length = 0
      stateList.push(...res.data)
    }
  })
  // 获取项目列表
  listData(initialCondition)
})

const listData = (condition) => {
  projectListData.length = 0
  getProjectList(condition).then(res => {
    if (res.code === 0 && res.data.total > 0) {
      const list = res.data.list
      list.forEach(item => {
        item.CreatedAt = formatDate(item.CreatedAt)
      })
      projectListData.push(...list)
      listTotal.value = res.data.total
    }
  })
}
const dataScreen = () => {
  projectDataScreen().then(res => {
    const obj = res.data
    Object.keys(obj).forEach(key => {
      screenData[key] = obj[key]
    })
  })
}
const formatDate = (dateString, locale = 'en-US', timezone = 'Asia/Shanghai') => {
  // 使用 Date 构造函数解析日期字符串
  const date = new Date(dateString)
  // 检查日期是否有效
  if (isNaN(date.getTime())) {
    throw new Error('Invalid date string')
  }
  // 使用 Date 对象的年份、月份和日期来构建新的日期字符串
  // 注意：JavaScript 中的月份是从 0 开始的，所以我们需要加 1

  return `${date.getFullYear()}-${('0' + (date.getMonth() + 1)).slice(-2)}-${('0' + date.getDate()).slice(-2)}`
}

const queryProject = () => {
  filterProjects.pageInfo.page = 1
  listData(filterProjects)
}
const changePage = (value) => {
  filterProjects.pageInfo.page = value
  listData(filterProjects)
}

const jumpProjectApproval = () => {
  const { href } = router.resolve({
    path: '/layout/projectManage/projectApproval', // 路径
  })
  location.href = (href)
}

</script>

<style scoped lang="less">
.dataStyle {
  font-size: 25px;
  font-weight: 500;
}
</style>
