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

      <el-col :span="20" :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"
              label-width="120px"
            >
              <el-form-item prop="name" label="检查库名称" label-width="100">
                <el-input
                  v-model="queryParams.name"
                  :maxlength="50"
                  placeholder="请输入检查库名称"
                  clearable
                />
              </el-form-item>
              <el-form-item prop="orgType" label="受检机构类型">
                <el-select
                  prop="orgType"
                  label="受检机构类型"
                  v-model="queryParams.orgType"
                  placeholder="请选择受检机构类型"
                  clearable
                >
                  <el-option
                    v-for="item in dict.type.sys_org_type"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>

            <!--    按纽    -->
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  size="mini"
                  @click="refresh"
                  v-hasPermi="['safetycheck:rule:query']"
                  >搜索</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="['safetycheck:rule:add']"
                  >新增检查内容库</el-button
                >
              </el-col>
              <right-toolbar
                :showSearch.sync="showSearch"
                @queryTable="getList"
                :columns="columns"
              ></right-toolbar>
            </el-row>
          </div>

          <el-table
            size="small"
            v-loading="loading"
            :data="pageData"
            @selection-change="handleSelectionChange"
            height="640"
          >
            <el-table-column
              header-align="center"
              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
              header-align="center"
              prop="name"
              label="检查库名称"
              v-if="columns[1].visible"
            ></el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              prop="orgType"
              label="受检机构类型"
              v-if="columns[2].visible"
            >
              <template slot-scope="r"
                >{{ getLabel(dict.type.sys_org_type, r.row.orgType) }}
              </template>
            </el-table-column>
            <el-table-column
              header-align="center"
              prop="orgName"
              label="发布机构"
              v-if="columns[3].visible"
            ></el-table-column>
            <el-table-column
              header-align="center"
              align="center"
              prop="status"
              label="状态"
              v-if="columns[4].visible"
            >
              <template slot-scope="r">
                <span>{{ getLabel(dict.type.plan_status, r.row.status) }}</span>
              </template>
            </el-table-column>
            <el-table-column
              header-align="center"
              prop="remark"
              label="备注"
              v-if="columns[5].visible"
            ></el-table-column>
            <el-table-column header-align="center" label="操作" width="250">
              <template slot-scope="r">
                <div  v-if="orgId == r.row.orgId"><el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit-outline"
                  @click="onEdit(r.row.id)"
                  v-hasPermi="['safetycheck:rule:edit']"
                  >编辑</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit-outline"
                  @click="onManageContent(r.row.id)"
                  v-if="r.row.status==0"
                  v-hasPermi="['safetycheck:rule:contentmanage']"
                  >管理内容库</el-button
                >

                <el-button
                  type="text"
                  size="mini"
                  slot="reference"
                  icon="el-icon-delete"
                  @click="onDel(r.row.id)"
                  v-if="orgId == r.row.orgId"
                  v-hasPermi="['safetycheck:rule:remove']"
                  >删除</el-button
                ></div>
                <div  v-else>
                  <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-view"
                  @click="onManageContent(r.row.id)"
                  v-hasPermi="['safetycheck:rule:contentmanage']"
                  >查看内容库</el-button
                >
                </div>
              </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"
      :statusOptions="dict.type.plan_status"
    ></dialog-edit>
  </div>
</template>

<script>
import OrgTree from "@/components/orgTree";
import { mapGetters } from "vuex";
import DialogEdit from "./dialog.edit";
import * as api from "@/api/safetycheck/rule";
import { getLabel } from "./../../commonOption";
import { wholeTreeByType } from "@/api/system/org.js";
export default {
  name: "safetycheckruletype",
  dicts: ["sys_org_type", "plan_status"],
  components: {
    DialogEdit,
    OrgTree,
  },
  data() {
    const { params, query } = this.$route;
    return {
      isShow: false,
      loading: false,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      total: 0,
      queryParams: {
        orgId: null,
        name: null,
        type: null,
        orgType: null,
        checkSub: true,
        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"]),
  },
  methods: {
    getLabel,
    orgTree() {
      return wholeTreeByType(3);
    },
    refresh() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      console.info(this.dict.type);
      this.pageData=[]
      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);
    },
    onManageContent(id) {
      let path = `/core/safetycheck/rule/content/${id}`;
      this.$router.push(path);
    },
    onDel(id) {
      this.$modal
        .confirm("确定删除检查内容库定义？")
        .then(() => {
          return api.remove(id);
        })
        .then(() => {
          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();
    },
    //apimark//
  },
  mounted() {},
};
</script>

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