<template>
  <section class="AppFinanceOrg">
    <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.organizationName" 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="organizationName">
          <el-input v-model.trim="form.organizationName" placeholder="请输入" clearable maxlength="32" show-word-limit/>
        </el-form-item>
        <el-form-item required label="机构类型" prop="organizationType">
          <ai-select v-model="form.organizationType" :selectList="dict.getDict('financialOrganizationType')"
                     placeholder="请选择"/>
        </el-form-item>
        <el-form-item label="抢单数量" prop="maxOrderNumber">
          <el-input type="number" v-model.number="form.maxOrderNumber" placeholder="每日最大抢单数量" clearable/>
        </el-form-item>
        <el-form-item label="logo" prop="logoUrl">
          <ai-uploader v-model="form.logoUrl" :instance="instance" :limit="1" isShowTip isCrop
                       :cropOps="{autoCropWidth:264,autoCropHeight:56,fixed:false}"/>
        </el-form-item>
      </el-form>
    </ai-dialog>
  </section>
</template>

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

export default {
  name: "AppFinanceOrg",
  label: "金融机构管理",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    rules() {
      return {
        organizationName: [{required: true, message: "请输入机构名称"}],
        organizationType: [{required: true, message: "请选择机构类型"}],
        logoUrl: [{required: true, message: "请上传logo"}],
        maxOrderNumber: [{required: true, message: "请输入抢单数量"}, {pattern: /^\d+$/, message: '请输入正整数'}],
      }
    },
  },
  data() {
    return {
      page: {current: 1, size: 10, total: 0},
      dialog: false,
      tableData: [],
      search: {organizationName: ""},
      form: {logoUrl: []},
      colConfigs: [
        {label: "金融机构名称", prop: "organizationName"},
        {label: "抢单数量", prop: "maxOrderNumber"},
        {label: "创建人", prop: "createUserName", align: 'center'},
        {label: "创建时间", prop: "createTime", align: '120px'},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appfinancialorganization/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) {
          let {form: {logoUrl: logo}} = this, logoUrl = logo?.[0]?.url
          this.instance.post("/appfinancialorganization/addOrUpdate", {...this.form, logoUrl}).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))
      this.form.logoUrl = [{url: row.logoUrl}]
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该机构?").then(() => {
        this.instance.post("/appfinancialorganization/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    }
  },
  created() {
    this.dict.load('financialOrganizationType')
    this.getTableData()
  }
}
</script>

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

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