<template>
  <div class="addWrap">
    <div class="d2-container-content" style="padding: 10px 20px">
      <el-form :inline="true" :model="editAllocationForm" ref="editAllocationFormRef" :rules="formRules" class="demo-form-inline">
        <el-form-item label="调出门店:" prop="callOutYqMerchantId">
          <el-select v-model="editAllocationForm.callOutYqMerchantId" style="width: 300px">
            <el-option v-for="item in callOutYqMerchantList" :key="item.merchantNo" :label="item.merchantName" :value="item.merchantNo" />
          </el-select>
        </el-form-item>
        <el-form-item label="调入门店:" prop="callInYqMerchantId">
          <el-select v-model="editAllocationForm.callInYqMerchantId" filterable style="width: 300px">
            <el-option v-for="item in callInYqMerchantList" :key="item.merchantId" :label="item.merchantName" :value="item.merchantId" />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="d2-container-content">
      <div style="padding: 15px 0">
        <div>
          <el-input v-model="searchForm.searchName" style="width: 200px" placeholder="请输入商品名称/系列" maxlength="20" clearable />
          <el-button @click="handleSearch" size="small" type="primary" style="marginleft: 15px">查询</el-button>
        </div>
      </div>
      <GLTable ref="allocationTable" :getListApi="getListApi" :searchForm="searchFormSearch" :tableParams="tableParams" :beforeSearchFn="beforeSearchFn">
        <div slot="auth-button" slot-scope="scope">
          <el-button type="text" @click="allocation(scope.scope)" :disabled="setDisabled(scope.scope)">盘点</el-button>
        </div>
      </GLTable>
    </div>
    <div class="d2-container-content">
      <p class="title">调拨确认单</p>
      <GLTable ref="allocationTable2" :tableData="chooseDataList" :tableParams="tableParams2" :hasPage="false">
        <div slot="auth-button" slot-scope="scope">
          <el-button type="text" @click="cancelAllocation(scope.row, scope.$index)">取消</el-button>
        </div>
      </GLTable>
    </div>
    <glsx-form ref="allocationForm" :origin-data="editData" :form-list="editForm" form-ref="inveFormRef" @handleAddConfirm="handleAddConfirm" @handleClose="handleAddClose"></glsx-form>
    <!-- 盘点 -->
    <allocation-num-dialog :allocation-num-visible="allocationNumVisible" :allocation-info="allocationInfo" v-if="allocationNumVisible" @dialogClose="dialogClose" />
  </div>
</template>

