<template>
  <div>
    <a-card size="small" :bordered="false" class="dealerAccountList-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper">
        <a-form layout="inline" id="dealerAccountList-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="经销商名称">
                <a-input id="dealerAccountList-name" v-model.trim="queryParam.org.name" allowClear placeholder="请输入经销商名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="管理员账号">
                <a-input id="dealerAccountList-loginName" v-model.trim="queryParam.loginName" allowClear placeholder="请输入管理员账号"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="账号状态">
                <v-select code="ENABLE_FLAG" id="dealerAccountList-loginFlag" v-model="queryParam.loginFlag" allowClear placeholder="请选择账号状态"></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="dealerAccountList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="dealerAccountList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator">
          <a-button id="dealerAccountList-add" type="primary" v-if="$hasPermissions('B_dealerAccount_add')" @click="handleEdit()">新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+70+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :data="loadData"
          :pageSize="30"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 账号状态 -->
          <template slot="loginFlag" slot-scope="text, record">
            <a-switch
              :id="'dealerAccountList-enable'+record.id"
              v-if="$hasPermissions('B_dealerAccount_enable')"
              checkedChildren="启用"
              unCheckedChildren="禁用"
              @change="changeStatus(record)"
              :checked="record.loginFlag == 1 ? true : false" />
            <span v-else :style="{color:(record.loginFlag==1?'#00aa00':'#999')}"> {{ record.loginFlag==1? '已启用': '已禁用' }} </span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_dealerAccount_edit')"
              class="button-info"
              @click="handleEdit(record)"
              :id="'dealerAccountList-edit-btn'+record.id">编辑</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_dealerAccount_detail')"
              class="button-success"
              @click="handleDetail(record)"
              :id="'dealerAccountList-detail-btn'+record.id">详情</a-button>
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_dealerAccount_reset')"
              class="button-warning"
              @click="handleReset(record)"
              :id="'dealerAccountList-reset-btn'+record.id">重置密码</a-button>
            <span v-if="!$hasPermissions('B_dealerAccount_edit') && !$hasPermissions('B_dealerAccount_detail') && !$hasPermissions('B_dealerAccount_reset')">--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 新增/编辑 -->
      <dealer-account-edit-modal
        v-drag
        :openModal="openModal"
        :nowData="nowData"
        :itemId="itemId"
        @ok="$refs.table.refresh(true)"
        @close="closeModal" />
      <!-- 查看 -->
      <dealer-account-detail-modal v-drag :openModal="openDetailModal" :itemId="itemId" @close="closeDetailModal" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import dealerAccountEditModal from './editModal.vue'
import dealerAccountDetailModal from './detailModal.vue'
// 接口
import { dealerUserList, dealerUserEnable, dealerUserResetP } from '@/api/dealer'
export default {
  name: 'DealerAccountManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, dealerAccountEditModal, dealerAccountDetailModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: { //  查询条件
        org: {
          name: ''// 经销商名称
        },
        loginName: '', // 管理员账号
        loginFlag: undefined// 状态
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '11%', align: 'center' },
        { title: '经销商名称', dataIndex: 'org.name', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '管理员姓名', dataIndex: 'name', width: '14%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '管理员账号', dataIndex: 'loginName', width: '14%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '授权用户数', dataIndex: 'org.childUserMaxNum', width: '8%', align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '状态', scopedSlots: { customRender: 'loginFlag' }, width: '9%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '15%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dealerUserList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            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
              data.list[i].loginFlag = Number(data.list[i].loginFlag)
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      },
      openModal: false, //  新增编辑  弹框
      openDetailModal: false, // 详情  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    //  重置
    resetSearchForm () {
      this.queryParam.org.name = ''
      this.queryParam.loginName = ''
      this.queryParam.loginFlag = undefined
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.itemId = row ? row.id : ''
      this.nowData = row || null
      this.openModal = true
    },
    // 详情
    handleDetail (row) {
      this.itemId = row ? row.id : ''
      this.openDetailModal = true
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.nowData = null
      this.openModal = false
    },
    // 关闭详情弹框
    closeDetailModal () {
      this.itemId = ''
      this.openDetailModal = false
    },
    // 重置密码
    handleReset (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '重置密码后，新的密码会以短信形式发送到【管理员手机】，确定要重置吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          dealerUserResetP({ id: row.id }).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$refs.table.refresh()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        }
      })
    },
    // 启用 禁用
    changeStatus (record) {
      const params = {
        id: record.id,
        flag: record.loginFlag == 1 ? '0' : '1'
      }
      this.spinning = true
      dealerUserEnable(params).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.$refs.table.refresh()
          this.spinning = false
        }
      })
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    // 计算表格高度
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.$refs.table.refresh()
      this.pageInit()
    }
  }
}
</script>
