<template>
  <div style="height: calc(100vh - 200px)">
    <obj-table-plus
      ref="oTable"
      style="height: 100%"
      :tableCols="tableCols"
      :tableProp="tableProp"
      @query="queryList"
      v-model="dataList"
      :tableEvent="tableEvent"
    >
      <template slot="tableTop">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="姓名">
            <el-input
              v-model="formInline.username"
              placeholder="姓名搜索"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="$refs.oTable.reload()"
              >查询</el-button
            >
            <el-button type="primary">重置</el-button>
          </el-form-item>
        </el-form>
        <div class="mb-2">
          <el-button type="primary" @click="addAccount">添加</el-button>
        </div>
      </template>
    </obj-table-plus>
    <!-- 编辑编辑账号 -->
    <addOrUpdate
      ref="addOrUpdate"
      @queryList="$refs.oTable.reload()"
    ></addOrUpdate>
  </div>
</template>

<script>
import addOrUpdate from "./popup/add-or-update.vue";
export default {
  components: { addOrUpdate },
  data() {
    return {
      dataList: [],
      formInline: {
        name: "",
      },
      tableProp: {
        "auto-resize": true,
        border: true,
        height: "auto",
        "row-id": "id",
        "show-overflow": false,
      },
      productFilterType: "SALE",
      selectList: [],
    };
  },
  created() {
    this.$nextTick(() => {
      this.$refs.oTable.reload();
    });
  },
  methods: {
    queryList(pageNo, pageSize) {
      this.$api.accountNumber
        .accountPage({
          pageNumber: pageNo,
          pageSize: pageSize,
          ownerId: JSON.parse(sessionStorage.getItem("userInfo")).managerId
            ? JSON.parse(sessionStorage.getItem("userInfo")).managerId
            : JSON.parse(sessionStorage.getItem("userInfo")).merchantId,
        })
        .then((res) => {
          console.log(res);
          this.$refs.oTable.complete(
            res.data.data.data,
            Number(res.data.data.total)
          );
        })
        .catch((err) => {
          this.$refs.oTable.complete(false);
        });
    },
    addAccount() {
      console.log("添加账号");
      this.$refs.addOrUpdate.toggle().add();
    },
  },
  computed: {
    tableCols() {
      return [
        { type: "checkbox", width: "60px", fixed: "left" },
        // { type: "seq", width: "60px", align: "center", title: "序号" },
        {
          title: "姓名",
          align: "center",
          field: "username",
        },
        {
          title: "账号",
          align: "center",
          field: "mobile",
        },
        {
          title: "加入时间",
          align: "center",
          field: "createTime",
        },
        {
          title: "操作",
          fixed: "right",
          type: "jsx",
          align: "center",
          width: "240px",
          render: (row) => {
            let edit = () => {
              this.$refs.addOrUpdate.toggle(row).update();
            };
            let deleteBanner = () => {
              this.$api.accountNumber
                .deleteAccount({ id: row.row.id })
                .then((res) => {
                  console.log(res);
                  this.$refs.oTable.reload();
                });
            };
            let onCancel = () => {};
            return (
              <div>
                <el-button
                  style="margin-right:20px"
                  size="mini"
                  type="primary"
                  onClick={edit}
                >
                  编辑
                </el-button>
                <el-popconfirm
                  onConfirm={deleteBanner}
                  onCancel={onCancel}
                  confirm-button-text="确定"
                  cancel-button-text="取消"
                  icon="el-icon-info"
                  icon-color="red"
                  title="确定删除吗？"
                >
                  <el-button size="mini" type="danger" slot="reference">
                    删除
                  </el-button>
                </el-popconfirm>
              </div>
            );
          },
        },
      ];
    },
    tableEvent() {
      return {
        "checkbox-all": ({ records, reserves }) => {
          this.selectList = [...records, ...reserves];
        },
        "checkbox-change": ({ records, reserves }) => {
          this.selectList = [...records, ...reserves];
        },
      };
    },
  },
};
</script>

<style lang="scss" scoped></style>