<template>
  <div>
    <a-card size="small" :bordered="false">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
        <a-form layout="inline" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="人员类型">
                <v-select
                  v-model="queryParam.bizUserType"
                  ref="printStatus"
                  id="businessOwnerSettings-printStatus"
                  code="BIZ_USER_TYPE"
                  placeholder="请选择人员类型"
                  allowClear></v-select>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="人员姓名">
                <a-input id="businessOwnerSettings-name" v-model.trim="queryParam.userName" allowClear placeholder="请输入人员姓名"/>
              </a-form-item>
            </a-col>
            <a-col :md="24" :sm="24" style="margin-top: 10px;text-align:center;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="businessOwnerSettings-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="businessOwnerSettings-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false" class="dealerZoneSearch-wrap">
      <a-spin :spinning="spinning" tip="Loading...">
        <div class="table-operator">
          <a-button id="businessOwnerSettings-add" type="primary" class="button-error" @click="handleAdd">+新增</a-button>
        </div>
        <!-- 列表 -->
        <s-table
          class="sTable fixPagination"
          ref="table"
          :style="{ height: tableHeight+84.5+'px' }"
          size="small"
          :rowKey="(record) => record.id"
          :columns="columns"
          :showPagination="false"
          :data="loadData"
          :scroll="{ y: tableHeight }"
          :defaultLoadData="false"
          bordered>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="link" class="button-warning" v-if="record.allDealerFlag!=1&&record.allProductFlag!=1" @click="handleSetCategory(record)">设置管辖范围</a-button>
            <a-button size="small" type="link" class="button-info" @click="handleEdit(record)">编辑</a-button>
            <a-button size="small" type="link" class="button-error" @click="handleDelete(record)">删除</a-button>
          </template>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 新增 -->
    <addModal :itemId="itemId" ref="addUser" :openModal="openAddModal" @close="closeModal" @ok="$refs.table.refresh(true)"></addModal>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import { queryPage, bizuserDelete } from '@/api/bizuser'
import addModal from './addModal'
export default {
  name: 'BusinessOwnerSettings',
  mixins: [commonMixin],
  components: { STable, VSelect, addModal },
  data () {
    return {
      spinning: false,
      disabled: false, //  查询、重置按钮是否可操作
      tableHeight: 0,
      queryParam: {
        bizUserType: '',
        userName: undefined
      },
      columns: [
        { title: '创建时间', dataIndex: 'createDate', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '人员姓名', dataIndex: 'userName', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '人员类型', dataIndex: 'bizUserTypeDictValue', width: '15%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '管辖经销商', dataIndex: 'allDealerFlagDictValue', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '管辖品类', dataIndex: 'allProductFlagDictValue', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '20%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return queryPage(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          data = res.data
          const no = (data.pageNo - 1) * data.pageSize
          this.total = data.count || 0
          for (var i = 0; i < data.list.length; i++) {
            data.list[i].no = no + i + 1
          }
          this.disabled = false
          this.spinning = false
          return data
        })
      },
      openAddModal: false, //  编辑  弹框
      itemId: ''
    }
  },
  methods: {
    userChange (val) {
      this.queryParam.userSn = val.key
    },
    handleSetCategory (row) {
      const obj = {
        name: row.userName,
        d_flag: row.allDealerFlag,
        p_flag: row.allProductFlag
      }
      const newString = JSON.stringify(obj)
      const newObj = encodeURI(newString)
      this.$router.push({ name: 'categorySet', query: { sn: row.userSn, con: newObj } })
    },
    // 新增
    handleAdd () {
      this.openAddModal = true
    },
    //  重置
    resetSearchForm () {
      this.queryParam = {
        bizUserType: '',
        userName: ''
      }
      this.$refs.table.refresh(true)
    },
    // 删除
    handleDelete (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确定要删除吗？',
        centered: true,
        onOk () {
          _this.spinning = true
          bizuserDelete({ 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
            }
          })
        }
      })
    },
    //  编辑
    handleEdit (row) {
      this.itemId = row.id
      this.openAddModal = true
      const _this = this
      this.$nextTick(() => {
        _this.$refs.addUser.getEditInfo(row)
      })
    },
    //  关闭弹框
    closeModal () {
      this.itemId = ''
      this.openAddModal = false
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.resetSearchForm()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 275
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>
<style lang="less">
  .dealerZoneSearch-wrap{
    height: 100%;
    margin-top:6px;
    .table-operator{
      text-align:right;
    }
  }
</style>
