<template>
  <div>
    <a-card size="small" :bordered="false" class="shelfNoMan-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="4" :sm="24">
              <a-form-item label="产品编码">
                <a-input id="shelfNoMan-code" v-model.trim="queryParam.product.code" allowClear placeholder="请输入产品编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="产品名称">
                <a-input id="shelfNoMan-name" v-model.trim="queryParam.product.name" allowClear placeholder="请输入产品名称"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="原厂编码">
                <a-input id="shelfNoMan-origCode" v-model.trim="queryParam.product.origCode" allowClear placeholder="请输入原厂编码"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="产品品牌">
                <ProductBrand id="shelfNoMan-productBrandSn" v-model="queryParam.product.productBrandSn"></ProductBrand>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="4" :sm="24">
                <a-form-item label="产品分类">
                  <productTypeAll placeholder="请选择产品分类" @change="changeProductType" v-model="productType" id="shelfNoMan-productType"></productTypeAll>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24" v-show="isShowWarehouse">
                <a-form-item label="仓库">
                  <warehouse
                    v-model="queryParam.warehouseSn"
                    isPermission
                    id="shelfNoMan-warehouseSn"
                    placeholder="请选择仓库"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="产品状态">
                  <v-select code="PRODUCT_STATUS" id="shelfNoMan-state" v-model="queryParam.product.state" allowClear placeholder="请选择产品状态"></v-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="货位编号">
                  <a-input id="shelfNoMan-stackPlaceCode" v-model.trim="queryParam.stackPlaceCode" allowClear placeholder="请输入货位编号"/>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="是否有货位编号">
                  <a-select id="shelfNoMan-haveStackCode" v-model="queryParam.haveStackCode" allowClear placeholder="请选择">
                    <a-select-option value="1">
                      有编号
                    </a-select-option>
                    <a-select-option value="0">
                      无编号
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="6" :sm="24" style="margin-bottom: 10px;">
              <a-button type="primary" @click="$refs.table.refresh(true)" :disabled="disabled" id="shelfNoMan-refresh">查询</a-button>
              <a-button style="margin-left: 5px" @click="resetSearchForm" :disabled="disabled" id="shelfNoMan-reset">重置</a-button>
              <a-button
                v-if="$hasPermissions('B_shelfNoManage_export')"
                style="margin-left: 5px"
                type="primary"
                class="button-warning"
                @click="handleExport"
                :disabled="disabled"
                :loading="exportLoading"
                id="shelfNoMan-export">导出</a-button>
              <a @click="advanced=!advanced" style="margin-left: 5px">
                {{ advanced ? '收起' : '展开' }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </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">
          <a-button type="primary" @click="openNewModal" id="shelfNoMan-add">新增</a-button>
          <a-button
            id="shelfNoMan-batchAudit"
            v-if="$hasPermissions('B_shelfNoManage_import')"
            type="primary"
            ghost
            @click="openGuideModal = 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="productType" slot-scope="text, record">
            <span v-if="record.product.productTypeName2 || record.product.productTypeName3">{{ record.product.productTypeName2 }} {{ record.product.productTypeName3 ? '>' : '' }} {{ record.product.productTypeName3 }}</span>
            <span v-else>--</span>
          </template>
          <!-- 操作 -->
          <template slot="action" slot-scope="text, record">
            <div>
              <a-button
                size="small"
                type="link"
                v-if="$hasPermissions('B_shelfNoManage_edit')"
                class="button-info"
                @click="handleEdit(record)"
                :id="'shelfNoMan-edit-btn'+record.id">编辑</a-button>
              <a-button
                size="small"
                type="link"
                v-if="$hasPermissions('B_shelfNoManage_detail')"
                class="button-info"
                @click="handleDetail(record)"
                :id="'shelfNoMan-detail-btn'+record.id">变更记录</a-button>
            </div>
          </template>
        </s-table>
      </a-spin>
      <!-- 导入产品 -->
      <importGuideModal :openModal="openGuideModal" @close="openGuideModal=false" @ok="hanldeOk" />
      <!-- 编辑 -->
      <editHwModal v-drag :openModal="openModal" :nowData="nowData" @ok="editOk" @close="closeModal" />
      <!-- 变更记录 -->
      <changeRecordModal v-drag :openModal="recordModal" :detailData="nowData" @close="closeModal"></changeRecordModal>
    </a-card>
  </div>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { STable, VSelect } from '@/components'
import editHwModal from './editHwModal.vue'
import ProductBrand from '@/views/common/productBrand.js'
import productTypeAll from '@/views/common/productTypeAll.js'
import { exportExcel } from '@/libs/JGPrint.js'
import ImportGuideModal from './importGuideModal.vue'
import changeRecordModal from './changeRecordModal.vue'
import warehouse from '@/views/common/chooseWarehouse.js'
import { queryStackPlaceQueryPage, stackPlaceExport, stackPlaceInsertImport } from '@/api/product'
export default {
  name: 'ProductInfoList',
  mixins: [commonMixin],
  components: { STable, VSelect, editHwModal, ProductBrand, productTypeAll, ImportGuideModal, changeRecordModal, warehouse },
  data () {
    return {
      spinning: false,
      openGuideModal: false,
      advanced: true, // 高级搜索 展开/关闭
      tableHeight: 0,
      queryParam: { //  查询条件
        stackPlaceCode: undefined, // 货位编号
        haveStackCode: undefined,
        product: {
          name: '', //  产品名称
          code: '', // 产品编码
          origCode: '', // 原厂编码
          productBrandSn: undefined, //  产品品牌
          productTypeSn1: '', //  产品一级分类
          productTypeSn2: '', //  产品二级分类
          productTypeSn3: '', //  产品三级分类
          state: undefined //  产品状态
        },
        warehouseSn: undefined // 仓库
      },
      productType: [],
      disabled: false, //  查询、重置按钮是否可操作
      exportLoading: false,
      loadingAudit: false,
      loadingLaunch: false,
      loadingDownline: false,
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        this.disabled = true
        this.spinning = true
        return queryStackPlaceQueryPage(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, //  编辑弹框
      recordModal: false, // 变更记录弹框
      nowData: null, //  当前sn
      pageFromInfo: null
    }
  },
  computed: {
	  columns () {
	    const arr = [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '产品编码', dataIndex: 'product.code', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品名称', dataIndex: 'product.name', align: 'left', width: '20%', ellipsis: true, customRender: function (text) { return text || '--' } },
        { title: '原厂编码', dataIndex: 'product.origCode', width: '10%', align: 'left', customRender: function (text) { return text || '--' } },
        { title: '产品品牌', dataIndex: 'product.productBrandName', width: '9%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '产品分类', scopedSlots: { customRender: 'productType' }, width: '12%', align: 'left' },
        { title: '产品状态', dataIndex: 'product.stateDictValue', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '仓库', dataIndex: 'warehouseName', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '货位编号', dataIndex: 'stackPlaceCode', width: '8%', align: 'center', customRender: function (text) { return text || '--' } },
        { title: '操作', scopedSlots: { customRender: 'action' }, width: '10%', align: 'center' }
      ]
	    return arr
	  }
  },
  methods: {
    // 导入产品
    hanldeOk (obj) {
      stackPlaceInsertImport(obj).then(res => {
        if (res.status == 200) {
          this.resetSearchForm()
        }
      })
    },
    //  重置
    resetSearchForm () {
      this.queryParam = {
        stackPlaceCode: undefined, // 货位编号
        haveStackCode: undefined,
        warehouseSn: undefined, // 仓库
        product: {
          name: '', //  产品名称
          code: '', // 产品编码
          origCode: '', // 原厂编码
          productBrandSn: undefined, //  产品品牌
          productTypeSn1: '', //  产品一级分类
          productTypeSn2: '', //  产品二级分类
          productTypeSn3: '', //  产品三级分类
          state: undefined //  产品状态
        }
      }
      this.productType = []
      this.$refs.table.refresh(true)
    },
    // 新增
    openNewModal () {
      this.nowData = null
      this.openModal = true
    },
    // 编辑
    handleEdit (row) {
      this.nowData = row
      this.openModal = true
    },
    // 编辑成功
    editOk () {
      this.closeModal()
      this.$refs.table.refresh()
    },
    //  关闭编辑弹框
    closeModal () {
      this.nowData = null
      this.openModal = false
      this.recordModal = false
    },
    // 变更记录
    handleDetail (row) {
      this.nowData = row
      this.recordModal = true
    },
    //  导出
    handleExport () {
      const _this = this
      const params = this.queryParam
      this.spinning = true
      exportExcel(stackPlaceExport, params, '货位编号导出', function () {
        _this.spinning = false
      })
    },
    //  产品分类  change
    changeProductType (val, opt) {
      this.queryParam.product.productTypeSn1 = val[0] ? val[0] : ''
      this.queryParam.product.productTypeSn2 = val[1] ? val[1] : ''
      this.queryParam.product.productTypeSn3 = val[2] ? val[2] : ''
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 240
    }
  },
  watch: {
    advanced (newValue, oldValue) {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
    },
    '$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()
    }
  },
  beforeRouteEnter (to, from, next) {
    next(vm => {
      vm.pageFromInfo = from
    })
  }
}
</script>
