<template>
  <div>
    <el-dialog
      title="任务单送达"
      width="50%"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <!-- 表单 -->
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <el-row class="my-row">
          <el-col>
            <el-form-item label="送达数量" prop="cnt">
              <el-input-number v-model="dataForm.cnt" :min="0" :max="Number(max)"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <upload-component :display="false" :title="'签收附件'" :accept="'*'" :file-obj-list="fileList" @uploadSuccess="uploadSuccess"/>
        </el-row>
      </el-form>
      <span slot="footer">
          <el-button @click="onChose">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import UploadComponent from '../common/upload-component'

export default {
    name: 'dispatch-arrived',
    components: { UploadComponent },
    data () {
      return {
        visible: false,
        fileList: [],
        dataForm: {},
        max: 0,
        dataRule: {
          cnt: [{ required: true, message: '请输入送达数量', trigger: 'change' }]
        }
      }
    },
    methods: {
      onChose () {
        this.visible = false
        this.$emit('onChose')
      },
      async init (deliverId, max) {
        if (!deliverId) return
        this.max = max
        this.fileList = []
        this.dataForm = {
          deliverId: deliverId
        }
        this.visible = true
      },
      uploadSuccess (fileList) {
        this.fileList = fileList
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            // 附件检查
            let fList = this.fileList
            if (fList.length > 0) {
              this.dataForm.attachList = []
              for (let i = 0; i < fList.length; i++) {
                this.dataForm.attachList.push({
                  fileName: fList[i].name,
                  url: fList[i].url
                })
              }
            } else {
              this.$message.error('请上传文件')
              return
            }
            this.$http({
              url: this.$http.adornUrl(`/biz-service/deliver/arrive`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.onChose()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
