<!--质检信息-->
<template>
  <div class="center">
    <el-row>
      <el-col :span="12">
        <h2 class="bg-left titleup">{{ information }}质检信息</h2>
      </el-col>
      <el-col :span="12" class="bg-right">
        <el-button class="bg-bottom" type="primary" size="small" @click="cancel"
          ><img
            width="6"
            height="10"
            style="vertical-align: bottom; margin-right: 3px"
            src="../../../public/img/lujing.png"
            alt=""
          />返回</el-button
        >
      </el-col>
    </el-row>
    <div class="substance">
      <ws-form>
        <div style="width: 50%; margin: 0 auto">
          <div class="title">基本信息</div>
          <ws-info-table>
            <ws-form-item label="编号" span="1" prop="qualityNo">
              <ws-input
                v-model="inspect.qualityNo"
                placeholder="请输入编号"
                maxlength="100"
                disabled
                size="small"
              />
            </ws-form-item>
            <ws-form-item label="仓位号" span="1" prop="binNumber">
              <ws-select
                v-model="inspect.binNumber"
                placeholder="请输入仓位号"
                class="typeselect"
                :disabled="disabled"
              >
                <ws-option
                  v-for="item in getbinNumberList"
                  :key="item.value"
                  :label="item.value"
                  :value="item.value"
                />
              </ws-select>
            </ws-form-item>
            <ws-form-item label="客户" span="1" prop="customerName">
              <ws-select
                placeholder="请输入客户"
                class="typeselect"
                @change="selectpackingMethod"
                :disabled="disabled"
              >
                <ws-option
                  v-for="item in customerList"
                  :key="item.valueKey"
                  :label="item.value"
                  :value="item.value"
                />
              </ws-select>
            </ws-form-item>
            <ws-form-item label="囤位号" span="1" prop="storageTagNo">
              <ws-input
                v-model="inspect.storageTagNo"
                placeholder="请输入囤位号"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="车牌号" span="1" prop="carNumber">
              <ws-input
                v-model="inspect.carNumber"
                placeholder="请输入车牌号"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="货名" span="1" prop="goodsName">
              <ws-select
                v-model="inspect.goodsName"
                placeholder=""
                class="typeselect"
                :disabled="disabled"
                @change="waterContentChange"
              >
                <ws-option
                  v-for="item in goodnameList"
                  :key="item.goodsNameKey"
                  :label="item.goodsName"
                  :value="item.goodsName"
                />
              </ws-select>
            </ws-form-item>
            <ws-form-item label="扣重比" span="1" prop="buckleWeightRatio">
              <ws-input
                v-model="inspect.buckleWeightRatio"
                placeholder="请输入扣重比"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
          </ws-info-table>

          <div class="title">质检数据</div>
          <ws-info-table>
            <ws-form-item label="等级" span="1" prop="grade">
              <ws-select
                v-model="inspect.grade"
                placeholder="请输入等级"
                class="typeselect"
                @change="waterContentChange"
                :disabled="disabled"
              >
                <ws-option
                  v-for="item in gradeList"
                  :key="item.constKey"
                  :label="item.constValue"
                  :value="item.constValue"
                />
              </ws-select>
            </ws-form-item>
            <ws-form-item label="杂质（%）" span="1" prop="impurity">
              <ws-input
                v-model="inspect.impurity"
                placeholder="请输入杂质占比"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="水分（%）" span="1" prop="waterContent">
              <ws-input
                v-model="inspect.waterContent"
                placeholder="请输入水分占比"
                maxlength="100"
                size="small"
                @change = "waterContentChange"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="霉变粒（%）" span="1" prop="mildewGrain">
              <ws-input
                v-model="inspect.mildewGrain"
                placeholder="请输入霉变粒占比"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="容重（克/升）" span="1" prop="bulkDensity">
              <ws-input
                v-model="inspect.bulkDensity"
                placeholder="请输入容重"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="热损伤（%）" span="1" prop="jiaorenli">
              <ws-input
                v-model="inspect.jiaorenli"
                placeholder="请输入热损伤占比"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
            <ws-form-item label="不完整粒（%）" span="1" prop="imperfectGrain">
              <ws-input
                v-model="inspect.imperfectGrain"
                placeholder="请输入不完整粒占比"
                maxlength="100"
                size="small"
                :disabled="disabled"
              />
            </ws-form-item>
          </ws-info-table>
          <div style="text-align: right; color: #323233">
            潮粮单价（元/公斤）：{{ unitPrice}}
            <!-- 潮粮单价（元/公斤）：{{ inspect.tidalGrainPrice == null?"0": inspect.tidalGrainPrice}} -->
          </div>
          <div class="but">
            <el-button @click="cancel" v-if="this.types != 2" type="primary"
              >取消</el-button
            >
            <el-button @click="print" v-if="this.types != 2" type="primary"
              >保存并打印</el-button
            >
            <el-button @click="cancel" v-if="this.types == 2" type="primary"
              >关闭</el-button
            >
          </div>
        </div>
      </ws-form>
    </div>
    <houseSelfCollectPrint v-show="false"></houseSelfCollectPrint>
  </div>
