<template>
  <div>
    <a-card size="small" :bordered="false" class="provincialWarehouse-wrap searchBoxNormal">
      <!-- 搜索条件 -->
      <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="provincialWarehouse-name" v-model.trim="queryParam.serviceCenterName" allowClear placeholder="请输入省仓名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="省仓客户">
                <a-input id="provincialWarehouse-dealerName" v-model.trim="queryParam.dealerName" allowClear placeholder="请输入省仓客户"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="省仓状态">
                <v-select code="ENABLE_FLAG" id="provincialWarehouse-enableFlag" v-model="queryParam.enableFlag" 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="provincialWarehouse-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="provincialWarehouse-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card size="small" :bordered="false">
      <a-spin :spinning="spinning" tip="Loading...">
        <!-- 操作按钮 -->
        <div class="table-operator" v-if="$hasPermissions('B_serviceCenter_add')">
          <a-button id="provincialWarehouse-add" type="primary" @click="handleEdit()">新增</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="customNum" slot-scope="text, record">
            {{ !queryParam.dealerName?'共':'' }}有<span style="font-weight:bold;font-size:13px;vertical-align:top;">{{ record.customerNum }}</span>个客户
          </template>
          <!-- 省仓状态 -->
          <template slot="loginFlag" slot-scope="text, record">
            <a-switch
              id="provincialWarehouse-enableFlag"
              v-if="$hasPermissions('B_serviceCenter_enable')"
              checkedChildren="启用"
              unCheckedChildren="禁用"
              @change="openStatusModal(record)"
              :checked="record.enableFlag == 1 ? true : false" />
            <span v-else :style="{color:(record.enableFlag==1?'#00aa00':'#999')}"> {{ record.enableFlag==1? '已启用': '已禁用' }} </span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record" v-if="">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_serviceCenter_dealer')"
              class="button-info"
              @click="handleDealer(record)"
              id="provincialWarehouse-edit-btn">关联经销商</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
      <!-- 新增/编辑 -->
      <provincial-warehouse-add-modal v-drag :openModal="openModal" @ok="$refs.table.refresh(true)" @close="closeModal" />
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import provincialWarehouseAddModal from './addModal.vue'
import { serviceCenterList, provincialWarehouseEnable, serviceCenterBinding } from '@/api/serviceCenter'
export default {
  name: 'DealerAccountManagementList',
  mixins: [commonMixin],
  components: { STable, VSelect, provincialWarehouseAddModal },
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      queryParam: { //  查询条件
        serviceCenterName: '',
        dealerName: '',
        enableFlag: undefined
      },
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center' },
        { 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: '省仓名称', dataIndex: 'serviceCenterName', width: '18%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '经销商名称', dataIndex: 'dealerName', width: '18%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '省仓客户', dataIndex: 'customerNum', width: '11%', align: 'center', scopedSlots: { customRender: 'customNum' } },
        { title: '省仓状态', scopedSlots: { customRender: 'loginFlag' }, width: '9%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '11%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return serviceCenterList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            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 //  新增编辑  弹框
    }
  },
  methods: {
    // 关联经销商
    handleDealer (row) {
      this.$router.push({ name: 'relatedDealersList', params: { sn: row.serviceCenterSn, name: row.serviceCenterName } })
    },
    //  重置
    resetSearchForm () {
      this.queryParam.serviceCenterName = ''
      this.queryParam.dealerName = ''
      this.queryParam.enableFlag = undefined
      this.notDealerSn = null
      this.$refs.table.refresh(true)
    },
    //  新增/编辑
    handleEdit (row) {
      this.openModal = true
    },
    //  关闭弹框
    closeModal () {
      this.openModal = false
    },
    // 打开启用 禁用 弹窗
    openStatusModal (record) {
      const _this = this
      if (record.enableFlag == 1) {
        this.$confirm({
          title: '提示',
          content: '禁用省仓后，省仓客户无法向省仓采购，确定禁用省仓吗？',
          centered: true,
          onOk () {
            const ajaxData = {
              serviceCenterSn: record.serviceCenterSn,
              enableFlag: '0'
            }
            _this.changeStatus(ajaxData)
          }
        })
      } else {
        const params = {
          serviceCenterSn: record.serviceCenterSn,
          enableFlag: '1'
        }
        _this.changeStatus(params)
      }
    },
    // 启用 禁用
    changeStatus (params) {
      this.spinning = true
      provincialWarehouseEnable(params).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        }
        this.spinning = false
        this.$refs.table.refresh()
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    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()
      this.resetSearchForm()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.$refs.table.refresh()
      this.pageInit()
    }
  }
}
</script>
