<template>
  <div class="doctorBody">
    <!-- 顶部筛选 -->
    <div class="screening">
      <div class="screening-title flex-vertical-center-l">
        <img src="~@/assets/filters.png" alt />
      </div>
      <div class="screening-form flex-vertical-center-l flex-wrap">
        <div class="screening-item flex-vertical-center-l">
          <span>角色名称：</span>
          <div class="input">
            <el-input size="mini" v-model="form.name" placeholder="请输入"></el-input>
          </div>
        </div>
        <el-button type="primary" size="mini" @click="search()">搜索</el-button>
        <el-button type="warning" size="mini" @click="clearFilter()">清空</el-button>
        <el-button type="primary" size="mini" @click="addData()" v-if="showAdd">新增</el-button>
      </div>
    </div>

    <!-- 表格 -->
    <div class="today-data">
      <!-- <div class="add flex-center" @click="addData()" v-if="showAdd">新增</div> -->
      <div class="today-table">
        <el-table :data="tableData" stripe style="width: 100%" border height="100%">
          <el-table-column prop="id" label="序号" width="50" align="center"></el-table-column>
          <el-table-column prop="rolename" label="中文名" align="center"></el-table-column>
          <el-table-column prop="englishName" label="英文名" align="center"></el-table-column>
          <el-table-column prop="remark" label="描述" align="center"></el-table-column>
          <el-table-column prop="userName" label="创建者" align="center"></el-table-column>
          <el-table-column prop="creTime" label="创建时间" align="center"></el-table-column>
          <el-table-column prop="state" label="状态" align="center">
            <template slot-scope="scope">
              <div v-if="scope.row.stateSel==1" class="flex-center enable">停用</div>
              <div v-if="scope.row.stateSel==0" class="flex-center disable">启用</div>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="240" align="center">
            <template slot-scope="scope">
              <div class="flex-center operation">
                <div class="flex-center" @click="editData(scope)" v-if="showEdit">编辑</div>
                <div class="flex-center bg-yellow" @click="deleteData(scope)" v-if="showDel">删除</div>
                <div
                  class="flex-center bg-red"
                  style="margin-left:15px;"
                  @click="toPermissions(scope)"
                  v-if="showRole"
                >权限</div>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="flex-vertical-center-r today-page">
        <el-pagination
          background
          layout=" prev, pager, next, jumper, total"
          :total="total"
          :page-size="limit"
          @current-change="sizeC($event)"
        ></el-pagination>
      </div>
    </div>

    <popup
      distanceTop="5vh"
      :showDialog="showDialog"
      @cancle="showDialog=false"
      @confim="submit()"
      title="角色管理"
    >
      <div class="flex-center" slot="body">
        <div class="form">
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span>*</span>
              <div>中文名：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.name" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span>*</span>
              <div>英文名：</div>
            </div>
            <div class="input">
              <el-input v-model="diaform.english" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l">
            <div class="name flex-vertical-center-r">
              <span></span>
              <div>角色描述：</div>
            </div>
            <div class="input">
              <el-input type="textarea" v-model="diaform.desc" placeholder="请输入"></el-input>
            </div>
          </div>
          <div class="form-item flex-plane-center-l" v-if="showState">
            <div class="name flex-vertical-center-r">
              <span></span>
              <div>状态：</div>
            </div>
            <div class="input">
              <el-radio-group v-model="diaform.radio">
                <el-radio label="0">启用</el-radio>
                <el-radio label="1">停用</el-radio>
              </el-radio-group>
            </div>
          </div>
        </div>
      </div>
    </popup>
  </div>
</template>
<script>
import {
  getRoleList,
  addRoleData,
  getRoleDetail,
  editRoleData,
  deleteRoleData,
  getRoleBR
} from "@/api/system.js";

