<template>
  <a-card size="small" :bordered="false">
    <a-spin :spinning="spinning" tip="Loading...">
      <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="goodsShelvesAdministrationList-shelfName" allowClear v-model.trim="queryParam.shelfName" placeholder="请输入货架名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="配件经销商">
                <dealerList ref="dealerList" @change="dealerChange" id="goodsShelvesAdministrationList-dealerChange"></dealerList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="汽车修理厂">
                <storeList ref="storeList" @change="storeChange" id="goodsShelvesAdministrationList-storeChange"></storeList>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-item label="货架状态">
                  <v-select id="goodsShelvesAdministrationList-state" code="SHELF_STATE" v-model="queryParam.state" allowClear placeholder="请选择状态"></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="结算状态">
                  <v-select id="goodsShelvesAdministrationList-settleType" code="SHELF_SETTLE_TYPE" v-model="queryParam.settleType" allowClear placeholder="请选择状态"></v-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="4" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" id="goodsShelvesAdministrationList-search">查询</a-button>
              <a-button type="" @click="reset" id="goodsShelvesAdministrationList-reset" style="margin-left: 10px;">重置</a-button>
              <a @click="advanced=!advanced" style="margin-left: 8px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <div class="table-operator">
        <a-button type="primary" class="button-error" @click="handleEdit()" id="goodsShelvesAdministrationList-handleEdit">新增数字货架</a-button>
      </div>
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px', wordBreak: 'break-all' }"
        size="small"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <template slot="state" slot-scope="text, record">
          <div @click="changeFlagHandle(record)" style="cursor:pointer;" id="goodsShelvesAdministrationList-changeFlagHandle">{{ record.stateDictValue }}<a-icon v-if="record.state!='CLOSE'" type="edit" title="编辑" :style="{color:' #1890FF',paddingLeft: '5px'}"/></div>
        </template>
        <template slot="action" slot-scope="text, record">
          <a-button size="small" type="link" class="button-info" @click="handleEdit(record)" id="goodsShelvesAdministrationList-handleEdit">编辑</a-button>
          <a-button size="small" type="link" class="button-info" @click="handleSet(record)" id="goodsShelvesAdministrationList-handleSet">货位设置</a-button>
          <a-button size="small" type="link" class="button-info" @click="handleSettlement(record)" id="goodsShelvesAdministrationList-handleSettlement">结算设置</a-button>
          <!-- <span v-if="!($hasPermissions('B_powerMD_user_edit') && record.superAdmin!=1) && !(record.loginFlag==1 && record.ownerOrgFlag==1 && $hasPermissions('B_powerMD_user_restPwd')) && !(record.loginFlag==0 && record.ownerOrgFlag==1 && $hasPermissions('B_powerMD_user_del'))">--</span> -->
        </template>
      </s-table>
    </a-spin>
    <!--新增/编辑数字货架 -->
    <addGoodsShelve :visible="showModal" @refresh="$refs.table.refresh(true)" :nowData="itemData" @close="handleUserCancel"></addGoodsShelve>
    <!-- 货架状态更改 -->
    <changeStatusModal :openStatusModal="openStatusModal" :nowData="itemData" @refresh="$refs.table.refresh()" @close="openStatusModal=false"></changeStatusModal>
  </a-card>
</template>

