<template>
  <div>
    <el-card style="margin-top: -20px">
      <!--<div style=" float: right;-->
      <!--                  text-align: right;-->
      <!--                  margin-bottom: 8px;">-->
      <!--  <el-button type="primary" size="mini" icon="search" @click="addDrugLevel"-->
      <!--             style="text-align:right">新增药品层级-->
      <!--  </el-button>-->
      <!--</div>-->
      <el-descriptions title="产品信息" :column="2" border style="margin-top: 5px">
        <el-descriptions-item label="UDI码" v-if="destroyType == 2">
          {{ prescribeData.code }}
        </el-descriptions-item>
        <el-descriptions-item label="产品名称">{{ prescribeData.cpmctymc }}</el-descriptions-item>
        <el-descriptions-item label="产品标识">{{ prescribeData.nameCode }}</el-descriptions-item>
        <el-descriptions-item label="产品规格">{{ prescribeData.ggxh }}</el-descriptions-item>
        <el-descriptions-item label="批次号">{{ prescribeData.batchNo }}</el-descriptions-item>
        <el-descriptions-item label="计量单位">{{ prescribeData.measname }}</el-descriptions-item>
        <el-descriptions-item label="剩余数量" custom-style class="custom-label">
          <el-tag size="small">{{ prescribeData.reCount }}</el-tag>
          <!--<el-tag type="danger">{{ prescribeData.reCount}}</el-tag>-->
        </el-descriptions-item>
        <el-descriptions-item label="生产日期">{{ produceDate }}</el-descriptions-item>
        <el-descriptions-item label="失效日期">{{ expireDate }}</el-descriptions-item>
        <el-descriptions-item label="生产企业">{{ prescribeData.manufactory }}</el-descriptions-item>
        <!--<el-descriptions-item label="供应商">{{ prescribeData.supName }}</el-descriptions-item>-->
        <el-descriptions-item label="医保编码">{{ prescribeData.ybbm }}</el-descriptions-item>
      </el-descriptions>
      <!--<el-button type="primary" icon="el-icon-plus" sty>提交</el-button>-->

      <el-form :inline="true" :model="formData" class="demo-form-inline" style="margin-top: 30px" :rules="rules"
               ref="formRef">
        <el-row :gutter="24">
          <el-col :span="10">
            <el-form-item label="损耗出库数量:" prop="count">
              <el-input v-model.number="formData.count" placeholder="请填写损耗出库数量" type="number" :min="1"
                        @input="checkCount"></el-input>
              <!--<span v-if="countExceeded" class="exceeded-message">损耗出库数量不能超过10个</span>-->
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="损耗出库说明:">
              <el-input v-model="formData.remark" placeholder="请填写损耗出库说明" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <!--<el-form-item>-->
        <!--  <el-button type="primary" @click="onSubmit">提交</el-button>-->
        <!--</el-form-item>-->
      </el-form>

      <div style="text-align: center; margin-top: 20px;">
        <el-button type="primary" @click="destroyOutOrder" :loading="saveLoading">提交</el-button>
        <el-button @click="closeAddDialog">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import {destroyOut, destroyOutByCode, destroySplitOut} from '@/api/collect/IoDestroy'

export default {
  name: 'destroyOutDialog',
  props: {
    prescribeData: {
      type: Object,
      required: true
    },
    destroyType: {
      type: Object,
      required: true
    },
    closeDialog: {
      type: Function,
      required: true
    }
  },
  data() {
    return {
      formData: {},
      produceDate: null,
      expireDate: null,
      saveLoading: false,
      rules: {
        count: [
          {validator: this.validateCount, message: '损耗出库数量不能超出剩余数量', trigger: 'blur'}
        ]
      },

    }
  },
  methods: {
    conversionDate(dateString) {
      const parts = dateString.split('');
      const year = '20' + parts.slice(0, 2).join('');
      const month = parts[2] + parts[3];
      const day = parts[4] + parts[5];
      return `${year}-${month}-${day}`;
    },
    validateCount(rule, value, callback) {
      if (value > this.prescribeData.reCount) {
        callback(new Error('损耗出库数量超出剩余数量'));
      } else {
        callback();
      }
    },
    checkCount() {
      // 这里可以调用表单验证方法
      this.$refs.formRef.validateField('count');
    },
    closeAddDialog() {
      this.closeDialog()
    },

    //损耗出库
    destroyOutOrder() {
      if (this.formData.count == null) {
        return this.$message.error("请输入损耗数量")
      }
      if (this.formData.count > this.prescribeData.reCount) {
        return this.$message.error("损耗出库数量不能超出剩余数量")
      }
      let query = {
        outCount: this.formData.count,
        remark: this.formData.remark,
        updateUser: this.$store.getters.adminId,
        createUser: this.$store.getters.adminId,
        splitFifoInv: this.prescribeData,
        code: this.prescribeData.code,
      }
      if (this.destroyType == 1) {
        destroyOut(query).then(res => {
          if (res.code == 20000) {
            this.closeDialog()
            return this.$message.success("损耗出库成功")
          } else {
            return this.$message.error(res.message)
          }
        })
      } else if (this.destroyType == 2) {
        destroyOutByCode(query).then(res => {
          if (res.code == 20000) {
            this.closeDialog()
            return this.$message.success("损耗出库成功")
          } else {
            return this.$message.error(res.message)
          }
        })
      } else if (this.destroyType == 3) {
        destroySplitOut(query).then(res => {
          if (res.code == 20000){
            this.closeDialog()
            this.$message.success("损耗出库成功")
          }else {
            this.$message.error(res.message)
          }
        }).catch(() => {

        })
      }

    },
    destroyOutByCodeOrder() {
      if (this.formData.count == null) {
        return this.$message.error("请输入损耗数量")
      }
      let query = {
        outCount: this.formData.count,
        remark: this.formData.remark,
        updateUser: this.$store.getters.adminId,
        createUser: this.$store.getters.adminId,
        splitFifoInv: this.prescribeData,
        code: this.formData.code,
      }

    }

  },
  created() {
    if (this.prescribeData.produceDate != null) {
      this.produceDate = this.conversionDate(this.prescribeData.produceDate)
    }
    if (this.prescribeData.expireDate != null) {
      this.expireDate = this.conversionDate(this.prescribeData.expireDate)
    }
    if (this.destroyType == 2) {
      this.prescribeData.reCount = this.prescribeData.totalCount
    }
    if (this.destroyType == 3) {
      this.prescribeData.reCount = this.prescribeData.remainCount
      this.prescribeData.cpmctymc = this.prescribeData.productName
    }
  }
}
</script>

<style scoped>
/* 添加自定义样式 */
.custom-label {
  background-color: #f56c6c; /* 选择你想要的背景色 */
  color: white; /* 文字颜色，根据背景色调整 */
  /*padding: 5px 10px; !* 内边距 *!*/
  /*border-radius: 4px; !* 边框圆角 *!*/
}


</style>
