<template>
  <div>
    <el-card :body-style="cardBodyStyle">
      <el-form :model="query" label-width="auto" label-position="right" ref="queryFormRef">
        <el-row :gutter="10" >
          <el-col :span="5">
            <el-form-item label="库房名称"  style="margin-bottom: 5px;" prop="storeNm">
              <el-input v-model="query.storeNm" placeholder="请输入库房名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="区号"  style="margin-bottom: 5px;" prop="areaCd">
              <el-input v-model="query.areaCd" placeholder="请输入区号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="垛位号"  style="margin-bottom: 5px;" prop="stackCd">
              <el-input v-model="query.stackCd" placeholder="请输入垛位号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="物料名称"  style="margin-bottom: 5px;" prop="itemNm">
              <el-input v-model="query.itemNm" placeholder="请输入物料名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="1">
            <el-button type="primary" link @click="reset(queryFormRef)">重置</el-button>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" link @click="queryShow = !queryShow">
              高级查询
              <el-icon v-if="queryShow">
                <ArrowUp/>
              </el-icon>
              <el-icon v-else>
                <ArrowDown/>
              </el-icon>
            </el-button>
          </el-col>
          <el-col :span="5" v-if="queryShow">
            <el-form-item label="物料类型" style="margin-top: 5px;margin-bottom: 5px;" prop="itemType">
              <el-select v-model="query.itemType" style="width: 100%" placeholder="物料类型" clearable >
                <el-option v-for="item in dictList.item_sort3" :key="item.dicCode" :label="item.dicName"
                           :value="item.dicCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5" v-if="queryShow">
            <el-form-item label="盘库时间" style="margin-top: 5px;margin-bottom: 5px;" prop="date">
              <el-date-picker
                  v-model="query.date"
                  type="daterange"
                  value-format="YYYY-MM-DD"
                  range-separator="至"
                  start-placeholder=""
                  end-placeholder=""
              />
            </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="true" prop="compCd" label="公司代码" width="160"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" 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="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="180"
                         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="itemNm" label="物料名称" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="itemType" label="规格" width="180"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qty" label="本次盘库量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qtyWet" label="本次盘库湿基量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qtyOld" label="原库存量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qtyWetOld" label="原库存湿基量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="qtyNew" label="新库存量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>

        <el-table-column v-if="true" prop="qtyWetNew" label="新库存湿基量" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>

        <el-table-column v-if="true" prop="stocktakingDate" label="盘库时间" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>


        <el-table-column v-if="true" prop="stocktakingUser" label="盘库人" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="submitEmpId" label="上传人员" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="submitFlag" label="上传状态" width="240" :formatter="formatter"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="submitDtm" label="上传时间" width="240"
                         header-align="center" align="center"
                         show-overflow-tooltip>
        </el-table-column>
        <el-table-column v-if="true" prop="remarks" label="备注" width="240"
                         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 :close-on-click-modal="false" draggable title="查看" v-model="dialogVisible" width="40%" class="diaClass"
               top="5vh" @open="openDialog">
      <el-card >
      <el-form :model="form" label-width="110" label-position="right" inline ref="ruleFormRef" :rules="rules">
        <el-row>
          <el-col :span="12">
            <el-form-item label="库房名称">
              <el-input v-model="form.storeNm" placeholder="" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区号">
              <el-input v-model="form.areaCd" placeholder="" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="垛位号">
              <el-input v-model="form.stackCd" placeholder="" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编码">
              <el-input v-model="form.itemCd" placeholder="" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料名称">
              <el-input v-model="form.itemNm" placeholder="" readonly>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类型">
              <el-select v-model="form.itemType" style="width: 100%" clearable  disabled>
                <el-option v-for="item in dictList.item_sort3" :key="item.dicCode" :label="item.dicName"
                           :value="item.dicCode" />
              </el-select>
<!--              <el-input v-model="form.itemType" placeholder="" readonly>-->
<!--              </el-input>-->
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本次盘库量">
              <el-input-number
                  disabled
                  style="width: 100%"
                  v-model="form.qty"
                  class="mx-4"
                  :min="0"
                  controls-position="right"
              />

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="本次盘库湿基量">
              <el-input-number
                  disabled
                  style="width: 100%"
                  v-model="form.qtyWet"
                  class="mx-4"
                  :min="0"
                  controls-position="right"
                  @change="handleChange"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原库存量">
              <el-input-number
                  disabled
                  style="width: 100%"
                  v-model="form.qtyOld"
                  class="mx-4"
                  :min="0"
                  :step="1"
                  :step-strictly="true"
                  controls-position="right"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="原库存湿基量">
              <el-input-number
                  disabled
                  style="width: 100%"
                  v-model="form.qtyWetOld"
                  class="mx-4"
                  :min="0"
                  :step="1"
                  :step-strictly="true"
                  controls-position="right"
              />
            </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" @click="handleCancelSave">取消</el-button>
        </div>
      </template>
    </el-dialog>


  </div>
</template>

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

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

const {
  proxy
} = getCurrentInstance()

import tools from '@/tools'

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

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


/**
 *   查询
 */




const query = ref({})
const tableData = ref([])
const handleSearch = () => {
  currentPage.value = 1
  getTableData()
}

const getTableData = () => {
  var url = 'sto/Stocktaking/query'
  var param = {
    page: currentPage.value,
    rows: pageSize.value,
    params: JSON.stringify(query.value)
  }
  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 queryForm = reactive({
  date: ''
})

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) => {
  dialogVisible.value = true
  form.value = row
}
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 queryFormRef = ref()
const reset = (queryFormRef) => {
  if (!queryFormRef) return
  queryFormRef.resetFields()
}

const dictList = ref({})
const dictData = () => {
  var url = 'framework/Common/querySelectorList'
  var param = {
    type: JSON.stringify(['MEASURING_UNIT' , 'item_sort3' ,'SUBMIT_FLAG'])
  }
  proxy.$axios.get(url, param).then(response => {
    if (response.data.code == '0') {
      dictList.value = response.data.data
    } else {
      ElMessage.error('操作失败！' + response.data.msg)
    }
  })

}

const queryShow = ref(false)

const formatter = (row, column, cellValue, index) => {
  if (!cellValue || cellValue == null || cellValue == '') {
    return cellValue
  }
  switch (column.property) {
    case 'submitFlag':
      cellValue = dictList.value && Array.isArray(dictList.value.SUBMIT_FLAG)
          ? dictList.value.SUBMIT_FLAG.find(item => item.dicCode == cellValue)?.dicName
          : undefined
      break;
  }
  return cellValue
}
const height = ref(600)
const updateViewportHeight = () => {
  height.value = window.innerHeight * 0.65
  console.log( window.innerHeight);
};

</script>

<style scoped>

::v-deep .diaClass90 .el-form-item {
  width: 90%
}

::v-deep .diaClass .el-form-item {
  width: 100%
}

::v-deep .el-dialog__body {
  background-color: #F2F6FC;
}
</style>
