<template>
  <div>
<!--    <SearchBox ref="SearchBoxRef" formName="selectTop" :formData="query" @showPop="showPop" @reset="reset"></SearchBox>-->
    <el-card :body-style="cardBodyStyle">
      <el-form :model="query" label-width="auto" label-position="right">
        <el-row style="margin-bottom: 5px;" :gutter="10">
          <el-col  :span="5">
            <el-form-item label="库房名称" prop="storeNm" style="margin-bottom: 0px; ">
              <Popup_sto_config_one ref="PopupRefThree3" v-model="query.storeNm" :level1="true" @receive-from-child="retuKf"></Popup_sto_config_one>
            </el-form-item>
          </el-col>
          <el-col  :span="5">
            <el-form-item label="区号" prop="areaCd" style="margin-bottom: 0px; ">
              <Popup_sto_config_one ref="PopupRefThree3" v-model="query.areaCd" :level2="true" @receive-from-child="retuQh"></Popup_sto_config_one>
            </el-form-item>
          </el-col>
          <el-col  :span="5">
            <el-form-item label="垛位" prop="stackCd" style="margin-bottom: 0px; ">
              <Popup_sto_config_one ref="PopupRefThree3" v-model="query.stackCd" :level3="true" @receive-from-child="retuDw"></Popup_sto_config_one>
            </el-form-item>
          </el-col>
          <el-col  :span="5">
            <el-form-item label="物料名称" prop="itemNm" style="margin-bottom: 0px; ">
              <el-input clearable placeholder="请输入" v-model="query.itemNm" >
                <template #append>
                  <el-button :icon="Search" @click="clickWl"/>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <ButtonBox @search="handleSearch" ></ButtonBox>

    <el-card :body-style="cardBodyStyle">
      <el-table :data="tableData" border highlight-current-row @selection-change="handleSelectionChange" :height="height">
        <el-table-column type="selection" fixed></el-table-column>
        <el-table-column type="index" label="序号" align="center" width="60">
          <template #default="scope">
            <span>{{ scope.$index + (currentPage - 1) * pageSize + 1 }}</span>
          </template>
        </el-table-column>

        <el-table-column v-if="false" prop="compCd" label="公司代码" width="160"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="false" prop="deptCd" label="部门代码" width="160"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="storeCd" label="库房代码" width="160"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="storeNm" label="库房名称" width="200"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="itemNm" label="物料名称" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qty" label="库存现有量" width="140"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="areaCd" label="区号" width="120"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="stackCd" label="垛位号" width="160"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="storeType" label="库房类型" :formatter="formatter"
                         width="140"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="itemCd" label="物料编码" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>

        <el-table-column v-if="true" prop="itemType" label="物料类型" width="140"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="uom" label="计量单位" width="140" :formatter="formatter"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>

        <el-table-column v-if="true" prop="qtyWet" label="湿基量" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="lastName" label="最后操作人" width="300"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="lastTime" label="最后操作时间" width="360"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="remarks" label="备注" width="120"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="price" label="物料成本" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="batchNo" label="批次号" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>

        <el-table-column label="操作" header-align="center" align="center" width="180px" fixed="right">
          <template #default="scope">
            <el-button plain type="primary" @click="handleEdit(scope.row)">查看</el-button>
<!--            <el-button plain type="warning" @click="handleEdit(scope.row)">编辑</el-button>-->
<!--            <el-button plain type="danger" @click="handleDelete(scope.row )">删除</el-button>-->
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card :body-style="cardBodyStyle">
      <el-pagination :current-page="currentPage" :page-size="pageSize" :page-sizes="pageSizes"
                     :total="totalRows" background="background" layout="total, sizes, prev, pager, next, jumper"
                     @size-change="handleSizeChange" @current-change="handleCurrentChange">
      </el-pagination>
    </el-card>
    <el-dialog class="diaClass"  :close-on-click-modal="false" draggable title="查看" v-model="dialogVisible" width="40%" top="5vh"
               @open="openDialog">
      <el-card >
      <el-form :model="form" label-width="120px" label-position="right" inline ref="ruleFormRef" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司代码">
              <el-input v-model="form.compCd" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库房代码">
              <el-input v-model="form.storeCd" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库房名称">
              <el-input v-model="form.storeNm" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区号">
              <el-input v-model="form.areaCd" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="垛位号">
              <el-input v-model="form.stackCd" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库房类型">
              <el-input v-model="form.storeType" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编码">
              <el-input v-model="form.itemCd" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称">
              <el-input v-model="form.itemNm" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类型">
              <el-input v-model="form.itemType" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购订单号">
              <el-input v-model="form.purOrdNo" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="采购订单行号">
              <el-input v-model="form.purOrdLn" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类型">
              <el-input v-model="form.itemType" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计量单位">
              <el-select v-model="form.uom" disabled>
                <el-option
                    v-for="item in dictList.MEASURING_UNIT"
                    :key="item.dicCode"
                    :label="item.dicName"
                    :value="item.dicCode"
                />
              </el-select>
