<!--
  Description: 现场管理列表页
  Author: 李亚光
  Date: 2023-04-23
 -->
<script lang="ts" setup name="ListLog">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { delScene, endApi, getSceneListPage, startApi } from '@/api/scene'
import { getDictByCode } from '@/api/system/dict'
const $router = useRouter()
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  projectState: '',
  startDate: '',
  endDate: '',
  workPersonName: '',
  workRoad: '',
  workTitle: '',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '作业名称',
    value: 'workTitle',
    align: 'center',
    // width: 170,
  },
  {
    text: '所属道路',
    value: 'workRoad',
    align: 'center',
    // width: 100,
  },
  {
    text: '作业简介',
    value: 'workSiteDesc',
    align: 'center',
  },
  {
    text: '负责人',
    value: 'workPersonName',
    align: 'center',
  },
  {
    text: '负责人联系方式',
    value: 'workPersonPhoneNumber',
    align: 'center',
  },
  {
    text: '注册时间',
    value: 'registerTime',
    align: 'center',
  },
  {
    text: '当前状态',
    value: 'projectStateName',
    align: 'center',
  },
])
const list = ref([])
const total = ref(20)
const listLoading = ref(true)

// 获取现场列表数据
const fetchData = () => {
  listLoading.value = true
  getSceneListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()

// 查询数据
const search = () => {
  fetchData()
}
// 现场状态列表
const sceneStatusList = ref<any[]>([])
const fetchstatusList = () => {
  getDictByCode('projectState').then((response) => {
    sceneStatusList.value = response.data
  })
}
fetchstatusList()
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 新增、
const add = () => {
  $router.push({ path: '/scene/sceneedit' })
}
// 编辑
const edit = (row: any) => {
  $router.push({ path: '/scene/sceneedit', query: { id: row.id } })
}
// 详情
const detail = (row: any) => {
  $router.push({ path: '/scene/scenedetail', query: { id: row.id } })
}
// 报告
const report = (row: any) => {
  $router.push({ path: '/scene/scenereport', query: { id: row.id, title: row.workTitle } })
}
// 删除
const del = () => {
  if (selectList.value.length) {
    ElMessageBox.confirm('是否删除所选的数据?', '提示', {
      confirmButtonText: '是',
      cancelButtonText: '否',
      type: 'info',
    }).then((res) => {
      // 点击确认
      const ids = selectList.value.map(item => item.id)
      delScene(ids).then((res) => {
        if (res.code === 200) {
          selectList.value = []
          ElMessage.success('删除成功')
          fetchData()
        }
      })
    }).catch(() => {
      // 点击取消
      selectList.value = []
    })
  }
  else {
    ElMessage.warning('请先选择需要删除的数据')
  }
}
const changeStatus = (type: string, row: any) => {
  ElMessageBox.confirm(`确认${type === 'start' ? '开始' : '完成'}此作业?`, '提示', {
    confirmButtonText: '是',
    cancelButtonText: '否',
    type: 'info',
  }).then((res) => {
    (type === 'start' ? startApi : endApi)(row.id).then((res) => {
      ElMessage.success('操作成功')
      fetchData()
    })
  })
}
</script>

<template>
  <div class="list-log">
    <!-- 布局 -->
    <app-container>
      <!-- 筛选条件 -->
      <search-area @search="search">
        <search-item>
          <el-input v-model.trim="listQuery.workTitle" placeholder="作业名称" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.workRoad" placeholder="所属街道" clearable />
          <!-- <el-select v-model="listQuery.beginTime" clearable placeholder="所属街道">
            <el-option v-for="item in []" :key="item.id" :label="item.name" :value="item.value" />
          </el-select> -->
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.workPersonName" placeholder="责任人" clearable />
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.startDate" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="注册开始时间"
          />
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.endDate" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="注册结束时间"
          />
        </search-item>
        <search-item>
          <el-select v-model="listQuery.projectState" clearable placeholder="当前状态">
            <el-option v-for="item in sceneStatusList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <template #btns>
          <el-button v-if="proxy.hasPerm('/scene/add')" class="search-btn" type="primary" @click="add">
            新增
          </el-button>
          <el-button v-if="proxy.hasPerm('/scene/del')" class="search-btn" type="info" @click="del">
            删除
          </el-button>
        </template>
      </search-area>
      <table-container>
        <!-- 查询结果Table显示 -->
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery" :is-showmulti-select="true"
          :is-multi="true" :list-loading="listLoading" @change="changePage" @multi-select="multiSelect"
        >
          <template #columns>
            <el-table-column v-if="proxy.hasPerm('/scene/edit')" label="编辑" width="70" align="center">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="edit(scope.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="70" align="center">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="detail(scope.row)">
                  ㅤ详情ㅤㅤ
                </el-button>
              </template>
            </el-table-column>
            <el-table-column label="作业报告" width="90" align="center">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="report(scope.row)">
                  作业报告
                </el-button>
              </template>
            </el-table-column>
            <el-table-column v-if="proxy.hasPerm('/scene/status')" label="作业状态" width="90" align="center">
              <template #default="scope">
                <el-button
                  v-if="scope.row.projectState === '1' || scope.row.projectState === '3'" link type="primary"
                  size="small" @click="changeStatus('start', scope.row)"
                >
                  开始
                </el-button>
                <el-button v-else link type="primary" size="small" @click="changeStatus('end', scope.row)">
                  结束
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

