<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">
            <el-form-item label="设备名称" prop="devicename">
              <el-input v-model="queryParams.devicename" placeholder="请输入设备名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工区" prop="workArea">
              <el-input v-model="queryParams.workArea" 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="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['mechanical:mechanicalrewriting:add']">新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['mechanical:mechanicalrewriting:export']"
              >导出</el-button
            >
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="mechanicalrewritingList">
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <!-- <el-table-column label="自增ID" align="center" prop="id" v-if="true" />
        <el-table-column label="项目ID" align="center" prop="projectId" /> -->
        <el-table-column label="班组名称" align="center" prop="teamName" />
        <el-table-column label="设备名称" align="center" prop="devicename" />
        <el-table-column label="设备类型" align="center" prop="deviceType" />
        <el-table-column label="设备编号" align="center" prop="deviceNumber" />
        <el-table-column label="入场日期" align="center" prop="entryTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center" prop="specification" />
        <el-table-column label="生产能力" align="center" prop="production" />
        <el-table-column label="车牌号码" align="center" prop="plateNumber" />
        <el-table-column label="设备管理员" align="center" prop="deviceKeeper" />
        <el-table-column label="车辆行驶证" align="center" prop="drivingLicence" />
        <el-table-column label="新旧程度" align="center" prop="degree" />
        <el-table-column label="车辆容量" align="center" prop="vehicleCapacity" />
        <el-table-column label="车辆净重" align="center" prop="suttle" />
        <el-table-column label="工区" align="center" prop="workArea" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" fixed="right" min-width="200" fixed-width>
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="View" @click="handleDetails(scope.row)" v-hasPermi="['mechanical:mechanicalrewriting:query']"
                >详情</el-button
              >
            </el-tooltip>
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mechanical:mechanicalrewriting:edit']"
                >修改</el-button
              >
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mechanical:mechanicalrewriting:remove']"
                >删除</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" />
    </el-card>

    <addComponent ref="addComponentRef" @success="getList" />
    <detailsComponent ref="detailsComponentRef" />
  </div>
</template>

<script setup name="Mechanicalrewriting" lang="ts">
import {
  listMechanicalrewriting,
  getMechanicalrewriting,
  delMechanicalrewriting,
  addMechanicalrewriting,
  updateMechanicalrewriting
} from '@/api/mechanical/mechanicalrewriting';
import { MechanicalrewritingVO, MechanicalrewritingQuery, MechanicalrewritingForm } from '@/api/mechanical/mechanicalrewriting/types';
import addComponent from './components/add.vue';
import detailsComponent from './components/details.vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const mechanicalrewritingList = ref<MechanicalrewritingVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const mechanicalrewritingFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: MechanicalrewritingForm = {
  id: undefined,
  projectId: undefined,
  teamName: undefined,
  devicename: undefined,
  deviceType: undefined,
  deviceNumber: undefined,
  entryTime: undefined,
  specification: undefined,
  production: undefined,
  plateNumber: undefined,
  deviceKeeper: undefined,
  drivingLicence: undefined,
  degree: undefined,
  vehicleCapacity: undefined,
  suttle: undefined,
  nameplate: undefined,
  qualification: undefined,
  equipmentPhoto: undefined,
  verificationReport: undefined,
  remark: undefined
};
const data = reactive<PageData<MechanicalrewritingForm, MechanicalrewritingQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: undefined,
    teamName: undefined,
    devicename: undefined,
    deviceType: undefined,
    deviceNumber: undefined,
    entryTime: undefined,
    specification: undefined,
    production: undefined,
    plateNumber: undefined,
    deviceKeeper: undefined,
    drivingLicence: undefined,
    degree: undefined,
    vehicleCapacity: undefined,
    suttle: undefined,
    nameplate: undefined,
    qualification: undefined,
    equipmentPhoto: undefined,
    verificationReport: undefined,
    workArea: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '自增ID不能为空', trigger: 'blur' }]
  }
});

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

/** 查询机械台账列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMechanicalrewriting(queryParams.value);
  mechanicalrewritingList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  mechanicalrewritingFormRef.value?.resetFields();
};

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection: MechanicalrewritingVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};
const addComponentRef = ref();
/** 新增按钮操作 */
const handleAdd = () => {
  addComponentRef.value?.open();
};

/** 修改按钮操作 */
const handleUpdate = async (row?: MechanicalrewritingVO) => {
  //   reset();
  const _id = row?.id || ids.value[0];
  addComponentRef.value?.open(_id);
};
const detailsComponentRef = ref();
//详情
const handleDetails = (row) => {
  const _id = row?.id || ids.value[0];
  detailsComponentRef.value?.open(_id);
};
/** 提交按钮 */
const submitForm = () => {
  mechanicalrewritingFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateMechanicalrewriting(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addMechanicalrewriting(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: MechanicalrewritingVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除机械台账编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delMechanicalrewriting(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'mechanical/mechanicalrewriting/export',
    {
      ...queryParams.value
    },
    `mechanicalrewriting_${new Date().getTime()}.xlsx`
  );
};

onMounted(() => {
  getList();
});
</script>