<!--              <el-input v-model="form.uom" readonly></el-input>-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="库存现有量">
              <el-input v-model="form.qty" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="湿基量">
              <el-input v-model="form.qtyWet" readonly></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input
                  readonly
                  v-model="form.remarks"
                  :autosize="{ minRows: 2, maxRows: 4 }"
                  type="textarea"
                  placeholder=""
              />
            </el-form-item>
          </el-col>

        </el-row>
      </el-form>
      </el-card>
      <template #footer>
        <div class="dialog-footer">
<!--          <el-button type="success" @click="handleSave(ruleFormRef)">保存</el-button>-->
          <el-button type="danger" plain @click="handleCancelSave">取消</el-button>
        </div>
      </template>
    </el-dialog>

    <Popup_base_wl ref="PopupRefTwo" @receive-from-child="retuWl"></Popup_base_wl>
  </div>
</template>

<script setup>
import ButtonBar from '@/components/ButtonBar.vue'

import {
  ref,
  reactive,
  onMounted,
  getCurrentInstance
} from 'vue'

const {
  proxy
} = getCurrentInstance()

import tools from '@/tools'
import SearchBox from "@/components/SearchBox.vue";
import ButtonBox from "@/components/ButtonBox.vue";
import Popup_sto_config_one from "@/views/sto/components/Popup_sto_config_one.vue";
import Popup_base_wl from "@/views/bas/Popup_base_wl.vue";
import {Search} from '@element-plus/icons-vue'

const cardBodyStyle = ref(tools.style.card)
const inputStyle = ref(tools.style.input)

onMounted(() => {
  handleSearch()
  dictData()
  updateViewportHeight()

})


/**
 *   查询
 */




const query = ref({
  storeNm : '',
  areaCd : '',
  stackCd : '',
  itemNm : '',
  itemType : '',
  storeType : '',
})
const tableData = ref([])
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

const getTableData = () => {
  var url = 'sto/Onhand/query'
  var param = {
    page: currentPage.value,
    rows: pageSize.value,
    params: JSON.stringify(query.value)
  }
  // tableData.value = list
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      tableData.value = response.data.data.records
      totalRows.value = response.data.data.total
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}
/**
 *   查询
 */

/**
 * 分页
 */
const currentPage = ref(1)
const totalRows = ref(0)
const pageSizes = ref([10, 20, 30, 40])
const pageSize = ref(10)
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}
const handleCurrentChange = (val) => {
  currentPage.value = val
  getTableData()
}
/**
 * 分页
 */


/**
 * 新增
 */
const dialogVisible = ref(false)
const form = ref({})
const ruleFormRef = ref()
const rules = reactive({})

const handleAdd = () => {
  form.value = {}
  dialogVisible.value = true
}
/**
 * 新增
 */


/**
 * 保存、修改、删除
 */

const openDialog = () => {
}

