<template>
  <div class="m-container">
    <h2>
      <div class="squrt"></div>
      操作手册管理
    </h2>
    <div class="m-core">
      <save-form
        :inline="true"
        :model="searchForm"
        @submit="search"
        @reset="onReSet"
      >
        <el-form-item prop="search">
          <el-input
            v-model.trim="searchForm.search"
            clearable
            @keyup.enter.native="search"
            placeholder="请输入手册名称"
          ></el-input>
        </el-form-item>
        <!-- search -->
        <el-form-item prop="menuId">
          <el-cascader
            ref="myCascader"
            class="myCascader"
            popper-class="myCascader"
            @change="changeCategor"
            :show-all-levels="false"
            clearable
            v-model="searchForm.menuId"
            style="width: 100%"
            :options="silderList"
            :props="optionProps"
            placeholder="请选择菜单"
          ></el-cascader>
        </el-form-item>
        <el-form-item>
          <el-button native-type="submit" type="primary">搜索</el-button>
          <el-button native-type="reset" type="danger">重置</el-button>
        </el-form-item>
      </save-form>
      <auth auths="sysManual/add">
        <el-button type="primary" style="margin-bottom: 20px" @click="addManual"
          >添加</el-button
        >
      </auth>

      <div class="tableWrap">
        <el-table
          style="width: 100%"
          :header-cell-style="{ background: '#EDEEF0', color: '#444' }"
          :data="tableList"
        >
          <el-table-column
            align="center"
            prop="menuName"
            label="菜单名"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="name"
            label="手册名称"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="updateTime"
            label="修改时间"
          ></el-table-column>
          <el-table-column
            align="center"
            prop="operatorName"
            label="修改人"
          ></el-table-column>
          <el-table-column align="center" prop="studentId" label="操作">
            <template slot-scope="scope">
              <div>
                <auth auths="sysManual/update">
                  <el-button type="text" @click="resetManual(scope.row)"
                    >修改</el-button
                  >
                </auth>
                <auth auths="sysManual/remove">
                  <el-button type="text" @click="deteleManual(scope.row)"
                    >删除</el-button
                  >
                </auth>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <pagination
          sync
          :total.sync="rules.total"
          :page.sync="rules.page"
          :limit.sync="rules.limit"
          :page-sizes="rules.page_size"
          @pagination="getList"
        />
      </div>
    </div>
    <el-dialog
      :title="operationStatus == 'create' ? '新增操作手册' : '修改操作手册'"
      width="800px"
      :visible.sync="operationVisible"
      v-if="operationVisible"
    >
      <setForm
        :silderList="silderList"
        :form="form"
        ref="setForm"
        @getList="getList"
        @close="operationVisible = false"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="operationVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitOperation">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
import { getToken } from "@/utils/auth";
import pagination from "@/components/Pagination/index";
import load from "@/utils/loading";
import { getSilder } from "@/api/silder";
import {
  getSysManualList,
  addSysManual,
  resetSysManual,
  deteltSysManual,
} from "./api";
import setForm from "./modals/manualSet";
export default {
  components: { pagination, setForm },
  data() {
    return {
      searchForm: {
        menuId: [],
        search:''
      },
      operationStatus: "create",
      form: {
        funRule: "",
        menuId: "",
        name: "",
        opFlow: "",
      },
      tableList: [],
      organList: [],
      silderList: [],
      rules: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50], // 选择限制显示条数
      },
      optionProps: {
        value: "id",
        label: "name",
        children: "children",
        checkStrictly: true,
        expandTrigger: "hover",
      },
      operationVisible: false,
    };
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    // 获取分部

    this.init();
  },
  methods: {
    init() {
      getSilder({ hid: 0 }).then((res) => {
        if (res.code == 200) {
          this.silderList = this.setTableData(res.data);
        }
      });
      this.getList();
    },
    async getList() {
      try {
        const res = await getSysManualList({
          menuId: this.searchForm.menuId[this.searchForm.menuId.length - 1],
          search: this.searchForm.search,
          page: this.rules.page,
          rows: this.rules.limit,
        });
        this.tableList = res.data.rows;
        this.rules.total = res.data.total;
      } catch (e) {
        console.log(e);
      }
    },
    search() {
      this.rules.page = 1;
      this.getList();
    },
    onReSet() {
      this.$nextTick(() => {
        this.search();
      });
    },
    setTableData(result) {
      let list = [];
      for (let i in result) {
        if (result[i].type == 1) {
          continue;
        }
        let tempList = {};
        tempList = {
          id: result[i].id,
          name: result[i].name,
          type: result[i].type,
          path: result[i].path,
          permission: result[i].permission,
          icon: result[i].icon,
          parentId: result[i].parentId,
          component: result[i].component,
          sort: result[i].sort,
          hid: result[i].hid,
          icon: result[i].icon,
          type: result[i].type,
          keepAlive: result[i].keepAlive,
          parentPermission: result[i].parentPermission,
          memo: result[i].memo,
        };
        let flag = false;
        if (result[i].sysMenus && result[i].sysMenus.length > 0) {
          for (let j in result[i].sysMenus) {
            if (result[i].sysMenus[j].type == 0) {
              flag = true;
              break;
            }
          }
          if (flag) {
            tempList.children = this.setTableData(result[i].sysMenus);
          }
        }
        list.push(tempList);
      }
      return list;
    },
    changeCategor(e) {
      this.$refs.myCascader.dropDownVisible = false;
    },
    resetManual(row) {
      this.form = { ...row };
      this.operationStatus = "reset";
      this.operationVisible = true;
    },
    deteleManual(row) {
      this.$confirm(`您确定删除${row.name}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        try {
          const res = await deteltSysManual({ id: row.id });
          this.$message.success("删除成功");
          this.getList();
        } catch (e) {
          console.log(e);
        }
      });
    },
    addManual() {
      this.operationStatus = "create";
      this.form = {
        funRule: "",
        menuId: "",
        name: "",
        opFlow: "",
      };
      this.operationVisible = true;
    },
    async submitOperation() {
      try {
        let { menuId, ...result } = this.form;
        if (this.operationStatus == "create") {
          const res = await addSysManual({
            ...result,
            menuId: menuId[menuId.length - 1],
          });
          this.$message.success("添加成功");
          this.operationVisible = false;
          this.getList();
        } else {
          const res = await resetSysManual({
            ...result,
            menuId: Array.isArray(menuId) ? menuId[menuId.length - 1] : menuId,
          });
          this.$message.success("修改成功");
          this.operationVisible = false;
          this.getList();
        }
      } catch (e) {
        console.log(e);
      }
      // console.log(this.form);
    },
  },
};
</script>
<style lang='scss'>
.myCascader {
  .el-radio {
    width: 100%;
    height: 100%;
    z-index: 10;
    position: absolute;
    top: 10px;
    right: 10px;
  }
  .el-radio__input {
    visibility: hidden;
  }
  .el-cascader-node__postfix {
    top: 10px;
  }
}
</style>
