<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="planName">
              <el-input v-model="queryParams.planName" 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"> 新增 </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"> 修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"> 删除
            </el-button>
          </el-col> -->
          <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="name" width="150" />
        <el-table-column label="计划编号" align="center" show-overflow-tooltip prop="id" width="150" />
        <el-table-column label="巡检类型" align="center"  width="120" >
          <template #default="scope">
            <dict-tag :options="inspection_type" :value="scope.row.inspection_type" />
          </template>
        </el-table-column>
        <el-table-column label="巡检范围" align="center"  width="120" >
          <template #default="scope">
            <dict-tag :options="inspection_range" :value="scope.row.inspection_range" />
          </template>
        </el-table-column>
        <el-table-column label="巡检周期（天）" align="center"  prop="inspection_cycle"  width="120"/>
        <el-table-column label="巡检开始日期" align="center" show-overflow-tooltip prop="inspection_start_date" width="150" />
        <el-table-column label="巡检完成日期" align="center" show-overflow-tooltip prop="inspection_end_date" width="150" />
        <el-table-column label="巡检员" align="center"  prop="inspection_person_name" width="120" />
        <el-table-column label="巡检结果" align="center" show-overflow-tooltip prop="final_result" 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="showDetail(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" title="巡检记录详情" class="customDrawer" size="80%">
      <div>
        <el-tabs v-model="detailTab">
          <el-tab-pane label="基础信息" name="1">
            <div>
              <el-descriptions class="detail-descriptions" :column="2" border>
                <el-descriptions-item label-align="right" label="计划名称">{{ curRow.name }}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="计划编号">{{ curRow.id }}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="巡检类型"><dict-tag :options="inspection_type" :value="curRow.inspection_type" /></el-descriptions-item>
                <el-descriptions-item label-align="right" label="巡检范围"> <dict-tag :options="inspection_range" :value="curRow.inspection_range" /></el-descriptions-item>
                <el-descriptions-item label-align="right" label="巡检周期（天）">{{ curRow.inspection_cycle }}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="巡检开始日期">{{ curRow.inspection_start_date }}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="巡检完成日期">{{ curRow.inspection_end_date}}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="执行人">{{ curRow.inspection_person_name}}</el-descriptions-item>
                <el-descriptions-item label-align="right" label="备注">{{ curRow.remark }}</el-descriptions-item>
              </el-descriptions>
            </div>
          </el-tab-pane>
          <el-tab-pane label="巡检项目" name="2">
            <el-table :data="curRow.inspectionItemInfo" max-height="300">
          <el-table-column label="设备名称" align="center" prop="device_name">
          </el-table-column>
          <el-table-column label="设备编号" align="center" prop="device_sn">
          </el-table-column>
          <el-table-column label="巡检项目名称" align="center" width="150" prop="name">
          </el-table-column>
          <el-table-column label="检验方法" align="center" show-overflow-tooltip prop="detection_method">
          </el-table-column>
          <el-table-column label="检验标准" align="center" show-overflow-tooltip prop="detection_standard">
          </el-table-column>
          <el-table-column label="判断标准" align="center" show-overflow-tooltip prop="judge_standard">
          </el-table-column>
          <el-table-column label="标准上限" align="center" prop="judge_max_value">
          </el-table-column>
          <el-table-column label="标准下限" align="center" prop="judge_min_value">
          </el-table-column>
          <el-table-column label="巡检结果" width="100" align="center" prop="inspectionResult">
          </el-table-column>
          <el-table-column label="巡检备注说明" width="120" align="center" prop="inspectionRemark">
          </el-table-column>
        </el-table>
          </el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
  </div>
</template>

<script setup name="PatrolRecord" lang="ts">
import {  getInspectionTaskRecordList,getListByRecordId} from '@/api/deviceCheck/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const tableList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const total = ref(0);
const queryFormRef = ref<ElFormInstance>();
const initFormData = {
  id: undefined,
  name: undefined,
  startTime: undefined,
  endTime: undefined,
  inspectionType: undefined,
  inspectionCycle: 1,
  status: 0,
  inspectionRange: undefined,
  inspectionPersonId: undefined,
  inspectionItem: [{
    deviceTypeId: undefined,
    deviceTypeDetailIds: [],
    items: [],
    deviceOptions: [],
  }],
  remark: undefined,
  ext1: <any>{
    phone: undefined,
    inspectionPersonName: undefined,
  },
  ext2: undefined
};
const formData = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    planName: '',
    params: {}
  }
});
const detailTab = ref('1')
const detailDrawer = ref(false)
const curRow = ref({} as any)
const { inspection_type } = toRefs<any>(proxy?.useDict('inspection_type'));
const { inspection_range } = toRefs<any>(proxy?.useDict('inspection_range'));
const { queryParams, form } = toRefs(formData);
/** 查询列表 */
const getList = async () => {
  loading.value = true;
  const res = await getInspectionTaskRecordList(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 showDetail = (row) => {
  detailDrawer.value = true
  detailTab.value = '1'
  curRow.value = row
  getListByRecordId({ recordId: row.record_id }).then(({ data}) => {
    curRow.value.inspectionItemInfo = data
  })
}

onMounted(() => {
  getList();
});
</script>
<style lang="scss" scoped>
.operateBtn {
  display: flex;
  align-items: center;

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