<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="120px">
            <el-form-item label="能源类型名称" prop="energyName">
              <el-input
                v-model="queryParams.energyName"
                placeholder="请输入能源类型名称"
                clearable
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
<!--      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['ems:baseEnergyType:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>-->

      <el-table v-loading="loading" :data="baseEnergyTypeList" @selection-change="handleSelectionChange">
        <el-table-column label="自增标识" align="center" prop="objId" v-if="columns[0].visible"/>
        <el-table-column label="能源类型编号" align="center" prop="energyTypeId" v-if="columns[1].visible"/>
        <el-table-column label="能源类型名称" align="center" prop="energyName" v-if="columns[2].visible"/>
        <el-table-column label="单位" align="center" prop="measureUnit" v-if="columns[3].visible"/>
        <el-table-column label="单价" align="center" prop="price" v-if="columns[4].visible"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['ems:baseEnergyType:edit']"></el-button>
            </el-tooltip>
          </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"
      ></pagination>
    </el-card>

    <!-- 添加或修改能源类型信息对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogOpen" width="500px" append-to-body>
      <el-form ref="baseEnergyTypeFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="能源类型编号" prop="energyTypeId">
          <el-input v-model="form.energyTypeId" placeholder="请输入能源类型编号" disabled/>
        </el-form-item>
        <el-form-item label="能源类型名称" prop="energyName">
          <el-input v-model="form.energyName" placeholder="请输入能源类型名称" disabled/>
        </el-form-item>
        <el-form-item label="单位" prop="measureUnit">
          <el-input v-model="form.measureUnit" placeholder="请输入单位" disabled/>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="dialogCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts" name="BaseEnergyType">
import { ref, reactive, getCurrentInstance, ComponentInternalInstance } from 'vue';
import type { FormInstance, FormRules } from 'element-plus';
import {
  listBaseEnergyType,
  getBaseEnergyType,
  addBaseEnergyType,
  updateBaseEnergyType
} from '@/api/ems/base/baseEnergyType';
import type { BaseEnergyTypeVO, BaseEnergyTypeForm, BaseEnergyTypeQuery } from '@/api/ems/base/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const loading = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const showSearch = ref(true);
const total = ref(0);
const baseEnergyTypeList = ref<BaseEnergyTypeVO[]>([]);
const dialogTitle = ref('');
const dialogOpen = ref(false);

const queryParams = ref<BaseEnergyTypeQuery>({
  pageNum: 1,
  pageSize: 10,
  energyTypeId: undefined,
  energyName: undefined,
  measureUnit: undefined,
  price: undefined
});

const form = ref<BaseEnergyTypeForm>({});

const rules = reactive<FormRules>({
  price: [
    { required: true, message: '单价不能为空', trigger: 'blur' }
  ]
});

const columns = ref([
  { key: 0, label: '自增标识', visible: false },
  { key: 1, label: '能源类型编号', visible: false },
  { key: 2, label: '能源类型名称', visible: true },
  { key: 3, label: '单位', visible: true },
  { key: 4, label: '单价', visible: true }
]);

const queryFormRef = ref<FormInstance>();
const baseEnergyTypeFormRef = ref<FormInstance>();

/** 查询能源类型信息列表 */
function getList() {
  loading.value = true;
  listBaseEnergyType(queryParams.value).then((response: any) => {
    baseEnergyTypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function dialogCancel() {
  dialogOpen.value = false;
  reset();
}

function reset() {
  form.value = {
    objId: undefined,
    energyTypeId: undefined,
    energyName: undefined,
    measureUnit: undefined,
    price: undefined
  };
  baseEnergyTypeFormRef.value?.resetFields();
}

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

/** 重置按钮操作 */
function resetQuery() {
  queryFormRef.value?.resetFields();
  handleQuery();
}

function handleSelectionChange(selection: BaseEnergyTypeVO[]) {
  ids.value = selection.map(item => item.objId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
}

/** 修改按钮操作 */
function handleUpdate(row?: BaseEnergyTypeVO) {
  reset();
  const objId = row?.objId || ids.value[0];
  getBaseEnergyType(objId).then((response: any) => {
    form.value = response.data;
    dialogOpen.value = true;
    dialogTitle.value = '修改能源类型信息';
  });
}

/** 提交按钮 */
function submitForm() {
  baseEnergyTypeFormRef.value?.validate((valid: boolean) => {
    if (valid) {
      if (form.value.objId != null) {
        updateBaseEnergyType(form.value).then(() => {
          proxy?.$modal.msgSuccess('修改成功');
          dialogOpen.value = false;
          getList();
        });
      } else {
        addBaseEnergyType(form.value).then(() => {
          proxy?.$modal.msgSuccess('新增成功');
          dialogOpen.value = false;
          getList();
        });
      }
    }
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy?.download('ems/base/baseEnergyType/export', {
    ...queryParams.value
  }, `baseEnergyType_${new Date().getTime()}.xlsx`);
}

getList();
</script>
