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

      <el-col :span="20" :xs="24">
        <!--    搜索条件    -->
        <el-form
          :model="queryParams"
          ref="search"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="120px"
        >
          <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="布撤防时间范围">
            <el-date-picker
              v-model="queryParams.dateRange"
              type="datetimerange"
              format="yyyy-MM-dd HH:mm"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList"
              v-hasPermi="['device:protection']"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</el-button
            >
          </el-form-item>
        </el-form>

        <!--    按纽    -->
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd()"
              v-hasPermi="['device:protection:add']"
              >新增</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            > 
          </el-col>-->

          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </el-row>

        <el-table
          v-loading="loading"
          :data="pageData"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="index"
            label="序号"
            v-if="columns[0].visible"
          ></el-table-column>
          <el-table-column
            prop="orgName"
            label="所属机构"
            v-if="columns[1].visible"
          ></el-table-column>
          <el-table-column
            prop="name"
            label="防区名称"
            v-if="columns[2].visible"
          >
          </el-table-column>
          <el-table-column
            prop="allHour"
            label="24小时防区"
            v-if="columns[3].visible"
            ><template slot-scope="r"
              >{{ r.row.allHour ? "是" : "否" }}
            </template>
          </el-table-column>
          <el-table-column
            prop="status"
            label="防区状态"
            v-if="columns[4].visible"
          >
            <template slot-scope="r"
              >{{ getLabel(dict.type.protection_status, r.row.status) }}
            </template>
          </el-table-column>
          <el-table-column
            prop="statusUpdateTime"
            label="布撤防时间"
            v-if="columns[5].visible"
          >
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="r">
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="updateStatus(r.row.id, 1)"
                v-hasPermi="['device:protection:maintain']"
                >布防</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="updateStatus(r.row.id, 0)"
                v-hasPermi="['device:protection:maintain']"
                >撤防</el-button
              > -->
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="onEdit(r.row.id)"
                v-hasPermi="['device:protection:edit']"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="onHistory(r.row)"
                v-hasPermi="['device:protection']"
                >布撤防历史</el-button
              >
              <el-popover
                placement="top-start"
                @confirm="onDel(r.row.id)"
                content="点击确定删除防区"
              >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  v-hasPermi="['device:protection:remove']"
                  slot="reference"
                  >删除</el-button
                >
              </el-popover>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </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 { mapState, mapMutations } from "vuex";
import DialogEdit from "./dialog.edit";
import DialogHistory from "./dialog.history";
import * as api from "@/api/resumption/protection";
import { getLabel } from "./../../commonOption";

export default {
  name: "protection",
  dicts: ["protection_status"],
  components: {
    DialogEdit,
    OrgTree,
    DialogHistory,
  },
  data() {
    const { params, query } = this.$route;
    return {
      isShow: false,
      loading: false,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      queryParams: {
        orgId: null,
        checkSub: 0,
        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: `24小时防区`, visible: true },
        { key: 4, label: `防区状态`, visible: true },
        { key: 5, label: `布撤防时间`, visible: true },
      ],
    };
  },
  props: {},
  watch: {},
  computed: {
    ...mapState([]),
  },
  methods: {
    ...mapMutations([]),
    getLabel,
    getList() {
      this.loading = true;
      api
        .list(this.queryParams)
        .then((response) => {
          this.pageData = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });
    },
    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    handleAdd(id, other = {}) {
      this.$refs.editDialog.show(id, other);
    },
    onEdit(id, other = {}) {
      this.$refs.editDialog.show(id, other);
    },
    onHistory(row) {
      this.$refs.historyDialog.show(row);
    },
    async onDel(id) {
      await api.del(id);
      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.orgId = undefined;
      // this.$refs.tree.setCurrentKey(null);
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.getList();
    },
    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.getList();
    },
  },
  mounted() {},
};
</script>

<style lang="scss" scoped>
.brand {
}
</style>
