<template>
  <div class="client-account-set ">
    <div class="part-box-top">
      <Button type="success" icon="md-cloud-upload" @click="toEdit({})"
        >新增</Button
      >
    </div>
    <div class="part-box">
      <Table
        ref="TableList"
        :columns="columes"
        :data="users"
        disabled-hover
        border
      ></Table>

      <div class="part-page">
        <Page
          :current="current"
          :total="total"
          :page-size="size"
          show-total
          show-elevator
          @on-change="toPage"
        ></Page>
      </div>
    </div>

    <!-- modify-client-user -->
    <modify-client-user
      :instance="curUser"
      @modified="getList"
      ref="ModifyClientUser"
    ></modify-client-user>
  </div>
</template>

<script>
import { clientUserPageList, deleteClientUser, uploadClientUser } from "@/api";
import ModifyClientUser from "./components/ModifyClientUser";

export default {
  name: "client-account-set",
  components: { ModifyClientUser },
  data() {
    return {
      workId: this.$route.params.workId,
      current: 1,
      size: this.GLOBAL.pageSize,
      total: 0,
      users: [],
      curUser: {},
      columes: [
        {
          type: "index",
          title: "序号",
          width: 100,
          align: "center",
          indexMethod: row => {
            return (this.current - 1) * this.size + row._index + 1;
          }
        },
        {
          title: "账号",
          key: "loginName"
        },
        {
          title: "密码",
          key: "password"
        },
        {
          title: "操作",
          key: "action",
          width: 240,
          align: "center",
          render: (h, param) => {
            let actions = [
              {
                name: param.row.enabled ? "禁用" : "启用",
                type: param.row.enabled ? "error" : "primary",
                action: () => {
                  this.toAble(param.row);
                }
              },
              {
                name: "编辑",
                action: () => {
                  this.toEdit(param.row);
                }
              },
              {
                name: "删除",
                type: "error",
                action: () => {
                  this.toDelete(param.row);
                }
              }
            ];
            return h("div", this.$tableAction(h, actions));
          }
        }
      ]
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    async getList() {
      const datas = {
        workId: this.workId,
        page: this.current - 1,
        size: this.size
      };
      const data = await clientUserPageList(datas);
      this.users = data;
      this.total = 10;
    },
    toPage(page) {
      this.current = page;
      this.getList();
    },
    toEdit(row) {
      this.curUser = row;
      this.$refs.ModifyClientUser.open();
    },
    async toAble(row) {
      await uploadClientUser({
        id: row.id,
        loginName: row.loginName,
        enabled: !row.enabled
      });
      row.enabled = !row.enabled;
    },
    toDelete(row) {
      this.$Modal.confirm({
        title: "删除警告",
        content: "确定要删除当前账号吗？",
        onOk: () => {
          this.toDel(row.id);
        }
      });
    },
    async toDel(id) {
      await deleteClientUser(id);
      this.$Message.success("删除成功！");
      this.deletePageLastItem();
    }
  }
};
</script>
