<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-select v-model="formInline.status" placeholder="请选择">
              <el-option label="全部" value=""></el-option>
              <el-option label="审核中" value="0"></el-option>
              <el-option label="未通过" value="2"></el-option>
              <el-option label="已通过" value="1"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="$refs.oTable.reload()"
              >查询</el-button
            >
          </el-form-item>
        </el-form>
        <div class="mb-2">
          <el-button type="primary" size="small" @click="addProduct"
            >添加银行卡</el-button
          >
          <!-- <el-button
            :disabled="selectList.length <= 0"
            type="danger"
            size="small"
            @click="deleteProduct"
            >批量删除</el-button
          > -->
        </div>
      </template>
    </obj-table-plus>
    <!-- 添加或编辑 -->
    <add-or-update
      @queryList="$refs.oTable.reload()"
      ref="addOrUpdate"
    ></add-or-update>
    <!-- 查看详情 -->
    <viewDetails ref="viewDetails"></viewDetails>
  </div>
</template>

<script>
import AddOrUpdate from "./popup/add-or-update.vue";
import viewDetails from "./popup/view-details.vue";
export default {
  components: { AddOrUpdate, viewDetails },
  data() {
    return {
      dataList: [],
      formInline: {
        status: "",
      },
      tableProp: {
        "auto-resize": true,
        border: true,
        height: "auto",
        "row-id": "id",
        "show-overflow": false,
      },
      productFilterType: "SALE",
      selectList: [],
    };
  },
  methods: {
    addProduct() {
      this.$refs.addOrUpdate.toggle().add();
    },
    deleteProduct() {
      console.log(this.selectList);
      let integers = this.selectList.map((item) => {
        return item.id;
      });
      this.$confirm("此操作将删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log("111");
          this.$api.mer_admin.BatchDeleteProducts(integers).then((res) => {
            this.$refs.oTable.reload();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    queryList(pageNo, pageSize) {
      this.$api.mer_admin
        .bankCardPage({
          pageNumber: pageNo,
          pageSize: pageSize,
          linkId: JSON.parse(sessionStorage.getItem("userInfo")).managerId
            ? JSON.parse(sessionStorage.getItem("userInfo")).managerId
            : JSON.parse(sessionStorage.getItem("userInfo")).merchantId,
          status: this.formInline.status,
        })
        .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);
        });
    },
  },
  computed: {
    tableCols() {
      return [
        { type: "checkbox", width: "60px", fixed: "left" },
        // { type: "seq", width: "60px", align: "center", title: "序号" },
        {
          title: "持卡人",
          align: "center",
          field: "cardName",
        },
        {
          title: "银行卡号",
          align: "center",
          field: "cardNo",
        },
        {
          title: "账户类型",
          align: "center",
          field: "bankAcctType",
          type: "jsx",
          render: ({ row }) => {
            if (row.bankAcctType == 2) {
              return <span>对私</span>;
            } else {
              return <span>对公</span>;
            }
          },
        },
        {
          title: "手机号",
          align: "center",
          field: "legalMp",
        },
        {
          title: "企业名称",
          align: "center",
          field: "name",
        },
        {
          title: "状态",
          align: "center",
          field: "status",
          type: "jsx",
          render: ({ row }) => {
            if (row.status == 0) {
              return <span>审核中</span>;
            } else if (row.status == 2) {
              return <span>未通过</span>;
            } else if (row.status == 1) {
              return <span>已通过</span>;
            }
          },
        },
        {
          title: "默认银行卡",
          align: "center",
          field: "status",
          type: "jsx",
          render: ({ row }) => {
            let defaultBank = () => {
              if (row.default) {
                this.$confirm(
                  "此操作将设置该银行卡为默认银行卡, 是否继续?",
                  "提示",
                  {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                  }
                )
                  .then(() => {
                    this.$api.mer_admin
                      .defaultBank({
                        linkId: row.linkId,
                        bankId: row.id,
                      })
                      .then((res) => {
                        this.$refs.oTable.reload();
                      })
                      .catch((err) => {
                        this.$refs.oTable.reload();
                      });
                  })
                  .catch(() => {
                    this.$refs.oTable.reload();
                  });
              }
            };
            return (
              <el-switch
                onchange={defaultBank}
                disabled={row.default}
                v-model={row.default}
                active-text="是"
                inactive-text="否"
              ></el-switch>
            );
          },
        },
        {
          title: "创建时间",
          align: "center",
          field: "createTime",
        },
        {
          title: "操作",
          fixed: "right",
          type: "jsx",
          align: "center",
          width: "160px",
          render: (row) => {
            let deleteBank = () => {
              this.$api.mer_admin.bankCardDelete(row.row.id).then((res) => {
                this.$refs.oTable.reload();
              });
            };
            let edit = () => {
              this.$refs.viewDetails.toggle(row).update();
            };
            return (
              <div>
                <el-button size="mini" type="primary" onClick={edit}>
                  详情
                </el-button>
                <span style={"margin-left:10px"}>
                  <el-popconfirm onConfirm={deleteBank} title="确定删除吗？">
                    <el-button size="mini" type="danger" slot="reference">
                      删除
                    </el-button>
                  </el-popconfirm>
                </span>
              </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>