<template>
  <avue-crud
    :option="option"
    :table-loading="loading"
    :data="data"
    v-model="form"
    v-model:page="page"
    v-model:search="query"
    ref="crud"
    @row-del="rowDel"
    @row-save="rowSave"
    @row-update="rowUpdate"
    @search-change="searchChange"
    @search-reset="searchReset"
    @selection-change="selectionChange"
    @current-change="currentChange"
    @size-change="sizeChange"
    @refresh-change="refreshChange"
    @on-load="onLoad"
  >
    <template #menu-left> </template>
    <template #menu-right> </template>
    <template #alarm="scope">
      <el-tag v-if="scope.row.alarm" type="danger">未处理</el-tag>
      <el-tag v-else type="success">已处理</el-tag>
    </template>
    <template #endTime="scope">
      <span>{{ timeFun(scope.row) }}</span>
    </template>
  </avue-crud>
</template>
<script>
import { getList, add , remove ,update} from '@/api/safetyManagement/acidMistToweraalarmRecord.js'

export default {
  data() {
    return {
      selectionList: [],
      loading: false,
      data: [],
      query: {
        dealStatus:1
      },
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: true,
        selection: true,
        viewBtn: false,
        delBtn: false,
        addBtn: false,
        editBtnText: "修改",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 120,
        menuWidth: 120,
        dialogWidth: 1040,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        index: false,
        showOverflowTooltip: true,
        menu: false,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "left",
        gridBtn: false,
        searchMenuPosition: "right",
        align: "center",
        column: [
          {
            label: "状态",
            prop: "dealStatus",
            search: true,
            searchLabelWidth: 50,
            sortable: true,
            filter: true,
            type: "select",
            value:1,
            dicData: [  
              {
                label: "已处理",
                value: 0,
              },
              {
                label: "未处理",
                value: 1,
              }
            ],
          },
          {
            label: "编码",
            prop: "btCode",
            sortable: true,
            filter: true,
            search: true,
            searchLabelWidth: 50,
          },
          {
            label: "日期",
            prop: "queryBtDate",
            search: true,
            searchLabelWidth: 50,
            sortable: true,
            hide: true,
            filter: true,
            type: "date",
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            searchRange: true,
            startPlaceholder: "开始时间",
            endPlaceholder: "结束时间"
          },
          {
            label: "日期",
            prop: "btDate",
            sortable: true,
            filter: true,
            type: "date",
            format: 'YYYY-MM-DD HH:mm:ss',
            valueFormat: 'YYYY-MM-DD HH:mm:ss',
            // searchRange: true,
          },
          {
            label: "错误点",
            prop: "errorPoint",
            search: true,
            searchLabelWidth: 65,
            sortable: true,
            filter: true,
          },
          {
            label: "消息文本",
            prop: "messText",
            search: true,
            searchLabelWidth: 80,
            sortable: true,
            filter: true,
          },
          {
            label: "持续时间",
            prop: "consLong",
            search: false,
            sortable: true,
            filter: true,
          },
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
    };
  },
  methods: {
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    timeFun(row) {
      let endTime;
      if (row.endTime !== null) {
        endTime = new Date(row.endTime).getTime();
      } else {
        endTime = new Date().getTime();
      }
      let str = "";
      const time = endTime - new Date(row.startTime).getTime();

      var date = new Date(time);
      // 获取小时
      var hh = Math.floor(time / (60 * 60 * 1000));
      // 获取分
      var mm = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
      // /获取秒
      var ss = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

      if (time > 24 * 60 * 60 * 1000) {
        str = Math.floor(time / 86400000) + "天" + hh + "小时" + mm + "分" + ss + "秒";
      } else if (time > 60 * 60 * 1000) {
        str = hh + "小时" + mm + "分" + ss + "秒";
      } else if (time > 60 * 1000) {
        str = mm + "分" + ss + "秒";
      } else {
        str = ss + "秒";
      }
      return str;
    },
    //删除
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning('请选择至少一条数据');
        return;
      }
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          this.$refs.crud.toggleSelection();
        });
    },
    //新增
    rowSave(row, done, loading) {
      add(row).then(res => {
        this.loading(this.page)
        this.$message({
          type: 'success',
          messge: '操作成功!'
        })
        done();
      },
        error => {
          window.console.log(error)
          loading()
        }
      )
    },
    //更新
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: 'success',
            message: '操作成功!',
          });
          done();
        },
        error => {
          window.console.log(error);
          loading();
        }
      );
    },
    // 重置
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    // 搜索
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.table.clearSelection();
    },
    onLoad(page, params = {}) {
      const requestParams = Object.assign({}, params, this.query);
      // 后端要字符串类型
      if (requestParams.queryBtDate && Array.isArray(requestParams.queryBtDate)) {

        requestParams.queryBtDate = requestParams.queryBtDate.join(',');
      }
      getList(page.currentPage, page.pageSize, requestParams)
        .then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          console.log("data.records", data.records)
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    // 刷新
    refreshChange() {
      this.onLoad(this.page, this.query);
    }
  },
};
</script>
<style lang="scss" scoped></style>
