<template>
  <div>
    <el-card class="el-card">
      <el-form :inline="true" :model="filterQuery" class="query-form" label-width="100px" size="mini">
        <el-form-item class="query-form-item" label="角色名称:" prop="name">
          <el-input
            v-model="filterQuery.name"
            clearable
            placeholder="角色名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色状态:" prop="status">
          <el-select
            v-model="filterQuery.status"
            placeholder="请选择状态"
            style="width: 90%"
          >
            <el-option label="全部" value=""/>
            <el-option label="启用" :value="0"/>
            <el-option label="禁用" :value="1"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px; display: flex">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset"
            >重置
            </el-button
            >
            <el-button type="primary" icon="el-icon-search" @click="onSubmit"
            >查询
            </el-button
            >
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="handleSubForm(null, 'add')"
            >新增
            </el-button
            >
          </el-button-group>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border
                @current-change="handleSelectionChange">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="name" show-overflow-tooltip></el-table-column>

        <el-table-column label="编码" prop="code" show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="状态" prop="status" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.status)">{{ status[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" width="120" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="printOrder(scope.row.id)"
            >授权
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="reviewOrder(scope.row,'edit')"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="rollback(scope.row)"
            >删除
            </el-button
            >

          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>


    <el-dialog
      :title="formMap[formName]"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :visible.sync="subFormVisible"
      :before-close="hideForm"
      width="60%"
      top="5vh"
    >
      <el-form
        :model="subData"
        ref="dataForm"
        :rules="formRules"
        label-width="90px"
      >
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="12" class="el-col">
            <el-form-item label="角色名称" prop="name" class="query-form-item">
              <el-input
                v-model="subData.name"
                style="width: 80%"
                auto-complete="off"
                placeholder="请输入接口名称"
              ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12" class="el-col">
            <el-form-item label="编码" prop="code">
              <el-input
                v-model="subData.code"
                style="width: 80%"
                placeholder="请输入权限标识"
                auto-complete="off"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20" class="el-row" type="flex">

          <el-col :span="12" class="el-col">
            <el-form-item
              label="状态"
              prop="status"
              class="query-form-item"
            >
              <el-radio-group v-model="subData.status">
                <el-radio label='0'>启用</el-radio>
                <el-radio label='1'>禁用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="hideForm">取消</el-button>
        <el-button type="primary" @click.native="forInvSubmit()"
        >提交
        </el-button>
      </div>
    </el-dialog>


    <el-dialog title="授权" :visible.sync="roleMenuDialog.visible">
      <el-tree
        :data="roleMenuDialog.detail"
        show-checkbox
        ref="menu"
        node-key="id"
        empty-text="暂无数据"
        :default-checked-keys="roleMenuDialog.checkKeys"
        :props="roleMenuDialog.defaultProps"
      ></el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button @click="roleMenuDialog.visible = false">取 消</el-button>
        <el-button type="primary" @click="saveRoleMenu">保存</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import {getRolelist, insertRole, updateRole, deleteRole, treeselect, saveRoleMenu}
  from "@/api/system/srole"


const formJson = {
  site_id: "",
  site_name: "",
  describe: "",
  ads: [],
};

export default {
  name: "IoSupSearchOrder",
  data() {
    return {
      rId: "",
      visible: false,
      check: {},
      subData: {},
      formName: null,
      subFormVisible: false,
      filterQuery: {
        name: "",
        page: 1,
        limit: 10,
        startTime: null,
        endTime: null,
        status: "",

      },
      status: {
        0: "正常",
        1: "禁用",
      },
      roleMenuDialog: {
        visible: false,
        detail: {},
        roleId: 0,
        checkKeys: [],
        defaultProps: {
          children: "children",
          label: "name"
        },
      },
      invList: [],
      deptList: [],
      list: [],
      total: 0,
      loading: false,
      index: null,
      enableDept: false,

      dialogTableVisible: false,
      formLoading: false,
      dialogVisible: false,
      formData: formJson,
      deleteLoading: false,
      busTypes: [],
      currentRow: {},
      actDateRange: [],
      auditDateRange: [],
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      formMap: {
        add: "新增",
        edit: "编辑",
      },
    };
  },
  components: {},
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        name: "",
        page: 1,
        limit: 10,
        startTime: null,
        endTime: null,
        status: "",
      };
      this.actDateRange = [];
      this.auditDateRange = [];
      this.getList();
    },


    onSubmit() {
      this.loading = true;
      this.filterQuery.page = 1;
      this.getList();
    },
    //获取订单列表
    getList() {
      this.loading = true;
      getRolelist(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code === 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
        })
        .catch((error) => {
          this.$message.error(error.message)
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },

    hideForm() {
      this.subFormVisible = false;
      this.getList();
    },
    reviewOrder(data, formName) {
      this.subFormVisible = true;
      this.formName = formName;
      this.subData = data;
    },

    handleSubForm(data, formName) {
      this.formName = formName;
      this.subData = {};
      this.subFormVisible = true;
    },

    forInvSubmit() {
      if (this.formName === "add") {
        insertRole(this.subData)
          .then((res) => {
            if (res.code == 20000) {
              this.$message.success("添加成功!");
              this.subFormVisible = false;
              this.getList();
            }
          })
      } else {
        updateRole(this.subData)
          .then((res) => {
            if (res.code == 20000) {
              this.$message.success("修改成功!");
              this.subFormVisible = false;
              this.getList();
            }
          })
      }
    },

    rollback(row) {
      this.$confirm("此操作将删除角色,是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        this.subData = row;
        deleteRole(this.subData)
          .then((res) => {
            if (res.code == 20000) {
              this.$message.success("删除成功!");
              this.getList();
            }

          })


      })


    },

    printOrder(id) {
      this.rId = id;
      let param = {
        roleId: id
      }
      treeselect(param).then((res) => {
        this.roleMenuDialog.detail = res.data.data.menuTrees;
        this.roleMenuDialog.checkKeys = res.data.data.roleMenuIds;
        this.roleMenuDialog.visible = true;
      })

    },

    saveRoleMenu() {
      let param = {
        roleId: this.rId,
        menuIds: this.getMenuAllCheckedKeys()
      }
      saveRoleMenu(param).then((res) => {
        if (res.code == 20000) {
          this.$message.success("修改成功！");
          this.roleMenuDialog.visible = false;
          this.getList();
        }


      })
    },
    getMenuAllCheckedKeys() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.menu.getHalfCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.menu.getCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },


    statusFilterType(status) {
      const statusMap = {
        0: "success",
        1: "warning",
      };
      return statusMap[status];
    }
  },
  filters: {},
  mounted() {

  },
  created() {
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>
