<template>
  <div class="categorySet-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-card size="small" :bordered="false" class="categorySet-cont">
        <div class="areaSet-wrap">
          <div class="table-page-search-wrapper newTableSearchName" >
            <div class="areaSet-btnGroup" style="margin-bottom:20px;">
              <a-button type="primary" class="button-error" @click="openSupplierModal">新增</a-button>
            </div>
            <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="areaSet-dealerName" v-model.trim="queryParam.supplierName" 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="areaSet-refresh">查询</a-button>
                  <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="areaSet-reset">重置</a-button>
                </a-col>
              </a-row>
            </a-form>
          </div>
          <a-spin :spinning="spinning" tip="Loading...">
            <!-- 列表 -->
            <s-table
              class="sTable fixPagination"
              ref="table"
              size="small"
              :rowKey="(record) => record.no"
              rowKeyName="no"
              :columns="columns"
              :data="loadData"
              :defaultLoadData="false"
              :style="{ height: tableHeight+'px' }"
              :scroll="{ y: tableHeight-180 }"
              bordered>
              <!-- 操作 -->
              <template slot="action" slot-scope="text, record">
                <a-button
                  size="small"
                  type="link"
                  class="button-error"
                  @click="handleDel(record)"
                >
                  删除
                </a-button>
              </template>
            </s-table>
          </a-spin>
        </div>
      </a-card>
    </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 chooseSupplier from './chooseSupplier.vue'
import ChooseTypeModal from '@/views/common/chooseTypeModal.vue'
import { STable } from '@/components'
import { querySupplierScopePage, querySupplierScope, saveSupplierList, bizuserScopeDelete } from '@/api/bizuser'
export default {
  name: 'CategorySet',
  mixins: [commonMixin],
  components: { chooseSupplier, ChooseTypeModal, 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: [],
      openModal: false, //  新增编辑产品品牌  弹框
      itemSn: '', //  当前sn
      nowData: null, //  当前记录数据
      openBrandModal: false,
      openChooseDealer: true,
      openTypeModal: false,
      chooseBrand: [],
      chooseType: [],
      chooseObj: null, // 选择品牌 内容
      // 加载数据方法 必须为 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 - 385
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {})
  }
}
</script>

<style lang="less">
  .categorySet-wrap{
    .table-operator{
    text-align:right;
    }
  }
</style>
