<template>
  <div class="app-container">
    <!--机构数据-->
    <!-- <el-col :span="4" :xs="24" v-if="false">
        <org-tree
          v-model="queryParams.orgId"
          @defaultKey="getDefaultKey"
          @checkChange="checkChange"
          @click="clickTreeNode"
          wholeTree
        ></org-tree>
      </el-col> -->
    <!--用户数据-->
    <div class="main-right-box">
      <!--    搜索条件    -->
      <div class="main-search-box">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
        >
              <el-form-item prop="type" label="签署类型">
                <el-select
                  prop="type"
                  label="签署类型"
                  v-model="queryParams.type"
                  placeholder="请选择签署类型"
                  clearable
                >
                  <el-option
                    v-for="dict in dict.type.safety_book_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </el-form-item>
              <el-form-item label="所属年度" prop="year">
                <el-date-picker
                  v-model="queryParams.year"
                  :clearable="timeClearable"
                  type="year"
                  placeholder="选择时间"
                  value-format="yyyy"
                >
                </el-date-picker>
              </el-form-item>
              <el-form-item label="签署机构" prop="orgId" class="formTreeItem">
                <tree-select
                  v-model="queryParams.orgId"
                  :options="deptOptions"
                  :show-count="true"
                  :normalizer="tenantIdnormalizer"
                  :props="{ checkStrictly: true, label: 'name' }"
                  placeholder="请选择签署机构"
                  clearValueText="清除"
                  :noChildrenText="''"
                  noOptionsText="没有数据"
                  noResultsText="没有搜索结果"
                />
              </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"
              >搜索</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-refresh"
              size="mini"
              @click="resetQuery"
              >重置</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd(null)"
              v-hasPermi="['core:book:add']"
              >新增
            </el-button>
          </el-col>

          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row>
      </div>
      <!-- 表格数据 -->
      <el-table
        border
        height="644"
        size="small"
        v-loading="loading"
        :data="dataList"
      >
        <el-table-column label="序号" type="index" align="center" width="60" />
        <el-table-column
          label="签署机构"
          align="left"
          prop="orgName"
        />
        <el-table-column label="签署类型" align="left"  prop="type">
          <template slot-scope="r"
            >{{ getLabel(dict.type.safety_book_type, `${r.row.type}`) }}
          </template>
        </el-table-column>
        <el-table-column
          label="所属年度"
          align="center"
          width="200"
          prop="year"
        />
        <el-table-column
          v-if="false"
          label="签署人"
          align="center"
          prop="userName"
        >
        </el-table-column>
        <el-table-column label="签署时间" align="center" prop="createTime">
          <template slot-scope="r">
            {{
              r.row.createTime
                ? dayjs(r.row.createTime).format("YYYY年MM月DD日")
                : ""
            }}
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          width="250"
          align="left"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleInfo(scope.row.id)"
              v-hasPermi="['core:book:query']"
              >详情
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit-outline"
              @click="handleAdd(scope.row.id)"
              v-hasPermi="['core:book:add']"
              >编辑
            </el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['core:book:remove']"
              >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <dialog-edit ref="editDialog" @success="getList()"></dialog-edit>
    <DialogDes ref="DialogDes"></DialogDes>
  </div>
</template>

<script>
import {
  listSafetyBook,
  getSafetyBook,
  editOrAdd,
  delSafetyBook,
} from "@/api/safetyBook/index";
import { getLabel } from "@/views/commonOption";
import DialogEdit from "./dialog.edit";
import DialogDes from "./dialog.des";
import { deptTreeSelect } from "@/api/system/public";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import OrgTree from "@/components/orgTree/index.vue";
import kOrgTree from "@/components/k-orgTree/index.vue";
import KFileUpload from "@/components/K-FileUpload/index.vue";
import tableList from "@/mixins/tableList";
import orgTree from "@/components/orgTree";
import dayjs from "dayjs";
export default {
  dicts: ["safety_book_type"],
  name: "safetyBook",
  components: {
    OrgTree,
    kOrgTree,
    KFileUpload,
    DialogEdit,
    orgTree,
    DialogDes,
  },
  mixins: [tableList],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个停用
      single: true,
      // 非多个停用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全责任书表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 机构树选项
      deptOptions: [],
      // 机构名称
      deptName: undefined,
      //是否关联下级
      checked: false,
      timeClearable: true,
      defaultProps: {
        children: "children",
        label: "name",
      },
      formFileListDefualtValue: [],
      // 查询参数
      queryParams: {
        checkSub: true,
        pageNum: 1,
        pageSize: 10,
        orgId: null,
        year: null,
        searchOrgId: null,
        type: null,
      },
      // 表单参数
      form: {},

      //默认选中节点
      defaultKeys: [],
    };
  },
  watch: {
    // 根据名称筛选机构树
    deptName(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getDeptTree();
    this.getConfigKey("sys.user.initPassword").then((response) => {
      this.initPassword = response.msg;
    });
    this.getList();
  },
  methods: {
    dayjs,
    resetQuery() {
      this.resetForm("queryForm");
      this.getList();
    },
    getLabel(options, value) {
      return getLabel(options, value);
    },
    //新增
    handleAdd(id) {
      this.$refs.editDialog.show(id);
    },
    /** 查询安全责任书列表 */
      getList() {
      this.loading = true;
       listSafetyBook(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch(()=>{
        this.loading = false;
      });
    },
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },

    // 节点单击事件
    clickTreeNode(data) {
      this.queryParams.orgId = data.id;
      this.handleQuery();
    },
    /** 下穿状态改变*/
    changeCheckBox() {
      this.getList();
    },

    getDefaultKey(key) {
      this.queryParams.orgId = key;
      this.getList();
    },
    //单选框状态改变
    checkChange(state) {
      this.queryParams.checkSub = state;
      this.handleQuery();
    },

    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.name.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.orgId = data.id;
      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除？")
        .then(function () {
          return delSafetyBook(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 详情按钮操作 */
    handleInfo(id) {
      this.$refs.DialogDes.show(id);
    },
    /** treeSelect组件自定义数据*/
    tenantIdnormalizer(node, instanceId) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.shortName,
        children: node.children,
      };
    },
  },
};
</script>
<style lang="scss" scoped>
.ellipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
::v-deep.formTreeItem {
  .el-form-item__content {
    width: 264px;
  }
}
</style>
