<template>
  <div>
    <a-card size="small" :bordered="false" class="searchBoxNormal">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper newTableSearchName">
        <a-form layout="inline" id="businessOwnerSettings-form" @keyup.enter.native="$refs.table.refresh(true)">
          <a-row :gutter="15">
            <a-col :md="6" :sm="24">
              <a-form-item label="人员类型">
                <v-select
                  v-model="queryParam.bizUserType"
                  ref="printStatus"
                  id="businessOwnerSettings-printStatus"
                  code="BIZ_USER_TYPE"
                  placeholder="请选择人员类型"
                  allowClear></v-select>
              </a-form-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="6" :sm="24">
              <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" @click="handleAdd">新增</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="showWarehouse" slot-scope="text, record">
            <div v-if="record.warehouseList&&record.warehouseList.length>0 ">
              <a-tooltip placement="bottom" class="warehouseBox">
                <template slot="title">
                  <span v-for="con in record.warehouseList" :key="con.id">
                    {{ con.name }}
                  </span>
                </template>
                <span v-for="con in record.warehouseList" :key="con.id">
                  {{ con.name }}
                </span>
              </a-tooltip>
            </div>
            <div v-else>--</div>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              :id="'businessOwnerSettings-set-btn'+record.id"
              class="button-warning"
              v-if="record.allDealerFlag!=1 || record.allProductFlag!=1 ||record.allSupplierFlag&&record.allSupplierFlag!=1"
              @click="handleSetCategory(record)">设置管辖范围</a-button>
            <a-button size="small" type="link" :id="'businessOwnerSettings-edit-btn'+record.id" class="button-info" @click="handleEdit(record)">编辑</a-button>
            <a-button size="small" type="link" :id="'businessOwnerSettings-del-btn'+record.id" 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 addModal from './addModal'
// 接口
import { queryPage, bizuserDelete } from '@/api/bizuser'
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: '管辖供应商', dataIndex: 'allSupplierFlagDictValue', width: '18%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '管辖仓库', scopedSlots: { customRender: 'showWarehouse' }, width: '22%', align: 'center' },
        { 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: ''// 当前id
    }
  },
  methods: {
    // 设置管辖范围
    handleSetCategory (row) {
      this.$router.push({ name: 'categorySet', query: { sn: row.userSn, id: row.id } })
    },
    // 新增
    handleAdd () {
      this.itemId = ''
      this.openAddModal = true
    },
    //  重置
    resetSearchForm () {
      this.queryParam = {
        bizUserType: '',
        userName: ''
      }
      this.itemId = ''
      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 - 240
    }
  },
  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;
    .warehouseBox{
      width: 100%;
      display: -webkit-box;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      overflow: hidden;
      text-overflow: ellipsis;
    }
    .warehouseBox span::after{
      content:','
    }
    .warehouseBox span:last-child::after{
      content:''
    }
  }
</style>
