<script setup name="templateType">
import { nextTick, onMounted, reactive, ref, getCurrentInstance } from 'vue'
import {
  templateTypePage,
  templateTypeDetail,
  templateTypeSave,
  templateTypeDel
} from '@/http/apis/performanceEvaluation/indicatorTemplate'

const { proxy } = getCurrentInstance(),
  searchForm = reactive({
    indexName: undefined
  }),
  tableListRef = ref(),
  total = ref(0),
  data = ref([]),
  column = [
    {
      label: '序号',
      type: 'index',
      width: '60'
    },
    {
      label: '类型名称',
      key: 'name',
      prop: 'name'
    },
    {
      label: '操作',
      slot: 'action',
      width: '80',
      fixed: 'right'
    }
  ],
  getTableData = async (pageParams = tableListRef.value.pageParams) => {
    const res = await templateTypePage({
      ...pageParams,
      ...searchForm
    })
    data.value = res?.data?.records || []
    total.value = res.data.total
  },
  search = () => {
    getTableData()
  },
  reset = () => {
    searchForm.name = undefined
    tableListRef.value.pageParams.pageNumber = 1
    tableListRef.value.pageParams.pageSize = 10
    getTableData()
  },
  // 新增编辑指标
  dialogVisible = ref(false),
  editType = ref(),
  formData = ref({}),
  formRef = ref(),
  rules = {
    name: [{ required: true, message: '请输入' }]
  },
  loading = ref(false),
  showDialog = async (type, data) => {
    if (type === 2) {
      const res = await templateTypeDetail(data.id)
      formData.value = {
        ...res.data
      }
    }
    editType.value = type
    dialogVisible.value = true
    await nextTick()
    formRef.value.clearValidate()
  },
  save = async (formEl) => {
    if (!formEl) return
    await formEl.validate(async valid => {
      if (valid) {
        loading.value = true
        const postData = {
          id: formData.value.id || undefined,
          name: formData.value.name || ''
        }
        try {
          await templateTypeSave(postData)
          proxy.$message.success('提交成功！')
          getTableData()
          loading.value = false
          close(true)
        } catch (e) {
          loading.value = false
        }
      }
    })
  },
  close = () => {
    formData.value = {}
    dialogVisible.value = false
  },
  // 删除
  delData = (data) => {
    proxy.$messageBox
      .confirm(`是否删除${data.name}?`, '提示！', {
        type: 'warning'
      })
      .then(async () => {
        await templateTypeDel(data.id)
        proxy.$message.success('删除成功！')
        await getTableData()
      })
  }
onMounted(() => {
  getTableData()
})
</script>

<template>
  <div>
    <el-card v-if="false" shadow="never" class="search mb-16">
      <el-form label-suffix=":" :model="searchForm" size="small">
        <el-row :gutter="16">
          <el-col :span="8">
            <el-form-item label="类型名称">
              <el-input
                v-model="searchForm.name"
                maxlength="10"
                placeholder="请输入"
              />
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item class="btn">
              <el-button type="primary" @click="search">查询</el-button>
              <el-button @click="reset">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-card shadow="never">
      <template #header>
        <div class="flex justify-between items-center">
          <span>模板类型列表</span>
          <el-button
            type="primary"
            size="small"
            icon="Plus"
            @click="showDialog(1)"
          >新增模板类型</el-button>
        </div>
      </template>
      <table-list
        ref="tableListRef"
        :column="column"
        :data="data"
        :total="total"
        @get-table-data="getTableData"
      >
        <template #isDisplay="{scope}">
          <el-switch v-model="scope.row.isDisplay" :before-change="()=>changeIsDisplay(scope.row)" />
        </template>
        <template #action="{scope}">
          <a @click="showDialog(2,scope.row)">编辑</a>
          <a v-if="false" class="text-danger" @click="delData(scope.row)">删除</a>
        </template>
      </table-list>
    </el-card>
  </div>
  <el-dialog :title="editType===1?'新增指标':'编辑指标'" :model-value="dialogVisible" @close="close">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-suffix="："
      label-width="130"
    >
      <el-form-item label="类型名称" prop="name">
        <el-input v-model="formData.name" :maxlength="50" placeholder="请输入" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button
        type="primary"
        size="small"
        :loading="loading"
        @click="save(formRef)"
      >
        保存
      </el-button>
      <el-button size="small" @click="close">
        关闭
      </el-button>
    </template>
  </el-dialog>
</template>