<script>
import { STable, VSelect } from '@/components'
import addGoodsShelve from './addGoodsShelve.vue'
import changeStatusModal from './changeStatusModal.vue'
import { getShelfList } from '@/api/shelf.js'
import dealerList from '@/views/common/dealerList.vue'
import storeList from '@/views/common/storeList.vue'
export default {
  name: 'UserList',
  components: { STable, VSelect, addGoodsShelve, changeStatusModal, storeList, dealerList },
  data () {
    return {
      spinning: false,
      advanced: false,
      tableHeight: 0,
      // 查询参数
      queryParam: {
        dealerSn: undefined, // 配件经销商sn
        shelfName: '', // 货架名称
        settleType: '', // 结算状态
        storeSn: undefined, // 汽车修理厂
        state: undefined// 货架状态
      },
      showModal: false,
      itemData: null, // 编辑行数据
      openStatusModal: false, // 货架状态弹窗初始状态
      // 表头
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '货架名称', dataIndex: 'shelfName', width: '17%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '配件经销商', dataIndex: 'dealerName', width: '17%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '汽车修理厂', dataIndex: 'storeName', width: '17%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '货位数量', dataIndex: 'shelfPlaceNum', width: '7%', align: 'center', customRender: function (text) { return (text || text == 0) ? text : '--' } },
        { title: '结算方式', dataIndex: 'settleTypeDictValue', width: '7%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: <div>货架状态&nbsp;<a-tooltip placement='top' title='货架状态包括：未启用、已启用、已暂停、已关闭。货架状态只能按照这个过程向前流转，不能后退'><a-icon type="question-circle" /></a-tooltip></div>, width: '7%', align: 'center', scopedSlots: { customRender: 'state' } },
        { title: '创建时间', dataIndex: 'createDate', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', width: '15%', align: 'left', scopedSlots: { customRender: 'action' } }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.spinning = true
        return getShelfList(Object.assign(parameter, this.queryParam)).then(res => {
          let data
          if (res.status == 200) {
            data = res.data
            const no = (data.pageNo - 1) * data.pageSize
            for (let i = 0; i < data.list.length; i++) {
              const _item = data.list[i]
              _item.no = no + i + 1
            }
          }
          this.spinning = false
          return data
        })
      }
    }
  },
  methods: {
    // 经销商 change
    dealerChange (obj) {
      this.queryParam.dealerSn = obj.key || undefined
    },
    // 汽车修理厂 change
    storeChange (obj) {
      this.queryParam.storeSn = obj.key || undefined
    },
    // 新增
    openModal () {
      this.itemData = null
      this.showModal = true
    },
    // 关闭弹框
    handleUserCancel () {
      this.showModal = false
      this.itemData = null
    },
    // 重置
    reset () {
      this.queryParam = {
        dealerSn: undefined, // 配件经销商sn
        shelfName: '', // 货架名称
        settleType: '', // 结算状态
        storeSn: undefined, // 汽车修理厂
        state: undefined// 货架状态
      }
      this.$refs.dealerList.resetForm()
      this.$refs.storeList.resetForm()
      this.$refs.table.refresh(true)
    },
    // 编辑
    handleEdit (row) {
      console.log(row, 'row')
      this.showModal = true
      if (row) { //  编辑
        this.itemData = row
      } else {
        this.itemData = null
      }
    },
    // 货位设置
    handleSet (row) {
      this.$router.push({ name: 'goodsAllocationSet', params: { shelfSn: row.shelfSn } })
    },
    // 结算设置
    handleSettlement (row) {
      this.$router.push({ name: 'settlementSet', params: { shelfSn: row.shelfSn } })
    },
    // 修改状态
    changeFlagHandle (row) {
      this.itemData = row
      if (row.state != 'CLOSE') {
        this.openStatusModal = true
      }
    },
    pageInit () {
      const _this = this
      _this.$refs.table.refresh()
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 215
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    this.pageInit()
    this.reset()
    // if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
    //   this.pageInit()
    //   this.reset()
    // }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      if (from.name == 'goodsAllocationSet' || from.name == 'settlementSet') {
        vm.$refs.table.refresh()
      } else {
        vm.reset()
      }
    })
    // next(vm => {
    //   vm.$refs.table.refresh()
    // })
  }
  // activated () {
  //   // 如果是新页签打开，则重置当前页面
  //   if (this.$store.state.app.isNewTab) {
  //     this.pageInit()
  //     this.resetSearchForm()
  //   }
  //   // 仅刷新列表，不重置页面
  //   if (this.$store.state.app.updateList) {
  //     this.pageInit()
  //     this.$refs.table.refresh()
  //   }
  // }
}
</script>
