<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="type" width="150">
          <template #default="{ row }">
            {{ row.type == '1' ? '内部工单' : '外部工单' }}
          </template>
        </el-table-column>
        <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="faultTime" width="150" />
        <el-table-column label="故障等级" align="center">
          <template #default="{ row }">
            <dict-tag :value="row.faultLevel" :options="fault_level" />
          </template>
        </el-table-column>
        <el-table-column label="是否停机" align="center">
          <template #default="{ row }">
            {{ row.isstop == 1 ? '是' : '否' }}
          </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="120" prop="repartName" />
        <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="completeTime" width="150" />
        <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="handleDetail(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-drawer v-model="detailDrawer" class="customDrawer" size="60%">
      <template #header>
        <div class="drawer-title">
          <div class="title-name">维修工单详情</div>
          <el-descriptions title="" direction="vertical" :column="6">
            <el-descriptions-item align="center" label="报修单号">{{ curRow.faultSn }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修名称">{{ curRow.faultName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修部门">{{ curRow.deptName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="报修人">{{ curRow.createFaultName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="设备名称">{{ curRow.deviceName }}</el-descriptions-item>
            <el-descriptions-item align="center" label="设备编号">{{ curRow.deviceSn }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </template>
      <div>
        <el-tabs v-model="detailTab">
          <el-tab-pane label="故障信息" name="1">
            <el-descriptions class="detail-descriptions" :column="2" border>
              <el-descriptions-item label-align="center" label="故障发生时间">{{ curRow.faultTime
                }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="故障等级"> <dict-tag :value="curRow.faultLevel"
                  :options="fault_level" /></el-descriptions-item>
              <el-descriptions-item label-align="center" label="故障类型">{{ curRow.faultTypeName }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="是否停机">{{ curRow.isstop == 1 ? '是' : '否'
                }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="故障描述">{{ curRow.faultRemark }}</el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
          <el-tab-pane label="维修信息" name="2">
            <SubTitle title="维修工单" style="margin-bottom: 10px; " />
            <el-descriptions class="detail-descriptions" :column="2" border>
              <el-descriptions-item label-align="center" label="工单名称">{{ curRow.faultName }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="工单编号">{{ curRow.id }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="分派人员">{{ curRow.assignName }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="分派时间">{{ curRow.assignTime }}</el-descriptions-item>
            </el-descriptions>
            <SubTitle title="处理情况" style="margin: 10px 0; " />
            <el-descriptions class="detail-descriptions" :column="2" border>
              <el-descriptions-item label-align="center" label="故障原因">{{ curRow.repairInfo.reason
                }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="处理方式">
                <dict-tag :value="curRow.repairInfo.faultSolution" :options="fault_solution" />
              </el-descriptions-item>
              <el-descriptions-item label-align="center" label="维修人员">{{ curRow.repartName }}</el-descriptions-item>
              <el-descriptions-item label-align="center" label="维修时间">{{curRow.completeTime}}</el-descriptions-item>
            </el-descriptions>
          </el-tab-pane>
          <el-tab-pane label="处理进度" name="3">
            <el-table :data="curRow.dealProgress" max-height="300">
              <el-table-column label="节点名称" align="center" prop="nodeName">
              </el-table-column>
              <el-table-column label="耗时（分钟）" align="center" prop="costTime">
              </el-table-column>
              <el-table-column label="操作人" align="center" prop="createName">
              </el-table-column>
              <el-table-column label="操作时间" align="center" show-overflow-tooltip prop="createTime">
              </el-table-column>
              <el-table-column label="状态" align="center" show-overflow-tooltip prop="status">
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
  </div>
</template>

<script setup name="Record" lang="ts">
import { getWorkTaskList, getWorkTaskInfo ,getOrderProgress} from '@/api/deviceMaintain/index';
import { listUser } from '@/api/system/user/index'
import { listProductorEngineers } from '@/api/supplierManage/index'
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const detailDrawer = ref(false);
const detailTab = ref('1');
const curRow = ref({ repairInfo: {} } as any);
const initFormData = {} as any;
const formData = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    faultId: undefined,
    dealStatus: '1',
    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 userOptions = ref([])
const engineersOptions = ref([])
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const res = await getWorkTaskList(queryParams.value);
  tableList.value = res.rows
  total.value = res.total;
  loading.value = false;
};



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

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


const handleDetail = async (row) => {
  detailDrawer.value = true;
  detailTab.value = '1';
  Object.assign(curRow.value, row)
  getWorkTaskInfo(row.id).then(({ code, data }) => {
    if (code === 200) {
      curRow.value.repairInfo = data
    }
  })
  getOrderProgress(curRow.value.faultSn).then(({ code, data }) => {
    if (code === 200) {
      curRow.value.dealProgress = data
    }
  })
};
const getUserList = () => {
  listUser({ status: '0', pageNum: 1, pageSize: 10000 }).then(({ code, rows }) => {
    if (code === 200) {
      userOptions.value = rows
    }
  })
}
const getEngineersList = () => {
  listProductorEngineers({}).then(({ code, rows }) => {
    if (code === 200) {
      engineersOptions.value = rows
    }
  })
}
onMounted(() => {
  getList();
  getUserList();
  getEngineersList();
});
</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>
