<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="100px">
            <el-form-item label="报修名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入报修名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="报修单号" prop="faultId">
              <el-input v-model="queryParams.faultId" 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">
          <right-toolbar v-model:showSearch="showSearch" @query-table="getList" />
        </el-row>
      </template>

      <el-table v-loading="loading" :data="tableList">
        <el-table-column label="报修单号" align="center" show-overflow-tooltip prop="faultSn" width="150" />
        <el-table-column label="报修名称" align="center" show-overflow-tooltip prop="faultName" width="150" />
        <el-table-column label="报修人" align="center" show-overflow-tooltip prop="createFaultName" width="150" />
        <el-table-column label="报修部门" align="center" show-overflow-tooltip prop="deptName" width="150" />
        <el-table-column label="报修时间" align="center" show-overflow-tooltip prop="createTime" width="150" />
        <el-table-column label="故障等级" align="center" show-overflow-tooltip prop="faultLevel">
          <template #default="{ row }">
            <dict-tag :value="row.faultLevel" :options="fault_level" />
          </template>
        </el-table-column>
        <el-table-column label="是否停机" align="center" show-overflow-tooltip prop="isstop">
          <template #default="{ row }">
            <el-tag v-if="row.isstop == 1" type="danger">是</el-tag>
            <el-tag v-else type="primary">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="设备名称" align="center" show-overflow-tooltip prop="deviceName" width="150" />
        <el-table-column label="设备编号" align="center" show-overflow-tooltip prop="deviceSn" width="150" />
        <el-table-column label="维修人员" align="center" show-overflow-tooltip width="150" prop="repartName" />
        <el-table-column label="预计维修时间" align="center" show-overflow-tooltip prop="requireRepartTime" width="150" />
        <el-table-column label="分派人" align="center" show-overflow-tooltip prop="assignName" width="150" />
        <el-table-column label="分派时间" align="center" show-overflow-tooltip prop="assignTime" width="150" />
        <el-table-column label="工单状态" align="center" show-overflow-tooltip prop="repairStatus" width="150">
          <template #default="{ row }">
            <span :style="{ 'color': row.repairStatus == '正常' ? 'inherit' : 'red' }">{{ row.repairStatus }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="small" link type="primary" @click="handleDeal(scope.row)">处理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
        :total="total" @pagination="getList" />
    </el-card>
    <!-- 添加或修改对话框 -->
    <el-dialog v-model="dialog.visible" :title="dialog.title" width="750px" append-to-body>
      <el-form ref="addFormRef" :model="form" label-width="120px">
        <div class="drawer-title">
          <el-descriptions title="" direction="vertical" :column="5">
            <el-descriptions-item align="center" label="报修单号">{{ form.faultSn }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修名称">{{ form.faultName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修部门">{{ form.deptName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="设备名称">{{ form.deviceName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修时间">{{ form.createTime }}</el-descriptions-item>
          </el-descriptions>
        </div>
        <el-row style="margin-top: 20px;">
          <el-col :span="12">
            <el-form-item label="故障原因" prop="reason"
              :rules="[{ required: true, message: '故障原因不能为空', trigger: 'blur' }]">
              <el-input v-model="form.reason" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="处理方式" prop="faultSolution"
              :rules="[{ required: true, message: '处理方式不能为空', trigger: 'change' }]">
              <el-select v-model="form.faultSolution" clearable placeholder="请选择处理方式">
                <el-option v-for="item in fault_solution" :key="item.value" :label="item.label"
                  :value="item.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="维修说明" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm"> 确 定 </el-button>
          <el-button @click="cancel"> 取 消 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OuterWorkorder" lang="ts">
import { getWorkTaskList, updateWorkTask } from '@/api/deviceMaintain/index';
import { deepClone, dateFormat } from '@/utils';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const tableList = ref([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const addFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const initFormData = {
  reason: undefined,
  faultSolution: undefined,
  remark: undefined,
} as any;
const formData = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    faultId: undefined,
    dealStatus: '0',
    type: '2',
    params: {}
  }
});
const { queryParams, form } = toRefs(formData);
const { fault_level } = toRefs<any>(proxy?.useDict('fault_level'));
const { fault_solution } = toRefs<any>(proxy?.useDict('fault_solution'));
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const res = await getWorkTaskList(queryParams.value);
  tableList.value = res.rows
  total.value = res.total;
  loading.value = false;
};

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

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

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

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


/** 提交按钮 */
const submitForm = () => {
  addFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      const { id, reason, faultSolution, remark } = form.value;
      await updateWorkTask({
        id,
        reason,
        faultSolution,
        remark,
        completeTime: dateFormat(new Date(), 'yyyy-MM-dd HH:mm:ss')
      })
      buttonLoading.value = false;
      proxy?.$modal.msgSuccess('处理成功')
      dialog.visible = false;
      getList();
    }
  });
};
const handleDeal = async (row) => {
  Object.assign(form.value, row)
  dialog.visible = true;
  dialog.title = '内部维修工单处理';
};
onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.operateBtn {
  display: flex;
  align-items: center;

  .el-icon {
    color: #409eff;
    margin-left: 5px;
    cursor: pointer;
  }
}

.drawer-title {
  :deep(.el-descriptions__body) {
    margin-top: 10px;
    background-color: #F4F5F7;
    border-radius: 2px;
    padding: 10px 10px 0 10px;
  }

  :deep(.el-descriptions__label) {
    padding-bottom: 5px;
  }

  .title-name {
    color: #000;
  }
}
</style>
