<template>
  <div>
    <div class="search-term">
      <el-form :inline="true" :model="searchInfo" class="demo-form-inline">
        <el-form-item label="部门名称">
          <el-input v-model="searchInfo.unitName" clearable placeholder="请输入部门名称" />
        </el-form-item>
        <el-form-item label="期编号">
          <el-input v-model="searchInfo.period" clearable placeholder="请输入期编号" />
        </el-form-item>
        <el-form-item>
          <el-button size="mini" type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
          <el-popover v-model:visible="deleteVisible" placement="top" width="160">
            <p>确定要删除吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="deleteVisible = false">取消</el-button>
              <el-button size="mini" type="primary" @click="onDelete">确定</el-button>
            </div>
            <template #reference>
              <el-button icon="el-icon-delete" size="mini" type="danger" style="margin-left: 10px;">批量删除</el-button>
            </template>
          </el-popover>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      ref="multipleTable"
      border
      stripe
      style="width: 100%"
      tooltip-effect="dark"
      :data="tableData"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="期结束时间" width="180">
        <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
      </el-table-column>
      <el-table-column label="期编号" prop="period" />
      <!--      <el-table-column label="部门id" prop="unitId" />-->
      <el-table-column label="部门名称" prop="unitName" />
      <el-table-column label="满分" prop="unitIntegral">
        <template #default="scope">{{ scope.row.unitMaxIntegral/10 }}</template>
      </el-table-column>
      <el-table-column label="扣分" prop="unitIntegral">
        <template #default="scope">{{ (scope.row.unitMaxIntegral-scope.row.unitIntegral)/10 }}</template>
      </el-table-column>
      <el-table-column label="得分" prop="unitIntegral">
        <template #default="scope">{{ scope.row.unitIntegral/10 }}</template>
      </el-table-column>
      <!--      <el-table-column label="关联问题id" prop="problemIds" />-->
      <el-table-column label="按钮组">
        <template #default="scope">
          <el-button size="small" type="primary" icon="el-icon-edit" class="table-button" @click="openProDialog(scope.row)">扣分详情</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="deleteRow(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :current-page="page"
      :page-size="pageSize"
      :page-sizes="[10, 30, 50, 100]"
      :style="{float:'right',padding:'20px'}"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="弹窗操作">
      <el-form :model="formData" label-position="right" label-width="80px">
        <el-form-item label="部门名称:">
          <el-input v-model="formData.unitName" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="现有积分:">
          <el-input v-model.number="formData.unitIntegral" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="满分:">
          <el-input v-model.number="formData.unitMaxIntegral" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="期编号:">
          <el-input v-model="formData.period" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="部门id:">
          <el-input v-model.number="formData.unitId" clearable placeholder="请输入" />
        </el-form-item>
        <el-form-item label="关联问题id:">
          <el-input v-model="formData.problemIds" clearable placeholder="请输入" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="enterDialog">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog v-model="dialogFormVisiblePro" :before-close="closeDialogPro" title="扣分详情">
      <div>
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :current-page="pagePro"
          :page-size="pageSizePro"
          :page-sizes="[10, 30, 50, 100]"
          :style="{float:'right',padding:'20px'}"
          :total="totalPro"
          @current-change="handleCurrentChangePro"
          @size-change="handleSizeChangePro"
        />
        <el-table
          ref="crudItem"
          border
          stripe
          style="width: 100%"
          tooltip-effect="dark"
          :data="tableDataPro"
        >
          <el-table-column label="日期" width="180">
            <template #default="scope">{{ formatDate(scope.row.CreatedAt) }}</template>
          </el-table-column>
          <el-table-column label="发布人" prop="oper" />
          <el-table-column label="站点" prop="siteName" />
          <el-table-column label="站点类型" prop="siteType">
            <template #default="scope">
              <dict-tag :options="dict.type.site_type" :value="scope.row.siteType" />
            </template>
          </el-table-column>
          <el-table-column label="已登录查阅次数" prop="count" />
          <el-table-column label="一类部门" prop="unitName" />
          <el-table-column label="扣评分" prop="integral">
            <template #default="scope">{{ scope.row.integral/10 }}</template>
          </el-table-column>
          <el-table-column label="处理状态" prop="status">
            <template #default="scope">
              <el-tag v-show="scope.row.status == 'Untreated'" type="warning">未处理</el-tag>
              <el-tag v-show="scope.row.status == 'Processed'" type="success">已处理</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="责任部门" prop="department" />
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  createUnitHistory,
  deleteUnitHistory,
  deleteUnitHistoryByIds,
  updateUnitHistory,
  findUnitHistory,
  getUnitHistoryList
} from '@/api/unitHistory' //  此处请自行替换地址
import infoList from '@/mixins/infoList'
import { getProblemInfoList } from '@/api/problemInfo'
export default {
  name: 'UnitHistory',
  mixins: [infoList],
  dicts: ['site_type'],
  data() {
    return {
      listApi: getUnitHistoryList,
      listProApi: getProblemInfoList,
      dialogFormVisible: false,
      type: '',
      deleteVisible: false,
      multipleSelection: [],
      dialogFormVisiblePro: false,
      tableDataPro: [],
      pagePro: 1,
      totalPro: 10,
      pageSizePro: 10,
      searchInfoPro: {
        CreatedAtStart: new Date(),
        CreatedAtEnd: new Date()
      },
      formData: {
        unitName: '',
        unitIntegral: 0,
        unitMaxIntegral: 0,
        period: '',
        unitId: 0,
        problemIds: '',
      }
    }
  },
  async created() {
    await this.getTableData()
  },
  methods: {
  // 条件搜索前端看此方法
    onSubmit() {
      this.page = 1
      this.pageSize = 10
      this.getTableData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    deleteRow(row) {
      this.$confirm('确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteUnitHistory(row)
      })
    },
    async onDelete() {
      const ids = []
      if (this.multipleSelection.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择要删除的数据'
        })
        return
      }
      this.multipleSelection &&
        this.multipleSelection.map(item => {
          ids.push(item.ID)
        })
      const res = await deleteUnitHistoryByIds({ ids })
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        if (this.tableData.length === ids.length && this.page > 1) {
          this.page--
        }
        this.deleteVisible = false
        this.getTableData()
      }
    },
    async updateUnitHistory(row) {
      const res = await findUnitHistory({ ID: row.ID })
      this.type = 'update'
      if (res.code === 0) {
        this.formData = res.data.reunitHistory
        this.dialogFormVisible = true
      }
    },
    closeDialog() {
      this.dialogFormVisible = false
      this.formData = {
        unitName: '',
        unitIntegral: 0,
        unitMaxIntegral: 0,
        period: '',
        unitId: 0,
        problemIds: '',
      }
    },
    async deleteUnitHistory(row) {
      const res = await deleteUnitHistory({ ID: row.ID })
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        if (this.tableData.length === 1 && this.page > 1) {
          this.page--
        }
        this.getTableData()
      }
    },
    async enterDialog() {
      let res
      switch (this.type) {
        case 'create':
          res = await createUnitHistory(this.formData)
          break
        case 'update':
          res = await updateUnitHistory(this.formData)
          break
        default:
          res = await createUnitHistory(this.formData)
          break
      }
      if (res.code === 0) {
        this.$message({
          type: 'success',
          message: '创建/更改成功'
        })
        this.closeDialog()
        this.getTableData()
      }
    },
    openDialog() {
      this.type = 'create'
      this.dialogFormVisible = true
    },
    openProDialog(row) {
      this.dialogFormVisiblePro = true
      console.log(row.period)
      const formatDateStr = row.period.replace(/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})$/, '$1/$2/$3 $4:$5:$6')
      // 将时间格式化成 yyyy-MM-dd HH:mm:ss
      this.searchInfoPro.CreatedAtStart = new Date(formatDateStr)
      this.searchInfoPro.CreatedAtEnd = new Date(row.CreatedAt)
      this.searchInfoPro.unitId = row.unitId
      console.log(this.searchInfoPro)
      this.getTableDataPro()
    },
    handleSizeChangePro(val) {
      this.pageSizePro = val
      this.getTableDataPro()
    },
    handleCurrentChangePro(val) {
      this.pagePro = val
      this.getTableDataPro()
    },
    closeDialogPro() {
      this.dialogFormVisiblePro = false
      this.formData = {
        unitName: '',
        unitIntegral: 0,
      }
    },
    getTableDataPro(page = this.pagePro, pageSize = this.pageSizePro) {
      this.listProApi({ page, pageSize, ...this.searchInfoPro }).then(res => {
        console.log(res)
        if (res.code === 0) {
          this.tableDataPro = res.data.list
          this.totalPro = res.data.total
          this.pagePro = res.data.page
          this.pageSizePro = res.data.pageSize
        }
      })
    }
  },
}
</script>

<style>
</style>
