<template>
  <a-card size="small" :bordered="false" class="dealerAccountList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" 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="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>
      <!-- 操作按钮 -->
      <div class="table-operator">
        <a-button id="dealerAccountList-add" type="primary" v-if="$hasPermissions('B_dealerAccount_add')" class="button-error" @click="handleEdit()">新增</a-button>
      </div>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ x: 1030, y: tableHeight }"
        bordered>
        <!-- 账号状态 -->
        <template slot="loginFlag" slot-scope="text, record">
          <a-switch
            id="dealerAccountList-enable"
            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">编辑</a-button>
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_dealerAccount_detail')"
            class="button-success"
            @click="handleDetail(record)"
            id="dealerAccountList-detail-btn">详情</a-button>
          <a-button
            size="small"
            type="link"
            v-if="$hasPermissions('B_dealerAccount_reset')"
            class="button-warning"
            @click="handleReset(record)"
            id="dealerAccountList-reset-btn">重置密码</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 :openModal="openModal" :nowData="nowData" :itemId="itemId" @ok="$refs.table.refresh(true)" @close="closeModal" />
    <!-- 查看 -->
    <dealer-account-detail-modal :openModal="openDetailModal" :itemId="itemId" @close="closeDetailModal" />
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import dealerAccountEditModal from './editModal.vue'
import dealerAccountDetailModal from './detailModal.vue'
import { dealerUserList, dealerUserEnable, dealerUserResetP } from '@/api/dealer'
export default {
  components: { STable, VSelect, dealerAccountEditModal, dealerAccountDetailModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        org: {
          name: ''
        },
        loginName: '',
        loginFlag: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: 70, align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: 140, align: 'center' },
        { title: '经销商名称', dataIndex: 'org.name', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '管理员姓名', dataIndex: 'name', width: 180, align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '管理员账号', dataIndex: 'loginName', width: 100, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '授权用户数', dataIndex: 'org.childUserMaxNum', width: 100, align: 'center', customRender: function (text) { return ((text || text == 0) ? text : '--') } },
        { title: '状态', scopedSlots: { customRender: 'loginFlag' }, width: 100, align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: 160, align: 'center', fixed: 'right' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return dealerUserList(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
            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, //  当前记录数据
      productBrandList: []
    }
  },
  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
        }
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.resetSearchForm()
    }
  },
  mounted () {
    const _this = this
    this.$nextTick(() => { // 页面渲染完成后的回调
      _this.setTableH()
    })
  }
}
</script>
