<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="80px">
            <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="sn">
              <el-input v-model="queryParams.sn" placeholder="请输入设备编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="使用状态" prop="status">
              <el-select v-model="queryParams.status" clearable placeholder="请选择使用状态">
                <el-option v-for="dict in use_status" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </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 v-hasPermi="['jdyw:deviceType:add']" type="primary" plain icon="Plus" @click="handleAdd"> 新增
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['jdyw:deviceType:edit']" type="success" plain icon="Edit" :disabled="single"
              @click="handleUpdate">
              修改
            </el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button v-hasPermi="['jdyw:deviceType:remove']" 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="deviceList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="设备名称" align="center" prop="name" width="120" />
        <el-table-column label="设备编号" align="center" prop="sn" />
        <!-- <el-table-column label="所属系统" align="center" prop="deviceSystem" /> -->
        <el-table-column label="品牌" align="center" prop="ext1.brand" />
        <el-table-column label="型号" align="center" prop="ext1.xh" />
        <!-- <el-table-column label="固定资产编号" align="center" prop="fixedId" width="120" /> -->
        <el-table-column label="领用日期" align="center" width="100">
          <template #default="scope">
            {{ scope.row.useTime.substring(0, 10) }}
          </template>
        </el-table-column>
        <el-table-column label="质保日期" align="center" width="100">
          <template #default="scope">
            {{ scope.row.qualityEndTime.substring(0, 10) }}
          </template>
        </el-table-column>
        <!-- <el-table-column label="维保状态" align="center" prop="qaStatus" /> -->
        <el-table-column label="使用状态" align="center">
          <template #default="scope">
            <dict-tag :options="use_status" :value="scope.row.status" />
          </template>
        </el-table-column>
        <!-- <el-table-column label="运行状态" align="center" prop="runningStatus" /> -->
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="180">
          <template #default="scope">
            <el-button size="small" link type="primary" @click="showDetail(scope.row)">详情</el-button>
            <el-button size="small" link type="primary" @click="showQrCode(scope.row)">二维码</el-button>
            <el-button v-if="scope.row.status != 3" size="small" link type="primary"
              @click="handleUpdate(scope.row)">修改</el-button>
            <el-button v-if="scope.row.status != 3" size="small" link type="danger"
              @click="handleScrap(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="800px" append-to-body>
      <el-form ref="deviceFormRef" :model="form" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="设备名称" prop="name" :rules="[{ required: true, message: '设备名称不能为空', trigger: 'blur' }]">
              <el-input v-model="form.name" :disabled="ifEdit" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备编号" prop="sn" :rules="[{ required: true, message: '设备编号不能为空', trigger: 'blur' }]">
              <el-input v-model="form.sn" :disabled="ifEdit" placeholder="请输入设备编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备类型" prop="devTypeDetailId"
              :rules="[{ required: true, message: '设备类型不能为空', trigger: 'change' }]">
              <el-select v-model="form.devTypeDetailId" :disabled="ifEdit" clearable placeholder="请选择设备类型"
                @change="devTypeDetailChange">
                <el-option v-for="dict in dictGroup.deviceTypeDetailOptions" :key="dict.id"
                  :label="dict.deviceType.name" :value="dict.id">
                  <span style="float: left">{{ dict.deviceType.name }}</span>
                  <span style="float: right;color: var(--el-text-color-secondary);font-size: 13px;">
                    {{ dict.ext1.brand }}{{ dict.xh }}
                  </span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="品牌" prop="ext1.brand">
              <el-input v-model="form.ext1.brand" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="型号" prop="ext1.xh">
              <el-input v-model="form.ext1.xh" disabled />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="领用日期" prop="useTime"
              :rules="[{ required: true, message: '领用日期不能为空', trigger: 'change' }]">
              <el-date-picker v-model="form.useTime" value-format="YYYY-MM-DD hh:mm:ss" style="width: 100%"
                :disabled="ifEdit" type="date" placeholder="请选择领用日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="质保日期" prop="qualityEndTime"
              :rules="[{ required: true, message: '质保日期不能为空', trigger: 'change' }]">
              <el-date-picker v-model="form.qualityEndTime" value-format="YYYY-MM-DD hh:mm:ss" style="width: 100%"
                :disabled="ifEdit" type="date" placeholder="请选择质保日期" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="使用状态" prop="status"
              :rules="[{ required: true, message: '使用状态不能为空', trigger: 'change' }]">
              <el-select v-model="form.status" clearable placeholder="请选择使用状态">
                <el-option v-for="dict in use_status.filter(item => item.value != 3)" :key="dict.value"
                  :label="dict.label" :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="地理位置">
          <div id="map" class="bdmap"></div>
        </el-form-item>
      </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>
    <el-drawer v-model="drawer" class="customDrawer" size="40%">
      <template #header>
        <div class="drawer-title">
          <div class="title-name">报废设备</div>
        </div>
      </template>
      <el-form ref="deviceScrapFormRef" :model="scrapForm" label-width="80px">
        <el-form-item label="设备名称">
          <div>{{ scrapForm.name }}</div>
        </el-form-item>
        <el-form-item label="设备编号">
          <div>{{ scrapForm.sn }}</div>
        </el-form-item>
        <el-form-item label="品牌">
          <div>{{ scrapForm.ext1.brand }}</div>
        </el-form-item>
        <el-form-item label="型号">
          <div>{{ scrapForm.ext1.xh }}</div>
        </el-form-item>
        <el-form-item label="报废理由" prop="reason" :rules="[{ required: true, message: '报废理由不能为空', trigger: 'blur' }]">
          <el-input v-model="scrapForm.reason" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="报废时间" prop="scrapDate"
          :rules="[{ required: true, message: '报废时间不能为空', trigger: 'change' }]">
          <el-date-picker v-model="scrapForm.scrapDate" style="width: 100%" type="date" placeholder="请选择" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="flex: auto">
          <el-button @click="drawer = false">取消</el-button>
          <el-button type="primary" @click="confirmClick">确认</el-button>
        </div>
      </template>
    </el-drawer>
    <el-dialog v-model="qrDialog" title="设备二维码" width="600px" append-to-body>
      <el-descriptions class="custom-descriptions" title="" :column="1" border>
        <el-descriptions-item label="设备名称"> {{ qrInfo.name }} </el-descriptions-item>
        <el-descriptions-item label="设备编号"> {{ qrInfo.sn }} </el-descriptions-item>
        <el-descriptions-item label="设备品牌"> {{ qrInfo.ext1.brand }} </el-descriptions-item>
        <el-descriptions-item label="设备型号"> {{ qrInfo.ext1.xh }} </el-descriptions-item>
        <el-descriptions-item label="存放位置"> {{ qrInfo.ext1.address }} </el-descriptions-item>
      </el-descriptions>
      <img class="qrImg" src="@/assets/images/qrcode.png" alt="" />
    </el-dialog>
    <el-drawer v-model="detailDrawer" class="customDrawer" size="70%">
      <template #header>
        <div class="drawer-title">
          <div class="title-name">设备详情</div>
          <el-descriptions title="" direction="vertical" :column="5">
            <el-descriptions-item align="left" label="设备名称">{{ curRow.name }}</el-descriptions-item>
            <el-descriptions-item align="left" label="设备编号">{{ curRow.sn }}</el-descriptions-item>
            <el-descriptions-item align="left" width="150" label="品牌">{{ curRow.ext1.brand }}</el-descriptions-item>
            <el-descriptions-item align="left" label="设备型号">{{ curRow.ext1.xh }}</el-descriptions-item>
            <el-descriptions-item align="left" label="所属系统">{{ curRow.systemName }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </template>
      <div>
        <el-tabs v-model="detailTab">
          <el-tab-pane label="基础信息" name="1">
            <el-collapse v-model="detailCollapse">
              <el-collapse-item name="1">
                <template #title>
                  <SubTitle title="设备信息" />
                </template>
                <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.sn }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="所属系统">{{ curRow.systemName
                      }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="品牌">{{ curRow.ext1.brand }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="设备型号">{{ curRow.ext1.xh }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="供应商">{{ curRow.productorName
                      }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="供应商联系人">{{ curRow.linkName
                      }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="联系方式">{{ curRow.linkPhone }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="领用日期">{{
                      curRow.useTime && curRow.useTime.substring(0, 11)
                    }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="质保日期">{{
                      curRow.qualityEndTime && curRow.qualityEndTime.substring(0, 11) }}</el-descriptions-item>
                    <el-descriptions-item label-align="right" label="使用状态">
                      <dict-tag :options="use_status" :value="curRow.status" />
                    </el-descriptions-item>
                    <el-descriptions-item label-align="right" label="备注">{{ curRow.remark }}</el-descriptions-item>
                  </el-descriptions>
                </div>
              </el-collapse-item>
              <el-collapse-item name="2">
                <template #title>
                  <SubTitle title="位置信息" />
                </template>
                <div>
                  <div id="positionMap" class="bdmap"></div>
                </div>
              </el-collapse-item>
              <el-collapse-item name="3">
                <template #title>
                  <SubTitle title="设备图片" />
                </template>
                <div>
                </div>
              </el-collapse-item>
            </el-collapse>
          </el-tab-pane>
          <el-tab-pane label="生命历程" name="2">
            <el-timeline style="max-width: 800px">
              <el-timeline-item center timestamp="" placement="top">
                <el-card>
                  <div class="timeline-label">
                    <div>领用</div>
                    <div>张姗姗</div>
                    <div>2024/08/12 12:00:00</div>
                    <div>备件库领用</div>
                  </div>
                </el-card>
              </el-timeline-item>
              <el-timeline-item center timestamp="" placement="top">
                <el-card>
                  <div class="timeline-label">
                    <div>使用</div>
                    <div>王网</div>
                    <div>2024/08/24 12:00:00</div>
                    <div>用于项目</div>
                  </div>
                </el-card>
              </el-timeline-item>
            </el-timeline>
          </el-tab-pane>
          <el-tab-pane label="物模型" name="3">
            <el-tabs v-model="detailModel" type="card">
              <el-tab-pane label="属性" name="1">
                <el-table :data="curRow.dataPoints">
                  <el-table-column label="属性" align="center" prop="name" />
                  <el-table-column label="属性值" align="center" prop="value" />
                  <el-table-column label="单位" align="center" prop="unit" />
                  <el-table-column label="描述" align="center" prop="remark" />
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="能力" name="2">
                <el-table :data="curRow.controlFuncs" >
                  <el-table-column label="能力名称" align="center" prop="name" />
                  <el-table-column label="下发参数" align="center" prop="value" />
                  <el-table-column label="描述" align="center" prop="remark" />
                </el-table>
              </el-tab-pane>
              <el-tab-pane label="事件" name="3">
                <el-table :data="curRow.events" >
                  <el-table-column label="事件名称" align="center" prop="name" />
                  <el-table-column label="事件类型" align="center" prop="type" />
                  <el-table-column label="事件代码" align="center" prop="code" />
                  <el-table-column label="外部事件代码" align="center" prop="outerCode" />
                  <el-table-column label="描述" align="center" prop="remark" />
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </el-tab-pane>
          <el-tab-pane label="运行状态" name="4"></el-tab-pane>
          <el-tab-pane label="维修记录" name="5">
            <el-table :data="repairData" >
                  <el-table-column label="报修工单" align="center" prop="name" />
                  <el-table-column label="报修人" align="center" prop="type" />
                  <el-table-column label="报修部门" align="center" prop="code" />
                  <el-table-column label="故障时间" align="center" prop="outerCode" />
                  <el-table-column label="故障等级" align="center" prop="remark" />
                  <el-table-column label="是否停机" align="center" prop="remark" />
                  <el-table-column label="维修人员" align="center" prop="remark" />
                  <el-table-column label="工单完工时间" align="center" prop="remark" />
                  <el-table-column label="维修备注说明" align="center" prop="remark" />
                </el-table>
          </el-tab-pane>
          <el-tab-pane label="巡检记录" name="6">
            <el-table :data="recordData" >
                  <el-table-column label="工单号" align="center" prop="name" />
                  <el-table-column label="计划名称" align="center" prop="type" />
                  <el-table-column label="计划编号" align="center" prop="code" />
                  <el-table-column label="巡检周期（天）" align="center" prop="outerCode" />
                  <el-table-column label="巡检项" align="center" prop="remark" />
                  <el-table-column label="计划巡检日期" align="center" prop="remark" />
                  <el-table-column label="巡检完成日期" align="center" prop="remark" />
                  <el-table-column label="执行人" align="center" prop="remark" />
                </el-table>
          </el-tab-pane>
          <el-tab-pane label="附件" name="7"></el-tab-pane>
        </el-tabs>
      </div>
    </el-drawer>
  </div>
</template>

<script setup name="Archives" lang="ts">
import { listDevice, getDevice, delDevice, addDevice, updateDevice } from '@/api/deviceManage/device';
import { listDeviceType, getDeviceType, getDeviceTypeDetail, getDeviceTypeDetailList, getModels } from '@/api/deviceManage/deviceType';
import { getProductor } from '@/api/supplierManage/index';
import { deepClone } from '@/utils';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const deviceList = ref([]);
const buttonLoading = ref(false);
const loading = ref(false);
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 deviceFormRef = ref<ElFormInstance>();
const deviceScrapFormRef = ref<ElFormInstance>();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const qrDialog = ref(false);
const dictGroup = reactive({
  deviceTypeDetailOptions: [],
})
const { use_status } = toRefs<any>(proxy?.useDict('use_status'));
const bdmap = ref(null);
const positionMap = ref(null);
const scrapForm = ref<any>({
  reason: '',
  scrapDate: ''
});
const qrInfo = ref<any>({});
const initFormData = {
  id: '',
  name: '',
  sn: '',
  devTypeDetailId: '',
  productorId: '',
  modelId: '',
  useTime: '',
  qualityEndTime: '',
  status: '',
  remark: '',
  dataPoints: '{}',
  controlFuncs: '{}',
  events: '{}',
  ext1: <any>{
    brand: '',
    xh: '',
    longitude: '',
    latitude: '',
    address: ''
  },
  ext2: '{}'
};
const formData = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    sn: undefined,
    status: undefined,
    params: {}
  }
});

const { queryParams, form } = toRefs(formData);
let ifEdit = computed(() => {
  return dialog.title.includes('修改');
});
const drawer = ref(false);
const detailDrawer = ref(false);
const curRow = ref<any>({ ext1: {} })
const detailTab = ref('1')
const detailCollapse = ref(['1', '2'])
const detailModel = ref('1')
const repairData = ref([]);
const recordData = ref([]);
/** 查询设备类型信息列表 */
const getList = async () => {
  loading.value = true;
  const res = await listDevice(queryParams.value);
  deviceList.value = res.rows.map((item) => ({
    ...item,
    controlFuncs: item.controlFuncs !== '{}' ? JSON.parse(item.controlFuncs) : [],
    dataPoints: item.dataPoints !== '{}' ? JSON.parse(item.dataPoints) : [],
    events: item.events !== '{}' ? JSON.parse(item.events) : [],
    ext1: item.ext1 ? JSON.parse(item.ext1) : null
  }));;
  total.value = res.total;
  loading.value = false;
};
const showQrCode = (row) => {
  qrDialog.value = true;
  Object.assign(qrInfo.value, row);
};
const initMap = () => {
  var map = new BMapGL.Map('map'); // 创建地图实例
  var point = new BMapGL.Point(118.879999, 32.016216); // 创建点坐标
  map.centerAndZoom(point, 20);
  map.enableScrollWheelZoom(true);
  const geoc = new BMapGL.Geocoder();
  map.addEventListener('click', (evt) => {
    bdmap.value.clearOverlays();
    const { lng, lat } = evt.latlng;
    form.value.ext1.longitude = lng
    form.value.ext1.latitude = lat
    const pt = new BMapGL.Point(lng, lat);
    const marker = new BMapGL.Marker(pt);
    bdmap.value.addOverlay(marker);
    geoc.getLocation(pt, function (rs) {
      var addComp = rs.addressComponents;
      var address = addComp.province + addComp.city + addComp.district + addComp.street + addComp.streetNumber;
      form.value.ext1.address = address
      var label = new BMapGL.Label(address, {       // 创建文本标注
        position: pt,                          // 设置标注的地理位置
        offset: new BMapGL.Size(-90, 0)           // 设置标注的偏移量
      })
      label.setStyle({                              // 设置label的样式
        color: '#000',
        fontSize: '14px',
        border: 'none',
        background: 'transparent',
        fontWeight: 400
      })
      bdmap.value.addOverlay(label);
    })

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

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

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

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

/** 多选框选中数据 */
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '新增设备';
  setTimeout(() => {
    initMap();
  }, 100);
};

/** 修改按钮操作 */
const handleUpdate = async (row) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getDevice(_id);
  res.data.ext1 = res.data.ext1 && JSON.parse(res.data.ext1);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改设备';
  setTimeout(() => {
    initMap();
    const { longitude, latitude, address } = form.value.ext1
    if (longitude) {
      const pt = new BMapGL.Point(longitude, latitude);
      const marker = new BMapGL.Marker(pt);
      bdmap.value.addOverlay(marker);
      var label = new BMapGL.Label(address, {       // 创建文本标注
        position: pt,                          // 设置标注的地理位置
        offset: new BMapGL.Size(-90, 0)           // 设置标注的偏移量
      })
      label.setStyle({                              // 设置label的样式
        color: '#000',
        fontSize: '14px',
        border: 'none',
        background: 'transparent',
        fontWeight: 400
      })
      bdmap.value.addOverlay(label);
      bdmap.value.centerAndZoom(pt, 20);
    }
  }, 100);
};
const handleScrap = (row) => {
  drawer.value = true;
  deviceScrapFormRef.value?.resetFields();
  Object.assign(scrapForm.value, deepClone(row));
};
/** 提交按钮 */
const submitForm = () => {
  deviceFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      const { ext1 } = form.value;
      const params = Object.assign(form.value, { ext1: JSON.stringify(ext1) });
      if (form.value.id) {
        await updateDevice(params).finally(() => (buttonLoading.value = false));
      } else {
        await addDevice(params).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除？').finally(() => (loading.value = false));
  await delDevice(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};
const getDeviceTypeList = async () => {
  let deviceTypeList = []
  await listDeviceType({}).then(({ code, rows }) => {
    if (code === 200) {
      deviceTypeList = rows || []
    }
  })
  let deviceTypeDetailList = []
  await getDeviceTypeDetailList({}).then(({ code, rows }) => {
    if (code === 200) {
      deviceTypeDetailList = rows.map((item) => ({
        ...item,
        deviceType: {},
        ext1: item.ext1 ? JSON.parse(item.ext1) : null
      }));
    }
  });
  deviceTypeDetailList.forEach(item => {
    deviceTypeList.forEach(el => {
      if (item.deviceTypeId === el.id) {
        item.deviceType = el
      }
    })
  })
  dictGroup.deviceTypeDetailOptions = deviceTypeDetailList
};
const devTypeDetailChange = (val) => {
  if (val) {
    const [{ ext1: { brand }, xh, productorId, deviceType: { modeId } }] = dictGroup.deviceTypeDetailOptions.filter(item => item.id === val)
    form.value.ext1.brand = brand
    form.value.ext1.xh = xh
    form.value.productorId = productorId
    form.value.modelId = modeId
    getModels(modeId).then(({ code, data }) => {
      if (code === 200) {
        form.value.dataPoints = data.dataPoints || '{}';
        form.value.controlFuncs = data.controlFuncs || '{}';
        form.value.events = data.events || '{}';
      }
    });
  }

}
const confirmClick = () => {
  deviceScrapFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      drawer.value = false;
      const { ext1, reason, scrapDate } = scrapForm.value;
      ext1.reason = reason
      ext1.scrapDate = scrapDate
      const params = Object.assign(scrapForm.value, { ext1: JSON.stringify(ext1), status: '3' });
      await updateDevice(params)
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};
const initPositionMap = () => {
  const map = new BMapGL.Map('positionMap'); // 创建地图实例
  map.enableScrollWheelZoom(true);
  positionMap.value = map;
  const { longitude, latitude, address } = curRow.value.ext1
  if (longitude) {
    const pt = new BMapGL.Point(longitude, latitude);
    const marker = new BMapGL.Marker(pt);
    positionMap.value.addOverlay(marker);
    var label = new BMapGL.Label(address, {       // 创建文本标注
      position: pt,                          // 设置标注的地理位置
      offset: new BMapGL.Size(-90, 0)           // 设置标注的偏移量
    })
    label.setStyle({                              // 设置label的样式
      color: '#000',
      fontSize: '14px',
      border: 'none',
      background: 'transparent',
      fontWeight: 400
    })
    positionMap.value.addOverlay(label);
    positionMap.value.centerAndZoom(pt, 20);
  }
}
const showDetail = async (row) => {
  detailDrawer.value = true
  detailTab.value = '1'
  detailCollapse.value = ['1', '2']
  detailModel.value = '1'
  curRow.value = row
  await getProductor(row.productorId).then(({ data }) => {
    curRow.value.productorName = data.name
    curRow.value.linkName = data.contact
    curRow.value.linkPhone = data.phone
  })
  await getDeviceTypeDetail(row.devTypeDetailId).then(res => {
    getDeviceType(res.data.deviceTypeId).then(result => {
      if (result.data.ext1) {
        curRow.value.systemName = JSON.parse(result.data.ext1).systemName
      }
    })
  })
  initPositionMap()
}
onMounted(() => {
  getList();
  getDeviceTypeList();
});
</script>
<style lang="scss" scoped>
.bdmap {
  width: 100%;
  height: 250px;
}

.custom-descriptions {
  :deep(.is-bordered-label) {
    width: 25%;
    font-weight: 500;
  }
}

.detail-descriptions {
  :deep(.is-bordered-label) {
    width: 15%;
  }
}

.timeline-label {
  display: flex;
  align-items: center;

  >div {
    flex: 1;

    &:first-child {
      font-weight: 500;
    }
  }

}

.qrImg {
  position: absolute;
  bottom: 35px;
  right: 35px;
  height: 150px;
}
</style>
<style lang="scss">
.customDrawer {
  .el-drawer__header {
    align-items: flex-start !important;
    margin-bottom: 10px;

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

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

      .title-name {
        color: #000;
      }
    }
  }

  .el-drawer__body {
    padding-top: 0;

    .el-collapse-item__content {
      padding-bottom: 8px;
    }
  }
}
</style>
