<template>
  <el-dialog
    :title="!row ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" v-loading="formLoading" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="标识卡号" prop="idenCardNum">
        <el-input v-model="dataForm.idenCardNum" @change="setIdenCardNum" ref="idenCardNum" placeholder="标识卡号"></el-input>
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input v-model="dataForm.customerName" disabled placeholder="客户名称"></el-input>
      </el-form-item>
      <el-form-item label="材料牌号" prop="materialDes">
        <el-input v-model="dataForm.materialDes" disabled placeholder="材料牌号"></el-input>
      </el-form-item>
      <el-form-item label="订单号" prop="orderNo">
        <el-input v-model="dataForm.orderNo" disabled placeholder="订单号"></el-input>
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="dataForm.productName" disabled placeholder="产品名称"></el-input>
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="dataForm.quantity"
          :max="dataForm.targetQuantity"
          @change="changes"
          type="number"
          placeholder="数量"
          style="width:200px"
        ></el-input>
        <el-tag>数量最大值:{{dataForm.targetQuantity}}</el-tag>
      </el-form-item>
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="dataForm.unit"
          disabled
          placeholder="单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="重量" prop="weight">
        <el-input
          v-model="dataForm.weight"
          type="number"
          :max="dataForm.targetWeight"
          @change="changeWeight"
          placeholder="重量"
          style="width:200px"
        ></el-input>
        <el-tag>重量最大值:{{dataForm.targetWeight}}</el-tag>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          idenCardNum: null,
          customerName: null,
          materialDes: null,
          orderNo: null,
          productName: null,
          targetQuantity: null,
          quantity: null,
          unit: null,
          targetWeight: null,
          weight: null
        },
        dataRule: {
          idenCardNum: [
            { required: true, message: '标识卡号不能为空', trigger: 'blur' }
          ],
          quantity: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '重量不能为空', trigger: 'blur' }
          ]
        },
        formLoading: false,
        row: null,
        editIndex: null
      }
    },
    methods: {
      changes (value) {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          this.dataForm.quantity = null
        }
        if (value > this.dataForm.targetQuantity) {
          this.dataForm.quantity = null
        }
      },
      changeWeight (value) {
        if (/[\u4E00-\u9FA5]/g.test(value)) {
          this.dataForm.weight = null
        }
        if (value > this.dataForm.targetWeight) {
          this.dataForm.weight = null
        }
      },
      init (row, idx) {
        this.visible = true
        this.row = null
        this.editIndex = null
        this.formLoading = false
        this.dataForm.targetQuantity = null
        this.dataForm.targetWeight = null
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (row) {
            this.row = JSON.parse(JSON.stringify(row))
            this.dataForm = JSON.parse(JSON.stringify(row))
            this.editIndex = JSON.parse(JSON.stringify(idx))
          }
          this.$refs['idenCardNum'].focus()
        })
      },
      setIdenCardNum () {
        this.formLoading = true
        if (this.dataForm.idenCardNum) {
          this.$http({
            url: this.$http.adornUrl(`/orderInfo/getTaskInfoByIdenCardNum`),
            method: 'get',
            params: this.$http.adornParams({
              'idenCardNum': this.dataForm.idenCardNum
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm = data.data
              this.dataForm.targetQuantity = data.data.quantity - data.data.finishQuantity - data.data.waitQuantity
              this.dataForm.targetWeight = data.data.weight - data.data.finishWeight - data.data.waitWeight
              this.dataForm.weight = null
              this.dataForm.quantity = null
            } else {
              this.$message.warning(data.msg)
            }
            this.formLoading = false
          })
        } else {
          this.$message.warning('请输入标识卡号')
        }
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.editIndex || this.editIndex === 0) {
              const taskList = JSON.parse(sessionStorage.getItem('taskList'))
              taskList.splice(this.editIndex, 1, this.dataForm)
              sessionStorage.setItem('taskList', JSON.stringify(taskList))
            } else if (sessionStorage.getItem('taskList')) {
              const taskList = JSON.parse(sessionStorage.getItem('taskList'))
              taskList.push(this.dataForm)
              sessionStorage.setItem('taskList', JSON.stringify(taskList))
            } else {
              const arr = [this.dataForm]
              sessionStorage.setItem('taskList', JSON.stringify(arr))
            }
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
