<template>
  <div class="layout_display_flex_y">
    <div>
      <span>入库日期：</span>
      <el-date-picker
          v-model="DateRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          format="YYYY-MM-DD"
          :shortcuts="shortcuts"
          style="width: 240px"
      ></el-date-picker>
      <span>入库单号：</span>
      <el-input style="width: 150px" clearable v-model="queryTerm.inDocuNo"
                @blur="inDocuNo = $event.target.value.trim()"></el-input>
      <el-button style="margin-left: 10px" type="primary" @click="queryRuKuXinXiClick">查询</el-button>
    </div>
    <div class="layout_flex_1-y layout_display_flex_y">
      <el-table
          height="100%"
          :data="ruKuXinXiList">
        <el-table-column label="药品编码" prop="chargeCode"></el-table-column>
        <el-table-column label="药品名称" prop="chargeCodeName"></el-table-column>
        <el-table-column label="入库日期" prop="inDate" width="80px"></el-table-column>
        <el-table-column label="入库单号" prop="inDocuNo"></el-table-column>
        <el-table-column label="供应商" prop="supplyCodeName"></el-table-column>
        <el-table-column label="发票号" prop="reptNo"></el-table-column>
        <el-table-column label="采购员" prop="purchaserName"></el-table-column>
        <el-table-column label="生产批号" prop="manuNo"></el-table-column>
        <el-table-column label="效期" prop="effDate"></el-table-column>
        <el-table-column label="发票号" prop="fpNo"></el-table-column>
        <el-table-column label="发票日期" prop="fpDate" width="80px"></el-table-column>
        <el-table-column label="生产厂家" prop="manuCodeName"></el-table-column>
        <el-table-column label="批准文号" prop="licenseNo"></el-table-column>
        <el-table-column label="操作">
          <template #default="scope">
            <el-button @click="xiuGiClick(scope.row)" type="warning">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryTerm.currentPage"
            :page-sizes="[100, 200, 300, 400]"
            :page-size="queryTerm.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryTerm.total"
        >
        </el-pagination>
      </div>
    </div>
  </div>

  <!-- 修改的抽屉 -->
  <el-drawer title="修改入库信息" v-model="ruKuDrawer" size="70%">
    <div style="width: 98%">
      <el-form label-width="100px">
        <el-row>
          <el-col :span="6">
            <el-form-item label="药品编码:">
              <el-input v-model="ruKuXiangXiXinXi.chargeCode" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="药品名称:">
              <el-input v-model="ruKuXiangXiXinXi.chargeCodeName" disabled></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="入库单号:">
              <el-input v-model="ruKuXiangXiXinXi.inDocuNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商:">
              <el-input v-model="ruKuXiangXiXinXi.supplyCodeName" @click="xiuGaiCode(1)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="传票号:">
              <el-input v-model="ruKuXiangXiXinXi.reptNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购员:">
              <el-input v-model="ruKuXiangXiXinXi.purchaserName" @click="xiuGaiCode(2)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="生产批号:">
              <el-input v-model="ruKuXiangXiXinXi.manuNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="发票号:">
              <el-input v-model="ruKuXiangXiXinXi.fpNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="生产厂家:">
              <el-input v-model="ruKuXiangXiXinXi.manuCodeName" @click="xiuGaiCode(3)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="批准文号:">
              <el-input v-model="ruKuXiangXiXinXi.licenseNo"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="备注:">
              <el-input v-model="ruKuXiangXiXinXi.inComment"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入库时间:">
              <el-date-picker type="datetime" v-model="ruKuXiangXiXinXi.inDate"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认时间:">
              <el-date-picker type="datetime" v-model="ruKuXiangXiXinXi.confirmDate"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发票日期:">
              <el-date-picker type="datetime" v-model="ruKuXiangXiXinXi.fpDate"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="效期:">
              <el-date-picker type="datetime" v-model="ruKuXiangXiXinXi.effDate"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col>
            <el-button @click="baoCunXiuGi" style="margin-left: 20px" type="success">保存修改</el-button>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </el-drawer>
  <el-dialog :title="dialogTitle" v-model="dialogXiuGi" width="400px">
    <span>{{ dialogTitle }}：</span>
    <el-select v-model="codeAndName" @change="changeData" placeholder="根据拼音首字母查找" filterable clearable
               remote :remote-method="remoteMethodCodeAndName">
      <el-option v-for="item in codeAndNameList" :key="item.code" :label="item.name" :value="item.code">
        <span style="color: #8492a6; font-size: 12px">{{ item.code }}</span>
        <el-divider direction="vertical"></el-divider>
        <span>{{ item.name }}</span>
      </el-option>
    </el-select>
  </el-dialog>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {formatDatetime, getDateRangeFormatDate} from '@/utils/date'
