<template>
  <section class="AppFinanceOrgMember">
    <ai-list>
      <ai-title slot="title" title="金融机构成员" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button size="small" type="primary" icon="iconfont iconAdd" @click="dialog=true">添加
            </el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="成员姓名、金融机构名称" v-model="search.name" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" align="center" label="操作" fixed="right" width="160px">
            <el-row type="flex" justify="center" align="middle" slot-scope="{row}">
              <el-button type="text" @click="handleEdit(row)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </el-row>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
    <ai-dialog title="添加机构成员" :visible.sync="dialog" width="600px" @closed="form={}" @onConfirm="submitAddAcount">
      <el-form ref="addAccountForm" :model="form" :rules="rules" size="small"
               label-width="120px">
        <el-form-item required label="选择机构成员" prop="userId">
          <ai-select v-model="form.userId" action="/user/page" placeholder="请选择" :prop="{label:`name`}"
                     :instance="instance"/>
        </el-form-item>
        <el-form-item required label="成员角色" prop="userRole">
          <ai-select v-model="form.userRole" :selectList="dict.getDict('financialOrganizationUserRole')"
                     placeholder="请选择"/>
        </el-form-item>
        <el-form-item label="所属金融机构" prop="organizationId">
          <ai-select v-model="form.organizationId" action="/appfinancialorganization/list" placeholder="请选择"
                     :prop="{label:`organizationName`}" :instance="instance"/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "AppFinanceOrgMember",
  label: "金融机构成员",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    rules() {
      return {
        organizationName: [{required: true, message: "请输入机构名称"}],
        userRole: [{required: true, message: "请选择成员角色"}],
        organizationId: [{required: true, message: "请选择所属金融机构"}],
        userId: [{required: true, message: "请选择机构成员"}],
      }
    },
  },
  data() {
    return {
      page: {current: 1, size: 10, total: 0},
      dialog: false,
      tableData: [],
      search: {organizationName: ""},
      form: {},
      colConfigs: [
        {label: "所属金融机构", prop: "organizationName"},
        {label: "成员姓名", prop: "name"},
        {label: "成员职务", prop: "userRole", dict: "financialOrganizationUserRole"},
        {label: "创建人", prop: "createUserName", align: 'center', width: "120px"},
        {label: "创建时间", prop: "createTime"},
        {slot: "options"}
      ],
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appfinancialorganizationuser/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    submitAddAcount() {
      this.$refs.addAccountForm.validate(v => {
        if (v) {
          this.instance.post("/appfinancialorganizationuser/addOrUpdate", this.form).then(res => {
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.getTableData()
            }
          })
        }
      })
    },
    handleEdit(row) {
      this.dialog = true
      this.form = JSON.parse(JSON.stringify(row))
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该成员?").then(() => {
        this.instance.post("/appfinancialorganizationuser/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    }
  },
  created() {
    this.dict.load('financialOrganizationUserRole')
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.AppFinanceOrgMember {
  height: 100%;

  ::v-deep .avatar {
    width: 40px;
    height: 40px;
    margin-right: 10px;
  }
}
</style>
