<template>
  <div class="app-container">
    <el-row :gutter="10">
      <!--机构数据-->
      <!-- <el-col :span="4" :xs="24">
        <org-tree
          v-model="queryParams.orgId"
          @defaultOrg="getDefaultOrg"
          @checkChange="checkChange"
          @click="clickTreeNode"
        ></org-tree>
      </el-col> -->

      <el-col :span="24" :xs="24">
        <div class="main-right-box">
          <!--    搜索条件    -->
          <div class="main-search-box">
            <el-form
              :model="queryParams"
              ref="search"
              size="small"
              :inline="true"
              v-show="showSearch"
            >
              <el-form-item label="所属机构">
                <org-tree
                  v-model="queryParams.orgId"
                  @defaultOrg="getDefaultOrg"
                  @checkChange="checkChange"
                  @click="clickTreeNode"
                  ref="orgTree"
                ></org-tree>
              </el-form-item>
              <el-form-item prop="key" label="报警控制器名称">
                <el-input
                  v-model="queryParams.key"
                  :maxlength="50"
                  placeholder="请输入报警控制器名称"
                  clearable
                />
              </el-form-item>
              <el-form-item prop="status" label="报警控制器状态">
                <el-select
                  label="报警控制器状态"
                  v-model="queryParams.status"
                  placeholder="请选择报警控制器状态"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.protection_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>

              <el-form-item prop="dateRange" label="布撤防登记时间">
                <DataRangePicker
                  v-model="queryParams.dateRange"
                  key="daterange"
                  type="daterange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </DataRangePicker>
              </el-form-item>
            </el-form>

            <!--    按纽    -->
            <el-row :gutter="10">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="getList"
                  v-hasPermi="['device:protection']"
                  >搜索</el-button
                >
                <el-button
                  type="primary"
                  icon="el-icon-refresh"
                  size="mini"
                  @click="resetQuery"
                  >重置</el-button
                >
                <!-- <el-button
                  type="primary"
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleAdd()"
                  v-hasPermi="['device:protection:add']"
                  >新增</el-button
                > -->
                <el-button
                  type="primary"
                  icon="el-icon-download"
                  size="mini"
                  @click="handleExport()"
                  v-hasPermi="['device:protection']"
                  >导出数据</el-button
                >
              </el-col>
              <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
              ></right-toolbar>
            </el-row>
          </div>
          <el-table
            size="small"
            border
            v-loading="loading"
            :data="pageData"
            @selection-change="handleSelectionChange"
            height="594"
          >
            <el-table-column
              align="center"
              type="index"
              label="序号"
              width="60"
              v-if="columns[0].visible"
            >
              <template slot-scope="scope">
                {{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="orgName"
              label="所属机构"
              v-if="columns[1].visible"
            ></el-table-column>
            <el-table-column
              align="center"
              prop="name"
              label="报警控制器名称"
              show-overflow-tooltip
              v-if="columns[2].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="status"
              label="报警控制器状态"
              width="150"
              v-if="columns[3].visible"
            >
              <template slot-scope="r"
                >{{
                  getLabel(dict.type.protection_status, r.row.status, "未知")
                }}
              </template>
            </el-table-column>
            <el-table-column
              align="center"
              prop="statusUpdatorName"
              label="登记人/数据来源"
              show-overflow-tooltip
              width="150"
              v-if="columns[4].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="statusUpdateTime"
              label="最近登记时间"
              width="150"
              v-if="columns[5].visible"
            >
            </el-table-column>
            <el-table-column
              align="center"
              prop="statusUpdateTime"
              label="布撤防登记时间"
              width="150"
              v-if="columns[5].visible"
            >
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="r">
                <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="updateStatus(r.row.id, 1)"
                v-hasPermi="['device:protectionstatus:maintain']"
                >布防</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit-outline"
                @click="updateStatus(r.row.id, 0)"
                v-hasPermi="['device:protectionstatus:maintain']"
                >撤防</el-button
              > -->
                <!-- <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit-outline"
                  @click="onEdit(r.row.id)"
                  v-hasPermi="['device:protection:edit']"
                  >编辑</el-button
                > -->
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-document-copy"
                  @click="onHistory(r.row)"
                  v-hasPermi="['device:protection']"
                  >布撤防历史</el-button
                >
                <!-- <el-popconfirm
                  @confirm="onDel(r.row.id)"
                  title="点击确定删除报警控制器"
                >
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    v-hasPermi="['device:protection:remove']"
                    slot="reference"
                    >删除</el-button
                  >
                </el-popconfirm> -->
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </div>
      </el-col>
    </el-row>
    <dialog-edit
      ref="editDialog"
      @success="getList()"
      :orgTypeOptions="dict.type.sys_org_type"
      :ruleTypeOptions="dict.type.rule_type"
    ></dialog-edit>
    <dialog-history
      ref="historyDialog"
      :statusDict="dict.type.protection_status"
    ></dialog-history>
  </div>
</template>

<script>
// import OrgTree from "@/components/orgTree";
import OrgTree from "@/components/orgTree/orgQuerySelector.vue";
import DialogEdit from "./dialog.edit";
import DialogHistory from "./dialog.history";
import * as api from "@/api/resumption/protection";
import { getLabel } from "@/views/commonOption.js";
import { mapGetters } from "vuex";
import dayjs from "dayjs";
import DataRangePicker from "@/components/dateTime/daterange.picker.vue";
export default {
  name: "protection",
  dicts: ["protection_status"],
  components: {
    DialogEdit,
    OrgTree,
    DialogHistory,
    DataRangePicker,
  },
  data() {
    const { params, query } = this.$route;
    return {
      isShow: false,
      loading: false,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      selectedOrgName: null,
      queryParams: {
        orgId: null,
        checkSub: true,
        key: null,
        status: null,
        dateRange: null,
        pageNum: 1,
        pageSize: 10,
        ...query,
      },
      pageData: [],
      // 列信息
      columns: [
        { key: 0, label: `序号`, visible: true },
        { key: 1, label: `所属机构`, visible: true },
        { key: 2, label: `报警控制器名称`, visible: true },
        { key: 3, label: `报警控制器状态`, visible: true },
        { key: 4, label: `登记人`, visible: true },
        { key: 5, label: `最近登记时间`, visible: true },
      ],
    };
  },
  props: {},
  watch: {},
  computed: {
    ...mapGetters(["orgId", "orgName"]),
  },
  methods: {
    getLabel,
    getList() {
      this.loading = true;
      this.pageData = [];
      api
        .list(this.queryParams)
        .then((response) => {
          this.pageData = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },

    handleAdd(id, other = {}) {
      this.$refs.editDialog.show(id, other);
    },
    handleExport() {
      if (this.total == 0) {
        this.$message.error({
          message: `无可用数据导出！`,
          type: "error",
          duration: 2000,
        });
        return;
      }

      this.download(
        "/core/resumption/protection/export",
        {
          ...this.queryParams,
        },
        `${this.selectedOrgName}-布撤防情况跟踪-${dayjs(new Date()).format(
          "YYYYMMDD"
        )}.xlsx`
      );
    },
    onEdit(id, other = {}) {
      this.$refs.editDialog.show(id, other);
    },
    onHistory(row) {
      this.$refs.historyDialog.show(row);
    },
    async onDel(id) {
      let b = (await api.del(id)).data;
      if (b) {
        this.$message.info("删除成功");
      }

      this.getList();
    },
    updateStatus(id, status) {
      api.updateStatus(id, status).then((r) => {
        this.$message.info("更新状态成功");
        this.getList();
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.userId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("search");
      this.queryParams.dateRange = null;
      this.queryParams.orgId = this.orgId;
      this.selectedOrgName = this.orgName;
      this.queryParams.checkSub = true;
      this.$refs["orgTree"].setCheckSub(this.queryParams.checkSub);
      // this.queryParams.orgId = undefined;
      // this.$refs.tree.setCurrentKey(null);
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.getList();
    },
    getDefaultOrg(node) {
      this.queryParams.orgId = node.id;
      this.selectedOrgName = node.shortName;
      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.selectedOrgName = data.shortName;
      this.getList();
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
/* .search_key{
  ::v-deep .el-form-item__label{
    width:60px !important;
  }
} */
</style>