import {ElMessage} from 'element-plus'
import {queryCode, queryRuKuXinXi, xiuGaiRuKuXinXi} from '@/api/yz-data-mod/in-or-out-of-warehouse'
import {clone} from 'xe-utils'
import {shortcuts} from "@/data/shortcuts";

//
const tableHeight = window.innerHeight - 70

//查询条件
const DateRange = ref('')
//把条件合起来
const queryTerm = reactive({
  startTime: '',
  endTime: '',
  inDocuNo: '',
  currentPage: 1,
  pageSize: 100,
  total: 0,
})
//分页
const handleSizeChange = (val) => {
  queryTerm.pageSize = val
  fenYe()
}

const handleCurrentChange = (val) => {
  queryTerm.currentPage = val
  fenYe()
}

const fenYe = () => {
  queryRuKuXinXi(queryTerm).then((res) => {
    ruKuXinXiList.value = res.data
    queryTerm.total = res.total
  })
}

//返回的数据
const ruKuXinXiList = ref([])

//点击查询
const queryRuKuXinXiClick = () => {
  if (!DateRange.value && queryTerm.inDocuNo === '') {
    ElMessage.warning({
      message: '至少选择一项',
      showClose: true,
      duration: 2500,
    })
    return
  }
  let getDateRange = getDateRangeFormatDate(DateRange.value)

  queryTerm.startTime = getDateRange.startTime
  queryTerm.endTime = getDateRange.endTime

  queryRuKuXinXi(queryTerm).then((res) => {
    ruKuXinXiList.value = res.data
    queryTerm.total = res.total
  })
}

const ruKuXiangXiXinXi = ref({})

const ruKuDrawer = ref(false)

const xiuGiClick = (val) => {
  ruKuDrawer.value = true
  ruKuXiangXiXinXi.value = clone(val, true)
  ruKuXiangXiXinXi.value.oldInDocuNo = clone(ruKuXiangXiXinXi.value.inDocuNo, true)
}

const dialogTitle = ref('提示')
const dialogXiuGi = ref(false)

const codeAndName = ref()
const flag = ref(0)

const codeAndNameList = ref([])

const xiuGaiCode = (val) => {
  dialogXiuGi.value = true
  flag.value = val
  codeAndNameList.value = []
  codeAndName.value = null
  if (val === 1) {
    dialogTitle.value = '修改供应商'
  } else if (val === 2) {
    dialogTitle.value = '修改采购员'
  } else if (val === 3) {
    dialogTitle.value = '修改生产厂商'
  }
}

const remoteMethodCodeAndName = (val) => {
  if (val.length > 1) {
    queryCode(val, flag.value).then((res) => {
      codeAndNameList.value = res
    })
  }
}

const changeData = (val) => {
  codeAndNameList.value.forEach((item) => {
    if (flag.value === 1 && item.code === val) {
      ruKuXiangXiXinXi.value.supplyCode = item.code
      ruKuXiangXiXinXi.value.supplyCodeName = item.name
    } else if (flag.value === 2 && item.code === val) {
      ruKuXiangXiXinXi.value.purchaser = item.code
      ruKuXiangXiXinXi.value.purchaserName = item.name
    } else if (flag.value === 3 && item.code === val) {
      ruKuXiangXiXinXi.value.manuCode = item.code
      ruKuXiangXiXinXi.value.manuCodeName = item.name
    }
  })
}

const baoCunXiuGi = () => {
  ruKuXiangXiXinXi.value.inDate = formatDatetime(ruKuXiangXiXinXi.value.inDate)
  ruKuXiangXiXinXi.value.confirmDate = formatDatetime(ruKuXiangXiXinXi.value.confirmDate)
  ruKuXiangXiXinXi.value.fpDate = formatDatetime(ruKuXiangXiXinXi.value.fpDate)
  ruKuXiangXiXinXi.value.effDate = formatDatetime(ruKuXiangXiXinXi.value.effDate)

  xiuGaiRuKuXinXi(ruKuXiangXiXinXi.value).then((res) => {
    fenYe()
    dialogXiuGi.value = false
  })
}

</script>

