<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"
          >{{ $t("create") }}
        </el-button>
      </div>
      <el-form ref="form" class="right-panel">
        <div class="right-panel-search">
           <el-input
            v-model="search.keyword"
            clearable
            style="width: 320px"
            :placeholder="$t('pleaseInput')"
          >
            <template #prepend>
              {{$t('admin.adminRoleName')}}
            </template>
          </el-input>
          <el-button type="primary" icon="el-icon-search" @click="upsearch">{{
            $t("query")
          }}</el-button>
        </div>
      </el-form>
    </el-header>
    <el-main class="nopadding">
      <scTable
        ref="table"
        :apiObj="apiObj"
        :pageSize="10"
        row-key="RoleCode"
        stripe
      >
         <el-table-column
          :label="$t('admin.adminRoleName')"
          prop="RoleName"
        ></el-table-column>
        <el-table-column
          :label="$t('response.description')"
          prop="Description"
        ></el-table-column>
        <el-table-column
          :label="$t('admin.roleUserNum')"
          prop="AdminNum"
        ></el-table-column>
        <el-table-column
          :label="$t('operate')"
          fixed="right"
          align="center"
          width="120"
        >
          <template #default="scope">
            <!-- 编辑 -->
            <el-button
              type="text"
              size="small"
              @click="table_edit(scope.row, scope.$index)"
              >{{ $t("edit") }}</el-button
            >
            <!-- 删除 -->
            <el-popconfirm
              :title="confirmDel"
              @confirm="table_del(scope.row, scope.$index)"
            >
              <template #reference>
                <el-button
                  type="text"
                  size="small"
                  @click="delValid(scope.row)"
                  >{{ $t("delete") }}</el-button
                >
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>

  <save-dialog
    v-if="dialog.show"
    ref="saveDialog"
    @success="refresh"
    @closed="dialog.show = false"
  ></save-dialog>
</template>

<script>
import saveDialog from "./saveAdminRole";


export default {
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        show: false,
      },
      apiObj: this.query,
      search: {
        keyword: null
      },
      confirmDel: "",
    };
  },
  methods: {
    async query(params) {
      return await this.$API.adminManage.adminRoleList.post(params);
    },
    //搜索
    upsearch() {
      this.$refs.table.upData(this.search);
    },
    //刷新当前页
    refresh() {
      this.$refs.table.refresh();
    },
    //增加
    add() {
      this.dialog.show = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open();
      });
    },
    //编辑
    table_edit(row) {
      this.dialog.show = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //删除
    async table_del(row) {
        var res = await this.$API.wing.post("QueryRoleAdminNum", {
        AdminRoleCode: row.RoleCode,
      });
      if(res >0 )
      {
          return false;
      }
      await this.$API.wing.post(
        "DeleteAdminRole",
        {
          AdminRoleCode: row.RoleCode,
        },
        this.refresh
     );
    },
    //删除前验证
    async delValid(row) {
      this.confirmDel ="";
      var res = await this.$API.wing.post("QueryRoleAdminNum", {
        AdminRoleCode: row.RoleCode,
      });
     
      if (res > 0) {
        this.confirmDel = this.$t("admin.adminRoleDel1")+res+this.$t("admin.adminRoleDel2");
        return false;
      }
      else
      {
        this.confirmDel = this.$t("admin.adminRoleConfirmDelete");
      }
    }
  },
};
</script>

