<template>
  <div>
    <el-table size="small" v-if="machineryDetailList.length !== 0" :data="machineryDetailList">
      <el-table-column label="出入场" align="center" prop="type">
        <template #default="scope">
          <dict-tag :options="machinery_entry_exit_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column label="检验证编号" align="center" prop="checkoutNumber" />
      <el-table-column label="检验单位" align="center" prop="checkoutUnit" />
      <el-table-column label="检定日期/有效期" align="center" prop="checkoutDate" />
      <el-table-column label="入场时间" align="center" prop="entryTime" />
      <el-table-column label="图片" align="center" prop="pictureList.url">
        <template #default="scope">
          <el-image :key="picture.id" v-for="picture in scope.row.pictureList" :src="picture.url" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-space wrap>
            <el-button link type="success" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['machinery:machineryDetail:edit']">
              修改
            </el-button>
          </el-space>
          <el-space wrap>
            <el-button link type="danger" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['machinery:machineryDetail:remove']">
              删除
            </el-button>
          </el-space>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      size="small"
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="修改机械出入场详情" v-model="dialogRef" width="500px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="140px">
        <el-form-item label="出入场" prop="type">
          <el-select v-model="form.type" clearable placeholder="请选择出入场">
            <el-option v-for="item in machinery_entry_exit_type" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="检验证编号" prop="checkoutNumber">
          <el-input v-model="form.checkoutNumber" placeholder="请输入检验证编号" />
        </el-form-item>
        <el-form-item label="检验单位" prop="checkoutUnit">
          <el-input v-model="form.checkoutUnit" placeholder="请输入检验单位" />
        </el-form-item>
        <el-form-item label="检定日期/有效期" prop="checkoutDate">
          <el-date-picker
            clearable
            v-model="form.checkoutDate"
            type="datetime"
            value-format="YYYY-MM-DD HH:mm:ss"
            placeholder="请选择检定日期/有效期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="施工类型状态" prop="status">
          <el-select v-model="form.status" clearable placeholder="请选择施工类型状态">
            <el-option v-for="item in sys_normal_disable" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="入场时间" prop="checkoutDate">
          <el-date-picker clearable v-model="form.entryTime" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择入场时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="图片" prop="picture">
          <image-upload v-model="form.picture" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="loading" type="primary" @click="submitForm">提 交</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { MachineryDetailForm, MachineryDetailQuery, MachineryDetailVO } from '@/api/machinery/machineryDetail/types';
import { delMachineryDetail, getMachineryDetail, listMachineryDetail, updateMachineryDetail } from '@/api/machinery/machineryDetail';
import { ref } from 'vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { machinery_entry_exit_type, sys_normal_disable } = toRefs<any>(proxy?.useDict('machinery_entry_exit_type', 'sys_normal_disable'));

interface Props {
  machineryId: string | number;
}

const props = defineProps<Props>();
const ids = ref<Array<string | number>>([]);
const formRef = ref();
const loading = ref(true);
const total = ref<number>(0);
const initFormData: MachineryDetailForm = {
  id: undefined,
  checkoutNumber: undefined,
  checkoutUnit: undefined,
  checkoutDate: undefined,
  status: undefined,
  type: undefined,
  entryTime: undefined,
  remark: undefined,
  picture: undefined,
  machineryId: undefined
};
const data = reactive<PageData<MachineryDetailForm, MachineryDetailQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    checkoutNumber: undefined,
    checkoutUnit: undefined,
    checkoutDate: undefined,
    status: undefined,
    type: undefined,
    entryTime: undefined,
    picture: undefined,
    machineryId: props.machineryId
  },
  rules: {
    checkoutNumber: [{ required: true, message: '请输入检验证编号', trigger: 'blur' }],
    checkoutUnit: [{ required: true, message: '请输入检验单位', trigger: 'blur' }],
    checkoutDate: [{ required: true, message: '请选择检定日期', trigger: 'blur' }],
    entryTime: [{ required: true, message: '请选择进场时间', trigger: 'blur' }]
  }
});

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

const machineryDetailList = ref<MachineryDetailVO[]>([]);
/** 展开选中数据 */
const getList = async () => {
  loading.value = true;
  const res = await listMachineryDetail(queryParams.value);
  machineryDetailList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/** 修改按钮操作 */
const handleUpdate = async (row?: MachineryDetailVO) => {
  const _id = row?.id || ids.value[0];
  const res = await getMachineryDetail(_id);
  Object.assign(form.value, res.data);
  dialogRef.value = true;
};

const dialogRef = ref();
/** 提交按钮 */
const submitForm = () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true;
      await updateMachineryDetail(form.value).finally(() => (loading.value = false));
      proxy?.$modal.msgSuccess('操作成功');
      dialogRef.value = false;
      await getList();
    }
  });
};
/** 取消按钮 */
const closeDialog = () => {
  dialogRef.value = false;
};
onMounted(() => {
  getList();
});
</script>