</template>
<script>
import {
  inspectAdd,
  getbinNumber,
  getcustomer,
  getinspectLook,
  getinspectLookUnitPrice,
  getinspectLookGoods,
  getinspectEdit
} from '@/model/houseSelfCollect/index'
import { packList } from '@/model/contarct/index'
import houseSelfCollectPrint from './houseSelfCollectPrint.vue'
export default {
  components: {
    houseSelfCollectPrint,
  },
  watch: {},
  data() {
    return {
      // warehouseList: {},
      inspect: {},
      disabled: false,
      types: '1',
      information: '添加',
      goodnameList: [],
      gradeList: [],
      customerList: [],
      getbinNumberList: [],
      cangid: '', //仓库id
      warehouseName:'',
       unitPrice:0,
       count:0,
       cangNo:"",
    }
  },
  activated() {
    this.types = this.$route.query.type
    this.cangid = this.$route.query.cangid
    this.warehouseName = this.$route.query.warehouseName
    let id = this.$route.query.id
    this.cangNo = this.$route.query.warehouseNo
    this.count = Number(this.$route.query.count)+1 
    if (this.types == 2) {
      this.disabled = true
      this.information = '查看'
    } else if (this.types == 3) {
      this.disabled = false
      this.information = '编辑'
    }
    this.calculation()
    this.getList(id)
  },
  methods: {
    getdate() {
      var date = new Date()
      var year = date.getFullYear() //获取完整的年份(4位)
      var mouth = date.getMonth() + 1 //获取当前月份(0-11,0代表1月)
      var datetime = date.getDate() //获取当前日(1-31)
      if (mouth < 10) {
        mouth = '0' + mouth
      }
      if (datetime < 10) {
        datetime = '0' + datetime
      }
      return year + mouth + datetime
    },
    calculation() {
      let number ="000"+ this.count
      number = number.substring(number.length - 4,number.length)
      this.inspect.qualityNo = 'SGRK' + this.getdate()+this.cangNo+number //SGRK+8位时间+3位仓库编号+4位序列号
      // + this.verifyinit()
    },
    print() {
      this.inspect.warehouseName = this.warehouseName
          this.inspect.compId = sessionStorage.getItem('ws-pf_compId')
      if(this.types == null){
         this.$confirm('确定保存质检信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          
          inspectAdd(this.inspect)
            .toPromise()
            .then((response) => {
              this.$message.success('保存成功')
              // this.xx = true;
              // let newstr = document.getElementById('print').innerHTML
              // let oldstr = document.body.innerHTML
              // document.body.innerHTML = newstr
              // window.print()
              // document.body.innerHTML = oldstr
              // // window.location.reload()
              this.$router.push({ path: 'paymentManagement' })
            })
        })
        .catch(() => {
          return false
        })

      }else if(this.types == 3){
         this.$confirm('确定保存质检信息？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // return
          getinspectEdit(this.inspect)
            .toPromise()
            .then((response) => {
              this.$message.success('修改成功')
              // this.xx = true;
              // let newstr = document.getElementById('print').innerHTML
              // let oldstr = document.body.innerHTML
              // document.body.innerHTML = newstr
              // window.print()
              // document.body.innerHTML = oldstr
              // window.location.reload()
              this.$router.push({ path: 'paymentManagement' })
            })
        })
        .catch(() => {
          return false
        })

      }
     
    },
    cancel() {
      this.$router.push({ path: 'inspectionManagement' })
    },
    waterContentChange() {
      //潮粮单价改变事件
      if(this.inspect.goodsName != null && this.inspect.waterContent != null && this.inspect.grade != null){
        getinspectLookUnitPrice({
        warehouseId: this.cangid,
        goodsName: this.inspect.goodsName,
        waterContent: this.inspect.waterContent,
        grade:this.inspect.grade
      })
        .toPromise()
        .then((response) => {
          this.inspect.tidalGrainPrice = response
          this.unitPrice = response
        })
      }
      
    },
    //货名改变事件
    selectpackingMethod(e) {
      let customers = e.split("(")
      //  customers = customers[1].split(")")

      this.inspect.customerName = customers[0]
      this.inspect.customerPhone = customers[1].split(")")[0]

    },
    getList(ids) {
      // 货名
      getinspectLookGoods({ warehouseId: this.cangid })
        .toPromise()
        .then((response) => {
          this.goodnameList = response
        })
      // 品级
      packList({ constId: 'CON3' })
        .toPromise()
        .then((response) => {
          this.gradeList = response
        })
      //客户
      getcustomer({ compId: sessionStorage.getItem('ws-pf_compId') })
        .toPromise()
        .then((response) => {
          // this.gradeList = response
          this.customerList = []
          let customer = response
          for (let i = 0; i < response.length; i++) {
            customer =
              response[i].customerName + '(' + response[i].customerPhone + ')'
            this.customerList.push({
              valueKey: i,
              value: customer,
            })
          }
        })
      // 仓位
      getbinNumber({ id: this.cangid })
        .toPromise()
        .then((response) => {
          this.getbinNumberList=[]
          for (let i = 0; i < response.warehousePositionInfoList.length; i++) {
            this.getbinNumberList.push({
              value: response.warehousePositionInfoList[i].binNumber,
            })
          }
          // this.getbinNumberList = response.warehousePositionInfoList
        })
      if (this.types == 2 || this.types == 3) {
        //查看
        getinspectLook({ id: ids })
          .toPromise()
          .then((response) => {
            this.inspect = response
          })
      }
    },
  },
}
</script>
<style lang="scss" scoped>
.center {
  background: #f6f7fc;
}

