<template>
  <div class="supplierSet-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <div class="supplierSetCon-wrap">
        <!-- 操作按钮 -->
        <div class="table-page-search-wrapper flex-center" style="width:100%;justify-content: space-between;margin-bottom:10px">
          <div class="supplierSet-btnGroup">
            <a-button type="primary" id="supplierSet-add" @click="openSupplierModal">新增</a-button>
          </div>
          <!-- 搜索条件 -->
          <div>
            <a-form layout="inline" id="supplierSet-form" @keyup.enter.native="$refs.table.refresh(true)">
              <a-input style="width:300px;" id="supplierSet-dealerName" v-model.trim="queryParam.supplierName" allowClear placeholder="请输入供应商名称"/>
              <a-button style="margin-left: 15px" type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="supplierSet-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="supplierSet-reset">重置</a-button>
            </a-form>
          </div>
        </div>
        <a-spin :spinning="spinning" tip="Loading...">
          <!-- 列表 -->
          <s-table
            class="sTable fixPagination"
            ref="table"
            size="small"
            :rowKey="(record) => record.id"
            :columns="columns"
            :data="loadData"
            :defaultLoadData="false"
            :style="{ height: tableHeight + 70 +'px' }"
            :scroll="{ y: tableHeight }"
            bordered>
            <!-- 操作 -->
            <template slot="action" slot-scope="text, record">
              <a-button
                size="small"
                type="link"
                :id="'supplierSet-del-btn'+record.id"
                class="button-error"
                @click="handleDel(record)"
              >
                删除
              </a-button>
            </template>
          </s-table>
        </a-spin>
      </div>
    </a-spin>
    <!-- 选择供应商 -->
    <a-drawer
      title="选择供应商"
      width="80%"
      :zIndex="100"
      :visible="openChooseSupplier"
      :get-container="false"
      :wrap-style="{ position: 'absolute' }"
      @close="openChooseSupplier = false">
      <div class="dealerModalCon">
        <chooseSupplier ref="supplierChoose" @plAdd="handleAddSupplier"></chooseSupplier>
      </div>
    </a-drawer>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable } from '@/components'
import chooseSupplier from './chooseSupplier.vue'
// 接口
import { querySupplierScopePage, querySupplierScope, saveSupplierList, bizuserScopeDelete } from '@/api/bizuser'
export default {
  name: 'SupplierSet',
  mixins: [commonMixin],
  components: { chooseSupplier, STable },
  props: {
    bizUserSn: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 表格高度
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '添加时间', dataIndex: 'createDate', width: '30%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '供应商名称', dataIndex: 'supplier.supplierName', width: '60%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        // { title: '联系人', dataIndex: 'subareaName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        // { title: '联系手机', dataIndex: 'subareaAreaName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '8%', align: 'center' }
      ],
      openChooseSupplier: false, // 选择供应商弹窗
      // 查询参数
      queryParam: {
        supplierName: ''// 供应商名称
      },
      dataSource: [], // 列表数据
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        this.queryParam.userSn = this.$route.query.sn
        this.queryParam.bizUserSn = this.bizUserSn
        // 获取列表数据  有分页
        return querySupplierScopePage(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.dataSource = data.list
          this.disabled = false
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 打开供应商弹窗
    async openSupplierModal () {
      const _this = this
      const arr = []
      const dataList = await querySupplierScope({ userSn: this.$route.query.sn })
      if (dataList.data && dataList.data.length > 0) {
        dataList.data.forEach(con => {
          arr.push(con.bizSn)
        })
        _this.chooseInfo = [...new Set(arr)]
      } else {
        _this.chooseInfo = []
      }
      const dataInfo = {
        list: _this.chooseInfo,
        sn: _this.$route.query.sn
      }
      this.openChooseSupplier = true
      this.$nextTick(() => {
        _this.$refs.supplierChoose.pageInit(dataInfo)
      })
    },
    // 选择供应商
    handleAddSupplier (list) {
      const newData = []
      list.forEach(con => {
        const obj = {}
        obj.userSn = this.$route.query.sn
        obj.bizUserSn = this.bizUserSn
        obj.bizType = 'supplier'
        obj.bizSn = con
        newData.push(obj)
      })
      this.saveChooseDealer(newData)
    },
    // 保存所选供应商
    saveChooseDealer (list) {
      const _this = this
      const ajaxData = {}
      ajaxData.userSn = _this.$route.query.sn
      ajaxData.bizUserScopeList = list
      saveSupplierList(ajaxData).then(res => {
        if (res.status == 200) {
          _this.openChooseSupplier = false
          _this.resetSearchForm()
        }
      })
    },
    // 删除供应商
    handleDel (row) {
      const _this = this
      this.$confirm({
        title: '提示',
        content: '确认要删除吗？删除后当前用户权限以最新权限为准',
        centered: true,
        onOk () {
          bizuserScopeDelete({ id: row.bizUserScopeSn }).then(res => {
            if (res.status == 200) {
              _this.resetSearchForm()
            }
          })
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.queryParam = {
        supplierName: ''
      }
      this.$refs.table.refresh(true)
    },
    // 初始化
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.$refs.table.refresh()
    },
    // 计算表格高度
    setTableH () {
      this.tableHeight = window.innerHeight - 345
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .supplierSet-wrap{

  }
</style>