import { getProver, getArea } from "@/api/city.js";
import popup from "@/components/Propup.vue";
import { load } from "mime";
export default {
  components: {
    popup
  },
  data() {
    return {
      showAdd: true,
      showEdit: true,
      showState: true,
      showDel: true,
      showRole: true,

      showDialog: false,
      form: {
        name: ""
      },
      diaform: {
        name: "",
        english: "",
        desc: "",
        radio: "0",
        pid: ""
      },
      tableData: [],
      type: "add",
      page: 1,
      limit: 10,
      total: 0
    };
  },
  created() {
    this.getRoleList();
    this.getRoleBR();
  },
  methods: {
    // 跳转到权限页面

    toPermissions(scope) {
      this.$router.push({
        path:
          "/index/permissions?name=" +
          scope.row.rolename +
          "&id=" +
          scope.row.pid
      });
    },
    clearFilter() {
      this.form.name = "";
      this.getRoleList();
    },
    search() {
      this.page = 1;
      this.getRoleList();
    },
    deleteData(scope) {
      this.$confirm("确定要删除当前数据吗", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteRoleData(scope.row.pid);
        })
        .catch(() => {});
    },
    sizeC(e) {
      this.page = e;
      this.getRoleList();
    },
    submit() {
      if (this.diaform.name == "") {
        this.$message({
          type: "error",
          message: "请输入中文名称",
          showClose: true
        });
        return;
      }
      if (this.diaform.english == "") {
        this.$message({
          type: "error",
          message: "请输入英文名称",
          showClose: true
        });
        return;
      }

      if (this.type == "add") {
        this.addRoleData();
      } else if (this.type == "edit") {
        this.editRoleData(this.diaform.pid);
      }
    },
    editData(scope) {
      this.type = "edit";
      this.diaform.pid = scope.row.pid;
      this.getRoleDetail(scope.row.pid);
      this.showDialog = true;
    },
    addData() {
      this.type = "add";
      this.showDialog = true;
      this.diaform = {
        name: "",
        english: "",
        desc: "",
        radio: "0",
        pid: ""
      };
    },
    // 添加角色数据
    async addRoleData() {
      let params = {
        englishName: this.diaform.english,
        remark: this.diaform.desc,
        rolename: this.diaform.name,
        stateSel: this.diaform.radio
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await addRoleData(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();
        this.$message({
          message: "提交成功",
          showClose: true,
          type: "success"
        });

        this.page = 1;
        this.getRoleList();
        this.showDialog = false;
      }
    },
    // 获取角色数据列表
    async getRoleList() {
      let params = {
        // role: {
        key: this.form.name,
        // },
        page: this.page,
        limit: this.limit
      };
      let res = await getRoleList(params);
      if (res.ResultCode == 0) {
        this.tableData = res.Data.Items;
        this.total = res.Data.TotalRecordCount;
        this.tableData.filter((item, index) => {
          return (item.id = index + 1);
        });

        this.showEdit = res.Data.ButtonRight.indexOf("edit") != -1;
        this.showDel = res.Data.ButtonRight.indexOf("del") != -1;
        this.showState = res.Data.ButtonRight.indexOf("setState") != -1;
        this.showRole = res.Data.ButtonRight.indexOf("setright") != -1;
      }
    },
    // 获取角色详细信息
    async getRoleDetail(id) {
      let res = await getRoleDetail(id);
      if (res.ResultCode == 0) {
        this.diaform.name = res.Data.rolename;
        this.diaform.english = res.Data.englishName;
        this.diaform.desc = res.Data.remark;
        this.diaform.radio = res.Data.stateSel;
      }
    },
    // 修改角色信息
    async editRoleData(id) {
      let params = {
        pid: id,
        englishName: this.diaform.english,
        remark: this.diaform.desc,
        rolename: this.diaform.name,
        stateSel: this.diaform.radio
      };
      const loading = this.$loading({
        lock: true,
        text: "正在提交",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await editRoleData(params).catch(err => {
        loading.close();
      });

      if (res.ResultCode == 0) {
        loading.close();
        this.$message({
          message: "提交成功",
          showClose: true,
          type: "success"
        });

        // this.page = 1
        this.getRoleList();
        this.showDialog = false;
      }
    },
    // 删除角色信息
    async deleteRoleData(id) {
      let arr = [];

      arr.push(id);

      let params = {
        ids: arr.length == 1 ? arr[0] + "," : arr.join(",")
      };
      const loading = this.$loading({
        lock: true,
        text: "正在删除...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let res = await deleteRoleData(params).catch(err => {
        loading.close();
      });
      if (res.ResultCode == 0) {
        loading.close();

        this.$message({
          showClose: true,
          message: "删除成功",
          type: "success"
        });

        this.getRoleList();
        this.showDialog = false;
      }
    },
    // 获取角色按钮权限
    async getRoleBR() {
      let res = await getRoleBR();
      if (res.ResultCode == 0) {
        this.showAdd = res.Data.indexOf("add") != -1;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../../style/common.scss";
@import "../../style/base.scss";

.doctorBody {
  .form-item {
    margin-bottom: 20px;

    span {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #ff3a3a;
      display: inline-block;
      width: 8px;
    }

    .name {
      font-size: 16px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #333;
      width: 120px;
      text-align: right;
    }

    .input {
      width: 260px;
    }
  }
}

.today-data {
  margin-top: 5px;
  padding: 10px 10px;
  background: #ffffff;
  border-radius: 5px;
  height: 71vh;

  .add {
    width: 74px;
    height: 36px;
    background: #5386f6;
    border-radius: 4px;
    font-size: 14px;
    font-family: PingFang SC;
    font-weight: 400;
    color: #ffffff;
    cursor: pointer;
    margin-bottom: 20px;
  }

  .today-table {
    height: 89%;

    .enable,
    .disable {
      width: 60px;
      height: 24px;
      background: #ff6245;
      border-radius: 2px;
      font-size: 14px;
      font-family: PingFang SC;
      font-weight: 400;
      color: #ffffff;
      cursor: pointer;
      margin: 0 auto;
    }

    .disable {
      background: #68a8ff !important;
    }

    .operation {
      div {
        width: 60px;
        height: 24px;
        background: #5386f6;
        border-radius: 2px;
        cursor: pointer;
        font-size: 14px;
        font-family: PingFang SC;
        font-weight: 400;
        color: #ffffff;
      }

      .bg-yellow {
        background: #ffae45;
        margin-left: 15px;
      }

      .bg-default {
        width: 80px;
        margin-left: 15px;
      }
    }
  }
}

.table::v-deep .el-table .cell {
  text-align: center;
}
</style>
<style lang="scss" scoped>
@media screen and (min-width: 1681px) and (max-width: 1920px) {
  .today-data {
    height: 82vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 18px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 18px 0;
  }
}

@media screen and (min-width: 1601px) and (max-width: 1680px) {
  .today-data {
    height: 81vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 94%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 16px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 20px 0;
  }
}

@media screen and (min-width: 1361px) and (max-width: 1600px) {
  .today-data {
    height: 73vh;
    padding: 10px;
    // background: red;

    .today-table {
      height: 92%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 10px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 14px 0;
  }
}

@media screen and(min-width:1281px) and (max-width: 1360px) {
  .today-data {
    height: 73vh;
    padding: 5px;
    // background: red;

    .today-table {
      height: 92%;
    }
  }

  .today-table::v-deep .el-table td {
    padding: 5px 0;
  }

  .today-table::v-deep .el-table th {
    padding: 5px 0;
  }
}
</style>