<script>
import allocationNumDialog from './allocationNumDialog'
import { membraneTypeList } from '@/utils/constants.js'
export default {
  name: 'allocationList',
  components: {
    allocationNumDialog
  },
  data() {
    return {
      callOutYqMerchantList: [],
      membraneTypeList: membraneTypeList,
      callInYqMerchantList: [],
      searchForm: {
        searchName: '',
        stockFlag: true
      },
      searchFormSearch: {},
      searchSelectObj: {},
      showFormData: [
        {
          // label: '商品名称/系列',
          placeholder: '请输入商品名称/系列',
          name: 'searchName',
          type: 'input',
          maxlength: '20',
          isClear: true
        }
      ],
      getListApi: this.$apis.stockManage.stockRemainPage,
      tableParams: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableOption: true,
        tableColumns: [
          {
            label: '商品名称',
            prop: 'goodsName',
            width: '160px'
          },
          {
            label: '系列',
            prop: 'sourceMaterialName',
            width: '250px'
          },
          {
            label: '所属分类',
            prop: 'goodsTypeName',
            width: '250px'
          },
          {
            label: '库存',
            prop: 'stock',
            formatter: cellValue => {
              return cellValue.stock || cellValue.stock == 0 ? parseFloat(cellValue.stock).toFixed(2) + (this.membraneTypeList.includes(cellValue.goodsType) ? '米' : '件') : '-'
            }
          },
          {
            label: '所属门店',
            prop: 'yqMerchantName',
            width: '200px'
          }
        ]
      },
      tableParams2: {
        btnId: new Date().getTime() + parseInt(Math.random() * 1000),
        listName: 'list',
        tableOption: true,
        tableColumns: [
          {
            label: '商品名称',
            prop: 'goodsName',
            width: '160px'
          },
          {
            label: '系列',
            prop: 'sourceMaterialName',
            width: '250px'
          },
          {
            label: '所属分类',
            prop: 'goodsTypeName',
            width: '250px'
          },
          {
            label: '库存',
            prop: 'stock',
            formatter: cellValue => {
              return cellValue.stock || cellValue.stock == 0 ? parseFloat(cellValue.stock).toFixed(2) + (this.membraneTypeList.includes(cellValue.goodsType) ? '米' : '件') : '-'
            }
          },
          {
            label: '调拨库存',
            prop: 'allocateNumber ',
            formatter: cellValue => {
              return cellValue.allocateNumber || cellValue.allocateNumber == 0 ? parseFloat(cellValue.allocateNumber).toFixed(2) + (this.membraneTypeList.includes(cellValue.goodsType) ? '米' : '件') : '-'
            }
          }
        ]
      },
    

      editData: {
        // thumbUrlList: [],
      },
      editForm: [
        {
          formBlock: [
            {
              name: 'remark',
              label: '备注说明',
              type: 'textarea',
              maxlength: 100
            }
          ]
        }
      ],

      editAllocationForm: {
        callOutYqMerchantId: '',
        callInYqMerchantId: ''
      },

      allocationNumVisible: false,
      allocationInfo: null,
      chooseDataList: [],
      formRules: {
        callOutYqMerchantId: [
          {
            required: true,
            message: '请选择调出门店',
            trigger: 'change'
          }
        ],
        callInYqMerchantId: [
          {
            required: true,
            message: '请选择调入门店',
            trigger: 'change'
          }
        ]
      }
    }
  },
  mounted() {
    // this.editAllocationForm.callOutYqMerchantId =
    //   this.callOutYqMerchantList[0].merchantNo
    //调入门店
    this.$apis.stockManage.yqRelatedList({ includeSelf: false }).then((res) => {
      this.callInYqMerchantList = res
    })
  },

  methods: {
    beforeSearchFn() {
      return new Promise((resolve) => {
        this.searchFormSearch = this.$common.utils.deepClone(this.searchForm)
        resolve()
      })
    },
    handleSearch() {
      this.$refs.allocationTable.refreshTable()
    },

    // 调拨
    allocation(row) {
      this.allocationInfo = row
      this.allocationNumVisible = true
    },

    //设置禁选
    setDisabled(row) {
      const index = this.chooseDataList.findIndex((item) => item.id == row.id)
      if (index == -1) {
        return false
      } else {
        return true
      }
    },

    dialogClose(chooseData) {
      this.allocationNumVisible = !this.allocationNumVisible
      console.log(chooseData)
      if (chooseData) {
        this.chooseDataList.push(chooseData)
      }
    },

    // 取消
    cancelAllocation(row, index) {
      this.chooseDataList.splice(index, 1)
    },

    handleAddConfirm(data) {
      this.$refs.editAllocationFormRef.validate((valid) => {
        if (valid) {
          if (this.chooseDataList.length == 0) {
            this.notice.warningTips("请选择调拨商品")
            return
          }
          var params = {}
          params.stockCheckDetailList = this.chooseDataList
          if (data.remark) {
            params.remark = data.remark
          }
          params = {
            ...params,
            ...this.editAllocationForm
          }
          this.$confirm(
            "调拨单保存后,系统自动按照调拨数量对调出和调入门店进行库存更新;是否确定保存?",
            "提示",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }
          )
            .then(() => {
              this.$apis.stockManage.saveStockAllocate(params).then((res) => {
                this.notice.okTips()
                this.handleAddClose()
              })
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },

    handleAddClose(isRefresh = false) {
    //   this.close({ tagName: "/addAllocation" })
    //   this.$router.push({ name: "allocationList" })
    }
  }
}
</script>
<style scoped>
.d2-container-content {
  background: #fff;
  padding: 20px;
  margin-bottom: 20px;
}
.count-txt {
  font-weight: 700;
  color: #2a58ad;
  margin: 0 2px;
}
.title {
  font-weight: 600;
  font-size: 16px;
  color: #333333;
  padding: 17px 0;
}
.leftSpan {
  display: inline-block;
  box-sizing: border-box;
  margin-right: 10px;
}
</style>