<template>
  <div>
    <a-card size="small" :bordered="false" class="tireStorageManagement-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="轮胎省仓名称">
                <dealerSubareaScopeList ref="provinceDealerList" placeholder="请输入轮胎省仓名称" id="tireStorageManagement-provinceDealerName" @change="custProvinceChange" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="轮胎省仓状态">
                <v-select code="ENABLE_FLAG" id="tireStorageManagement-status" v-model="queryParam.status" 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="tireStorageManagement-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="tireStorageManagement-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="tireStorageManagement-add" type="primary" @click="openAddModal = true">新增</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="tireStatus" slot-scope="text, record">
            <a-switch
              id="tireStorageManagement-enableFlag"
              v-if="$hasPermissions('B_serviceCenter_enable')"
              checkedChildren="启用"
              unCheckedChildren="禁用"
              @change="openStatusModal(record)"
              :checked="record.status == 1 ? true : false" />
            <span v-else :style="{color:(record.status==1?'#00aa00':'#999')}"> {{ record.status==1? '已启用': '已禁用' }} </span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <a-button
              size="small"
              type="link"
              v-if="$hasPermissions('B_serviceCenter_dealer')"
              class="button-info"
              @click="handleChangeRecord(record)"
              id="provincialWarehouse-edit-btn">变更记录</a-button>
            <span v-else>--</span>
          </template>
        </s-table>
      </a-spin>
    </a-card>
    <!-- 新增/编辑 -->
    <add-dealer-modal v-drag :openModal="openAddModal" @ok="$refs.table.refresh(true)" @close="openAddModal = false" />
    <!-- 变更记录 -->
    <change-record-list ref="changeRecord" v-drag :openModal="openRecordModal" @close="openRecordModal=false"></change-record-list>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { STable, VSelect } from '@/components'
import addDealerModal from './addDealerModal.vue'
import changeRecordList from './changeRecordList.vue'
import dealerSubareaScopeList from '@/views/common/dealerSubareaScopeList.vue'
// 接口
import { tireStorageList, tireStorageEnable } from '@/api/provinceServiceCenter'
export default {
  name: 'TireStorageManagement',
  mixins: [commonMixin],
  components: { STable, VSelect, addDealerModal, changeRecordList, dealerSubareaScopeList },
  data () {
    return {
      spinning: false,
      tableHeight: 0, // 列表高度
      disabled: false, //  查询、重置按钮是否可操作
      openAddModal: false, //  新增 弹框
      openRecordModal: false, // 变更记录 弹窗
      //  查询条件
      queryParam: {
        dealerEntity: {
          dealerName: undefined// 轮胎省仓名称
        },
        dealerSn: undefined, // 轮胎省仓sn
        status: undefined// 轮胎省仓状态
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '4%', align: 'center' },
        { title: '创建时间', dataIndex: 'createDate', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '区域', dataIndex: 'subareaArea.subareaName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '分区', dataIndex: 'subareaArea.subareaAreaName', width: '10%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '轮胎省仓名称', dataIndex: 'dealerEntity.dealerName', width: '18%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '轮胎省仓状态', scopedSlots: { customRender: 'tireStatus' }, width: '9%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '11%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return tireStorageList(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
            }
            this.disabled = false
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 变更记录
    handleChangeRecord (row) {
      this.$refs.changeRecord.pageInit({ dealerSn: row.dealerSn, dealerName: row.dealerEntity.dealerName })
      this.openRecordModal = true
    },
    // 轮胎省仓客户名称 change
    custProvinceChange (val) {
      this.queryParam.dealerEntity.dealerName = val.name
      this.queryParam.dealerSn = val.key
    },
    //  重置
    resetSearchForm () {
      this.queryParam.dealerEntity.dealerName = undefined
      this.queryParam.dealerSn = undefined
      this.queryParam.status = undefined
      this.$refs.provinceDealerList.resetForm()
      this.$refs.table.refresh(true)
    },
    // 打开启用 禁用 弹窗
    openStatusModal (record) {
      const _this = this
      if (record.status == 1) {
        this.$confirm({
          title: '提示',
          content: '禁用后该经销商不是轮胎省仓，确定禁用轮胎省仓吗？',
          centered: true,
          onOk () {
            const ajaxData = {
              serviceCenterSn: record.serviceCenterSn,
              status: '0'
            }
            _this.changeStatus(ajaxData)
          }
        })
      } else {
        const params = {
          serviceCenterSn: record.serviceCenterSn,
          status: '1'
        }
        _this.changeStatus(params)
      }
    },
    // 启用 禁用
    changeStatus (params) {
      this.spinning = true
      tireStorageEnable(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>
