<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-position="top">
      <el-form-item label="用户名" prop="userName" label-position="top">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="统计月份" prop="monthPeriod" label-position="top">
        <el-input
          v-model="queryParams.monthPeriod"
          placeholder="请输入统计月份(YYYY-MM)"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" label-position="top">
        <el-input
          v-model="queryParams.status"
          placeholder="请输入状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label-position="top">
        <div class="item-search">&nbsp;</div>
        <div class="item-search">
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        </div>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['gxt:userscore:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['gxt:userscore:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['gxt:userscore:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="Download"
          @click="handleExport"
          v-hasPermi="['gxt:userscore:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userScoreList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column label="主键ID" prop="id" />
      <el-table-column label="用户名" prop="userName" />
      <el-table-column label="统计月份" prop="monthPeriod" />
      <el-table-column label="维保工单总分" prop="maintenanceTotalScore" />
      <el-table-column label="维修工单总分" prop="repairTotalScore" />
      <el-table-column label="最终评分" prop="finalScore" />
      <el-table-column label="一级校核状态" prop="firstCheckStatus" />
      <el-table-column label="一级校核人" prop="firstChecker" />
      <el-table-column label="一级校核时间" prop="firstCheckTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.firstCheckTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="二级校核状态" prop="secondCheckStatus" />
      <el-table-column label="二级校核人" prop="secondChecker" />
      <el-table-column label="二级校核时间" prop="secondCheckTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.secondCheckTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" />
      <el-table-column label="操作" width="180" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['gxt:userscore:edit']">编辑</el-button>
          <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gxt:userscore:remove']">删除</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-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="userScoreRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="统计月份" prop="monthPeriod">
          <el-input v-model="form.monthPeriod" placeholder="请输入统计月份(YYYY-MM)" />
        </el-form-item>
        <el-form-item label="维保工单总分" prop="maintenanceTotalScore">
          <el-input v-model="form.maintenanceTotalScore" placeholder="请输入维保工单总分" />
        </el-form-item>
        <el-form-item label="维修工单总分" prop="repairTotalScore">
          <el-input v-model="form.repairTotalScore" placeholder="请输入维修工单总分" />
        </el-form-item>
        <el-form-item label="最终评分" prop="finalScore">
          <el-input v-model="form.finalScore" placeholder="请输入最终评分" />
        </el-form-item>
        <el-form-item label="一级校核状态" prop="firstCheckStatus">
          <el-input v-model="form.firstCheckStatus" placeholder="请输入一级校核状态" />
        </el-form-item>
        <el-form-item label="一级校核人" prop="firstChecker">
          <el-input v-model="form.firstChecker" placeholder="请输入一级校核人" />
        </el-form-item>
        <el-form-item label="一级校核时间" prop="firstCheckTime">
          <el-date-picker clearable
            v-model="form.firstCheckTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择一级校核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="二级校核状态" prop="secondCheckStatus">
          <el-input v-model="form.secondCheckStatus" placeholder="请输入二级校核状态" />
        </el-form-item>
        <el-form-item label="二级校核人" prop="secondChecker">
          <el-input v-model="form.secondChecker" placeholder="请输入二级校核人" />
        </el-form-item>
        <el-form-item label="二级校核时间" prop="secondCheckTime">
          <el-date-picker clearable
            v-model="form.secondCheckTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择二级校核时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-input v-model="form.status" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserScore">
import { listUserScore, getUserScore, delUserScore, addUserScore, updateUserScore } from "@/api/gxt/userScore"
import { getCurrentInstance, ref, reactive, toRefs } from "vue"

const { proxy } = getCurrentInstance()

const userScoreList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {
    id: null,
    userName: null,
    monthPeriod: null,
    maintenanceTotalScore: null,
    repairTotalScore: null,
    finalScore: null,
    firstCheckStatus: null,
    firstChecker: null,
    firstCheckTime: null,
    secondCheckStatus: null,
    secondChecker: null,
    secondCheckTime: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: undefined,
    monthPeriod: undefined,
    status: undefined
  },
  rules: {
    userName: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    monthPeriod: [
      { required: true, message: "统计月份(YYYY-MM)不能为空", trigger: "blur" }
    ],
  }
})

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

/** 查询用户月度评分统计表列表 */
function getList() {
  loading.value = true
  listUserScore(queryParams.value).then(response => {
    userScoreList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    id: null,
    userName: null,
    monthPeriod: null,
    maintenanceTotalScore: null,
    repairTotalScore: null,
    finalScore: null,
    firstCheckStatus: null,
    firstChecker: null,
    firstCheckTime: null,
    secondCheckStatus: null,
    secondChecker: null,
    secondCheckTime: null,
    status: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm("userScoreRef")
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

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

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加用户月度评分统计表"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getUserScore(id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改用户月度评分统计表"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["userScoreRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateUserScore(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addUserScore(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const deleteIds = row.id || ids.value
  proxy.$modal.confirm('是否确认删除用户月度评分统计表编号为"' + deleteIds + '"的数据项？').then(function() {
    return delUserScore(deleteIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('gxt/userscore/export', {
    ...queryParams.value
  }, `userScore_${new Date().getTime()}.xlsx`)
}

getList()
</script>