<template>
  <a-card size="small" :bordered="false" class="shelfSetList-wrap">
    <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="货架名称">
                <shelfSList v-model="queryParam.shelfSn"></shelfSList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="关联客户">
                <custList ref="custList" :dealerFlag="0" @change="custChange" :isEnabled="true"></custList>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-item label="是否设置完成">
                <v-select
                  v-model="queryParam.finishFlag"
                  ref="finishFlag"
                  id="shelfSetList-finishFlag"
                  code="FLAG"
                  placeholder="请选择"
                  allowClear></v-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="shelfSetList-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="shelfSetList-reset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 提示 -->
      <a-alert type="info" style="margin-bottom: 10px">
        <div slot="message">注意：只有当数字货架的“是否设置完成”为"是"，系统才会自动对该货架生成补货单，修理厂才能正常下单。</div>
      </a-alert>
      <!-- 列表 -->
      <s-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data="loadData"
        :scroll="{ y: tableHeight }"
        :defaultLoadData="false"
        bordered>
        <!-- 货架名称 -->
        <template slot="shelfName" slot-scope="text, record">
           {{record.shelfName}}
           <a-badge count="已注销" :number-style="{ zoom:'80%',marginLeft:'5px', color: '#fff',background:'#999' }" v-if="record.state=='WRITE_OFF'"></a-badge>
        </template>
        <!-- 货位数量 -->
        <template slot="shelfPlaceNum" slot-scope="text, record">
          <p style="margin: 0;">
            <span style="color: red;">
              {{ record.shelfPlaceBindNum||record.shelfPlaceBindNum==0 ? record.shelfPlaceBindNum:'' }}</span> /
            {{ record.shelfPlaceNum }}
          </p>
        </template>
        <!-- 是否设置完成 -->
        <template slot="finishFlag" slot-scope="text, record">
          <a-switch
            checkedChildren="是"
            unCheckedChildren="否"
            v-if="record.finishFlag!=undefined&&record.state!='WRITE_OFF'&&record.state!='SUSPEND'"
            @change="changeStatus(record)"
            :checked="record.finishFlag == 1"
          />
          <span v-else :style="{ color: record.finishFlag == 1 ? '#00aa00' : '#999' }">
            {{ record.finishFlag == 1 ? '是' : (record.finishFlag!=undefined?'否':'--') }}
          </span>
        </template>
        <!-- 启用禁用 -->
        <template slot="enableFlag" slot-scope="text, record">
          <a-switch
            checkedChildren="启用"
            unCheckedChildren="停用"
            v-if="record.state!=undefined&&record.state!='WRITE_OFF'&&record.state!='SUSPEND'"
            @change="enableShelf(record)"
            :checked="record.state == 'ENABLE'"
          />
          <span v-else :style="{ color: record.state == 'ENABLE' ? '#00aa00' : '#999' }">
            {{ record.state == 'ENABLE' ? '启用' : (record.state!=undefined&&record.state!='WRITE_OFF'&&record.state!='SUSPEND'?'停用':'--') }}
          </span>
        </template>
        <!-- 操作 -->
        <template slot="action" slot-scope="text, record">
          <div>
            <a-button size="small" type="link" class="button-primary" v-if="record.state!='SUSPEND'" @click="handleSet(record)">{{record.state!='WRITE_OFF'?'设置':'查看'}}</a-button>
            <a-button size="small" type="link" class="button-primary" v-if="record.state!='WRITE_OFF'&&record.state!='SUSPEND'" @click="handleCancel(record)">注销</a-button>
          </div>
        </template>
      </s-table>
    </a-spin>
    <!-- 基础设置 -->
    <basic-info-modal :type="0" :openModal="openModal" :nowData="nowData" @ok="handleOk" @close="openModal=false" />
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import custList from '@/views/common/custList.vue'
import shelfSList from '@/views/common/shelfList'
import basicInfoModal from './basicInfoModal.vue'
import { shelfList, modifFinishFlag, shelfModifState } from '@/api/shelf'
export default {
  components: { STable, VSelect, custList, shelfSList, basicInfoModal },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      queryParam: {
        shelfSn: undefined,
        customerSn: undefined,
        finishFlag: undefined,
        customerEntity: {
          customerName: undefined
        }
      },
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '货架名称', scopedSlots: { customRender: 'shelfName' }, width: '25%', align: 'left', ellipsis: true },
        { title: '关联客户', dataIndex: 'customerEntity.customerName', width: '25%', align: 'left', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '货位数量', scopedSlots: { customRender: 'shelfPlaceNum' }, width: '15%', align: 'center' },
        { title: '是否设置完成', scopedSlots: { customRender: 'finishFlag' }, width: '10%', align: 'center' },
        { title: '启用状态', scopedSlots: { customRender: 'enableFlag' }, width: '10%', align: 'center' },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return shelfList(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
        })
      },
      openModal: false,
      nowData: null
    }
  },
  methods: {
    // 设置/修改
    handleSet (row) {
      if (row.customerSn) {
        this.$router.push({ path: `/numsGoodsShelves/shelfSet/set/${row.shelfSn}` })
      } else {
        this.nowData = {
          shelfSn: row.shelfSn,
          shelfName: row.shelfName
        }
        this.openModal = true
      }
    },
    handleOk (data) {
      this.$router.push({ path: `/numsGoodsShelves/shelfSet/set/${data.shelfSn}` })
    },
    // 客户 change
    custChange (obj, row) {
      if (row) {
        this.queryParam.customerSn = row && row.customerSn || undefined
        this.queryParam.customerEntity.customerName = undefined
      } else {
        this.queryParam.customerEntity.customerName = obj || undefined
        this.queryParam.customerSn = undefined
      }
    },
    // 是否设置完成
    changeStatus (record) {
      const params = {
        shelfSn: record.shelfSn,
        finishFlag: record.finishFlag == 1 ? '0' : '1'
      }
      this.spinning = true
      modifFinishFlag(params).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.spinning = false
        }
      })
    },
    // 启用禁用
    enableShelf(record){
      const params = {
        shelfSn: record.shelfSn,
        state: record.state == 'ENABLE' ? 'DISABLED' : 'ENABLE'
      }
      const _this = this
      _this.$confirm({
        title: '提示',
        content: <div style="text-align:center;">是否{(record.state == 'ENABLE'?'停用':'启用')+record.shelfName}</div>,
        centered: true,
        closable: true,
        class: 'confirm-center',
        onOk () {
           _this.updateState(params)
        }
      })
    },
    // 注销
    handleCancel(record){
      const params = {
        shelfSn: record.shelfSn,
        state: 'WRITE_OFF'
      }
      const _this = this
      _this.$confirm({
        title: '',
        content: <div style="text-align:center;"><div style="font-size:14px;">是否注销该数字货架，</div><div>点击确定，该数字货架就不可做任何操作，用户不可登陆</div><div style="font-size:12px;color:#999;margin-top:10px;">如需要迁移该数字货架的数据，可去货架设置中导出货架信息</div></div>,
        centered: true,
        closable: true,
        class: 'confirm-center',
        onOk () {
           _this.updateState(params)
        }
      })
    },
    updateState(params){
      this.spinning = true
      shelfModifState(params).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
          this.$refs.table.refresh()
          this.spinning = false
        } else {
          this.spinning = false
        }
      })
    },
    // 重置
    resetSearchForm () {
      this.queryParam.shelfSn = undefined
      this.queryParam.customerSn = undefined
      this.queryParam.customerEntity.customerName = undefined
      this.$refs.custList.resetForm()
      this.queryParam.finishFlag = undefined
      this.$refs.table.refresh(true)
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 235
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
      this.resetSearchForm()
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
      this.resetSearchForm()
    }
    // 仅刷新列表，不重置页面
    if (this.$store.state.app.updateList) {
      this.pageInit()
      this.$refs.table.refresh()
    }
  }
}
</script>