const handleSave = (ruleFormRef) => {
  ruleFormRef.validate((valid, fields) => {
    if (valid) {
      var url = 'sto/Onhand/save'
      var param = {
        json: JSON.stringify(form.value),
      }
      proxy.$axios.post(url, param).then(response => {
        if (response.data.code == 0) {
          form.value = response.data.data
          ElMessage({
            message: '保存成功',
            type: 'success',
          })
          getTableData()
          dialogVisible.value = false
        } else {
          // ElMessage.error('操作失败！' + response.data.msg)
          ElMessage({
            message: '操作失败' + response.data.msg,
            type: 'error'
          })
        }
      })

    } else {
      console.log('error submit!', fields)
    }
  })
}
const handleCancelSave = () => {
  dialogVisible.value = false
}
const handleEdit = (row) => {

  var url = 'sto/Onhand/queryByPK'
  var param = {
    id: row.id,
  }
  proxy.$axios.post(url, param).then(response => {
    if (response.data.code == 0) {
      form.value = response.data.data
      dialogVisible.value = true
    } else {
      // ElMessage.error('操作失败！' + response.data.msg)
      ElMessage({
        message: '操作失败' + response.data.msg,
        type: 'error'
      })
    }
  })
}
const handleDelete = (row) => {
  ElMessageBox.confirm(
      '确定删除本条记录?',
      '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
        draggable: true,
      }
  )
      .then(() => {
        var url = 'sto/Onhand/remove'
        var param = {
          id: row.id
        }
        proxy.$axios.post(url, param).then(response => {
          if (response.data.code == 0) {
            ElMessage({
              type: 'success',
              message: '删除成功',
            })
            getTableData()
          } else {
            ElMessage({
              type: 'error',
              message: '删除失败' + response.data.msg
            });
          }
        });
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消',
        })
      })
}

/**
 * 保存、修改、删除
 */

const multipleSelection = ref([])
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

const handleRemoveBatch = () => {
  console.log('handleRemoveBatch')

  if (multipleSelection.value.length < 1) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的数据!'
    })
    return
  }

  ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    var ids = []
    multipleSelection.value.forEach((item, index) => {
      ids.push(item.id)
    })
    var url = 'sto/Onhand/removeBatch'
    var param = {
      ids: JSON.stringify(ids)
    }
    proxy.$axios.post(url, param).then(response => {
      if (response.data.code == '0') {
        ElMessage({
          type: 'success',
          message: '操作成功!',
        });
        getTableData()
        dialogVisible.value = false
      } else {
        ElMessage({
          type: 'error',
          message: '操作失败!' + response.data.msg
        });
      }
    })
  }).catch(() => {
    ElMessage({
      type: 'info',
      message: '已取消删除'
    })
  })
}

const handleEditBatch = () => {
  console.log('handleEditBatch')
  if (multipleSelection.value.length != 1) {
    ElMessage({
      type: 'warning',
      message: '请选择一条数据编辑!'
    })
    return
  }
  handleEdit(multipleSelection.value[0])
}

const handleUpload = () => {
  console.log('handleUpload')
}

const handleDownload = () => {
  console.log('handleDownload')
}

const handleImport = () => {
  console.log('handleImport')
}

const handleExport = () => {
  console.log('handleExport')
}

const retuKf = (val) => {
  console.log('val', val)

  query.value.storeNm = val.value
}
const retuQh = (val) => {
  console.log('val', val)
  query.value.areaCd = val
}
const retuDw = (val) => {
  console.log('val', val)
  query.value.stackCd = val
}
const retuWl = (val) => {
  console.log('val', val)
  query.value.itemNm = val.itemNm
}
const PopupRefTwo = ref(null)
const clickWl = () => {
  PopupRefTwo.value.open()
}

const height = ref(600)
const updateViewportHeight = () => {
  height.value = window.innerHeight * 0.65
  console.log( window.innerHeight);
};

const formatter = (row, column, cellValue, index) => {
  if (!cellValue || cellValue == null || cellValue == '') {
    return cellValue
  }
  switch (column.property) {
    case 'storeType':
      cellValue = dictList.value && Array.isArray(dictList.value.STORE_TYPE)
          ? dictList.value.STORE_TYPE.find(item => item.dicCode == cellValue)?.dicName
          : undefined
      break
    case 'uom':
      cellValue = dictList.value && Array.isArray(dictList.value.MEASURING_UNIT)
          ? dictList.value.MEASURING_UNIT.find(item => item.dicCode == cellValue)?.dicName
          : undefined
      break
  }
  return cellValue
}
const dictList = ref({})
const dictData = () => {
  var url = 'framework/Common/querySelectorList'
  var param = {
    type: JSON.stringify(['STORE_TYPE' , 'MEASURING_UNIT'  ])
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      dictList.value = response.data.data
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })
}


</script>

<style scoped>

::v-deep .diaClass .el-form-item {
  width: 100%
}
::v-deep .el-autocomplete{
  width: 100%;
}
::v-deep .el-dialog__body {
  background-color: #F2F6FC;
}
</style>