.ws-info-table {
  border: none;
}
.ws-info-table .el-form-item {
  width: 50%;
  border: none;
}
/deep/.ws-info-table .el-form-item .el-form-item__label {
  width: 30%;
  text-align: center;
  background: #ffffff;
  font-size: 14px;
  color: #8890b1;
}
/deep/.ws-info-table .el-form-item .el-form-item__content {
  border: none;
}
.title {
  font-size: 16px;
  font-weight: 600;
  margin: 10px;
}
.titleup {
  position: relative;
}
.title::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 15px;
  background: #8890b1;
  margin-right: 5px;
}

.substance {
  background: #ffffff;
  width: 100%;
  border-radius: 4px;
  margin: 0 auto;
  padding-top: 20px;
}
/deep/.el-table .el-table__header .cell,
.el-table .el-table__body .cell {
  text-align: center;
}
/deep/.el-table--enable-row-transition .el-table__body td {
  text-align: center;
}
.bg-left {
  padding-left: 30px;
}
.bg-right {
  padding-right: 10px;
  text-align: right;
}
.bg-bottom {
  margin: 15px 0px;
}
.titleup {
  position: relative;
}
.titleup::before {
  content: '';
  display: inline-block;
  width: 5px;
  height: 30px;
  background: #5473e8;
  position: absolute;
  left: 0;
}
.but {
  text-align: center;
  margin: 20px auto;
}
</style>
