<template>
  <div>
    <el-form ref="queryFormRef" :model="queryParams" :inline="true" style="margin-bottom: -20px;margin-top:-10px"
      :disabled="createUnitFalg!=='1'&&userType!=='0'" @keyup.enter.native="queryList">
      <el-form-item label="安装人:">
        <el-input v-model="queryParams.setupMan" placeholder="安装人" width="120px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="button" :disabled="state.search_disabled" @click="queryList">
          查询记录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
  <div>
    <el-table v-loading="loading" :data="state.tableData" border stripe style="width: 100%" :height="tableHeight"
      highlight-current-row @current-change="handleCurrentRowChange" :row-style="{height:'10px'}"
      :cell-style="{padding:'5px 0'}" :header-cell-style="{ background: '#606266', color: '#FFFFFF' }">
      <el-table-column fixed type="index" width="50" align="center" />
      <el-table-column fit prop="stationCode" label="遥测编码" header-align="center" width="100" />
      <el-table-column prop="stationName" label="遥测站名称" header-align="center" width="190" show-overflow-tooltip />
      <el-table-column prop="deviceSn" label="设备SN" header-align="center" width="120" />
      <el-table-column prop="floorRsrp" label="信号值" header-align="center" width="65" />
      <el-table-column prop="stationContact" label="站点联系方式" header-align="center" width="150" show-overflow-tooltip />
      <el-table-column prop="stationAddress" label="站点详细地址" header-align="center" min-width="50"
        show-overflow-tooltip />
      <el-table-column prop="stationLon" label="站点经度" header-align="center" width="110" />
      <el-table-column prop="stationLat" label="站点纬度" header-align="center" width="110" />
      <el-table-column prop="setupMan" label="安装人" header-align="center" width="100" />
      <el-table-column prop="setupTime" label="安装时间" header-align="center" width="138">
        <template #default="scope">
          {{ tool.dateFormatYMDHm(scope.row.setupTime,"YYYYMMDDHHmmss",'YY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>

      <el-table-column label="安装状态" header-align="center" align="center" width="100">
        <template #default="scope">
          <el-tag :type="deviceTypeColorMap[scope.row.finishedFlag]">{{ finishedFlagMap[scope.row.finishedFlag] }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="finishedTime" label="完成时间" header-align="center" width="100">
        <template #default="scope">
          <span>{{ tool.dateFormatYMDHm(scope.row.finishedTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="finishedMan" label="完成人" header-align="center" width="90" />
      <el-table-column label="操作" align="left" width="55" v-if="adminFlag&&userType==='0'">
        <template #default="scope">
          <el-button type="text" @click="submitDeleteForm(scope.row)" :disabled="!adminFlag">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-config-provider :locale="locale">
      <el-pagination v-model:currentPage="queryParams.page" v-model:page-size="queryParams.rows"
        :page-sizes="[10, 15, 20, 25, 100]" :small="state.small" :disabled="state.disabled"
        :background="state.background" layout="total, sizes, prev, pager, next, jumper" :total="state.total"
        style="text-align: center; margin-top: 10px; height: 30px" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </el-config-provider>

    <div class="block">
      <ul style="display:flex;overflow-x: scroll;overflow-y: hidden;background-color: transparent;">
        <li v-for="item in state.picModelList" :key="item.id"
          style="float: left;margin-right:30px;display::inline-block">
          <el-image :src="item.picUrl" :preview-src-list="state.imgList" :append-to-body="true" fit="contain"
            :hide-on-click-modal="true"
            style="background-color: transparent;height:260px;float:center;width: 170px;margin: 5px 1px;">
          </el-image>
          <h4 style="color: white;margin-left: 10px;">{{ item.picName }}</h4>
        </li>
      </ul>
      <!-- <el-carousel :interval="4000" type="card" direction="vertical" style="height: 100%;">
            <el-carousel-item v-for="item in state.picModelList" :key="item" style="height: 480px;width: 100%;">
              <div>
                <el-image :src="item.picUrl" :preview-src-list="state.imgList" :append-to-body="true" fit="cover" :height="tableHeight"
                  :hide-on-click-modal="true" style="background-color: aqua;">
                </el-image>
                <h3>{{ item.picName }}</h3>
              </div>
            </el-carousel-item>
          </el-carousel> -->
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, toRefs, toRef, ref, onMounted } from "vue";
import { ElForm, ElImage, ElMessage, ElMessageBox } from "element-plus";
import TSetupAction from "@/api/constructionmanagement/tsetup";
import { TSetupQueryParam, TSetupModel, Dialog, PageResult, TSetupPicModel } from "@/types";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import tool from '@/until/tool';

let locale = ref(zhCn);
const userType = localStorage.getItem("UserType")?.toString();
const adminFlag = localStorage.getItem("AdminFlag")?.toString() === '1';
const createUnitFalg = localStorage.getItem("CreateUnitFalg") + "";

type mapStatee = Record<string, string>;

const deviceTypeColorMap: mapStatee = {
  0: '',
  1: 'success',
};

const finishedFlagMap: mapStatee = {
  0: "正在安装",
  1: "安装完成",
};

const loading = ref(false);

const state = reactive({
  queryParams: { page: 1, rows: 10 } as TSetupQueryParam,
  currentType: 0,//val=0 新增; =1 批量新增一; =2 批量新增二; =3 编辑;
  total: 0,
  small: false,
  background: true,
  disabled: false,
  search_disabled: false,
  formAddData: {} as TSetupModel,
  formEditData: {} as TSetupModel,
  tableData: [] as TSetupModel[],
  imgList: [] as string[],
  picModelList: [] as TSetupPicModel[],
});

const {
  queryParams,
  total,
  formAddData,
  formEditData,
  tableData,
  imgList
} = toRefs(state);

const tableHeight = ref(document.documentElement.clientHeight - 530);

const currentRow = ref();

const handleCurrentRowChange = (val: TSetupModel | undefined) => {
  currentRow.value = val;
  state.imgList = [];
  state.picModelList = [];
  if (val !== undefined && val !== null) {
    state.imgList = [];

    if (val.picList !== null && val.picList !== undefined) {
      val.picList.forEach(s => {
        state.imgList.push(s.picUrl);
        state.picModelList.push(s);
      }
      );
    }
  }
};

const queryList = () => {
  loading.value = true;
  state.search_disabled = true;
  TSetupAction.QueryTSetupList(state.queryParams)
    .then((result) => {
      if (result.status === 200) {
        const { res, flag, error } = result.data;
        if (flag === "1") {
          const pageData = res as PageResult<TSetupModel[]>;
          state.tableData = pageData.list;
          state.total = pageData.total;

        } else {
          ElMessage.error(error);
        }
        loading.value = false;
        state.search_disabled = false;
      }
      else {
        state.search_disabled = false;
        loading.value = false;
      }
    })
    .catch((error) => {
      loading.value = false;
      state.search_disabled = false;
    });
  loading.value = false;
  state.search_disabled = false;
};

const handleSizeChange = (val: number) => {
  queryParams.value.rows = val;
  queryList();
};

const handleCurrentChange = (val: number) => {
  queryParams.value.page = val;
  queryList();
};


const submitDeleteForm = (row: any) => {
  ElMessageBox.confirm(" 是否删除安装记录[" + row.deviceSn + "]? ", "提示", {
    cancelButtonText: "取消",
    confirmButtonText: "确认",
    type: "warning",
    center: true,
  })
    .then(() => {
      TSetupAction.DeleteTSetup(row.id, row.setupId).then((result) => {
        const { res, flag, error } = result.data;
        if (flag === "1") {
          ElMessage.success("删除成功");
          queryList();
        }
        else {
          ElMessage.error(error || "未知异常...");
        }
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除成功!",
      });
    });
};

onMounted(() => {
  window.onresize = function () {
    tableHeight.value = document.documentElement.clientHeight - 530;
  };

  if (createUnitFalg === '1' || userType === '0') {
    queryList();
  }
});
</script>

<style lang="scss" scoped>
.button {
  margin: 10px 0;
}

// .el-carousel__item h3 {
//   color: #475669;
//   opacity: 0.75;
//   line-height: 200px;
//   margin: 0;
//   text-align: center;
// }

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.el-carousel__item h3 {
  color: rgb(253, 246, 246);
  font-size: 0.3rem;
  opacity: 0.75;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.block {
  width: 100%;
  height: 100%;
  background-color: black;
}

.small {
  width: 100%;
  height: 100%;
  position: relative;
}

.pic_item {
  position: relative;
  height: 100%;
}

.pic_item:hover {
  cursor: pointer;
}

.pic_item img {
  width: 100%;
  height: 100%;
}

.pic_item h3 {
  position: absolute;
  left: 1rem;
  bottom: 2rem;
}

#box ul {
  display: flex;
  flex-wrap: wrap;
  position: absolute;
  top: 220px;
}

#box li {
  padding: 3px;
  list-style: none;
  margin-right: 15px;
  border: 1px solid #eee;
}

#box img {
  width: 200px;
  height: 150px;
}

::v-deep .el-table__body tr.current-row>td {
  color: #fff;
  background: #6db3fa !important;
}
</style>