<template>
  <a-card size="small" :bordered="false" class="userAuthList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="中心店名称">
                <a-input id="userAuthList-ownerOrgName" v-model.trim="queryParam.ownerOrgName" allowClear placeholder="请输入中心店名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="中心店授权账号">
                <a-input id="userAuthList-userQueryWord" v-model.trim="queryParam.userQueryWord" allowClear placeholder="请输入中心店授权账号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="配送店名称">
                <a-input id="userAuthList-orgName" v-model.trim="queryParam.orgName" allowClear placeholder="请输入配送店名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="userAuthList-refresh">查询</a-button>
              <a-button style="margin-left: 8px" @click="resetSearchForm" :disabled="disabled" id="userAuthList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button id="userAuthList-add" type="primary" class="button-error" @click="handleEdit()">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable"
        ref="table"
        size="default"
        :rowKey="(record) => record.userSn"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        bordered>
        <!-- 授权账号 -->
        <template slot="loginName" slot-scope="text, record">
          <span>{{ record.user.name || '' }} - {{ record.user.loginName || '' }}</span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <a-button
            size="small"
            type="link"
            class="button-info"
            @click="handleEdit(record)"
            id="userAuthList-edit-btn">编辑</a-button>
          <a-button
            size="small"
            type="link"
            class="button-error"
            @click="handleDel(record)"
            id="userAuthList-del-btn">删除</a-button>
        </template>
      </s-table>
    </a-spin>
    <!-- 新增/编辑 -->
    <storeManagement-user-auth-edit-modal :openModal="openModal" :itemSn="itemSn" @ok="$refs.table.refresh(true)" @close="closeModal" />
  </a-card>
</template>

<script>
import { STable } from '@/components'
import storeManagementUserAuthEditModal from './editModal.vue'
import { userOrgList, userOrgDel } from '@/api/userOrg'
export default {
  components: { STable, storeManagementUserAuthEditModal },
  data () {
    return {
      spinning: false,
      queryParam: { //  查询条件
        ownerOrgName: '',
        userQueryWord: '',
        orgName: ''
      },
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 80, align: 'center' },
        { title: '中心店名称', dataIndex: 'ownerOrgName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '中心店授权账号', scopedSlots: { customRender: 'loginName' }, width: 200, align: 'center' },
        { title: '已绑配送店', dataIndex: 'userAllOrgName', align: 'center', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 150, align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        if (this.tableHeight == 0) {
          this.tableHeight = window.innerHeight - 380
        }
        return userOrgList(Object.assign(parameter, this.queryParam)).then(res => {
          const data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          return data
        })
      },
      openModal: false, //  新增编辑  弹框
      itemSn: '' //  当前sn
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.ownerOrgName = ''
      this.queryParam.userQueryWord = ''
      this.queryParam.orgName = ''
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemSn = row ? row.userSn : null
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemSn = ''
      this.openModal = false
    },
    //  删除
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '删除后会直接解除原有的授权，确定删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          userOrgDel({
            sn: row.userSn
          }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    }
  }
}
</script>
