<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add"
          >{{ $t("create") }}
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-compass"
          :disabled="selection.length == 0"
          @click="assignRole"
          >{{ $t("admin.assignRole") }}
        </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>
              <el-select
                v-model="search.queryType"
                style="width: 120px"
                :placeholder="$t('pleaseChoose')"
                class="filter-item"
              >
                <el-option
                  v-for="item in queryTypes"
                  :key="item"
                  :label="$t('admin.adminQueryTypeOption' + item)"
                  :value="item"
                />
              </el-select>
            </template>
          </el-input>
          <el-form-item
            ref="form"
            style="padding: 20px 0px 0px 0px"
            :label="$t('admin.adminRoleQuery')"
          >
            <sc-select
              v-model="search.roleCode"
              :apiObj="$API.adminManage.adminRoleSelectList"
              clearable
              filterable
              style="width: 100%"
            ></sc-select>
          </el-form-item>
          <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="UserCode"
        @selection-change="selectionChange"
        stripe
      >
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column
          :label="$t('admin.accountName')"
          prop="AdminName"
        ></el-table-column>
        <el-table-column
          :label="$t('admin.fullName')"
          prop="FullName"
        ></el-table-column>
        <el-table-column
          :label="$t('admin.adminRoleName')"
          prop="DefaultRoleName"
        ></el-table-column>
        <el-table-column :label="$t('createTime')" prop="CreateTime">
          <template #default="scope">
            <span>{{
              scope.row.CreateTime.slice(0, 19).replace("T", " ")
            }}</span>
          </template>
        </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">{{
                  $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>

  <save-role
    v-if="dialog.showRole"
    ref="saveRoleDialog"
    @success="saveRole"
    @closed="dialog.showRole = false"
  ></save-role>
</template>

<script>
import saveRole from "./saveRole";
import saveDialog from "./saveAdmin";


export default {
  components: {
    saveDialog,
    saveRole,
  },
  data() {
    return {
      dialog: {
        show: false,
        showRole: false,
      },
      apiObj: this.query,
      search: {
        queryType: "0",
        roleCode: "",
        keyword: null,
      },
      queryTypes: ["0", "1"],
      selection: [],
      confirmDel: this.$t("admin.adminConfirmDelete"),
    };
  },
  methods: {
    async query(params) {
      return await this.$API.adminManage.adminList.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) {
      await this.$API.wing.post(
        "RemoveAdminAsync",
        {
          AdminCode: row.AdminCode,
        },
        this.refresh
      );
    },
    //分配角色
    assignRole() {
      this.dialog.showRole = true;
      this.$nextTick(() => {
        var roleCode = "";
        if (this.selection.length == 1) {
          roleCode = this.selection[0].DefaultRoleCode;
        }
        this.$refs.saveRoleDialog.open().setData(roleCode);
      });
    },
    //保存角色
    async saveRole(selectCode) {
      if (this.selection.length == 0) {
        return;
      }
      var adminCodes = this.selection.map((x) => x.AdminCode);
      await this.$API.wing.post(
        "ModifyAdminsRoles",
        {
          AdminCodes: adminCodes,
          AdminRoleCodes: [selectCode],
        },
        this.refresh
      );
    },
    //表格选择后回调事件
    selectionChange(selection) {
      this.selection = selection;
    },
  },
};
</script>

<style>
</style>
