<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 :span="12">
            <el-form-item label="订单编码" prop="orderCode">
              <el-input v-model="dataForm.orderCode" :disabled="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="送达时间" prop="arrivedTime">
              <el-date-picker
                v-model="dataForm.arrivedTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="datetime">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-form-item label="客户反馈" prop="customerFeedback">
            <el-input type="textarea" v-model="dataForm.customerFeedback" placeholder="客户反馈"></el-input>
          </el-form-item>
        </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" class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="dataFormSubmit()">确定</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: {},
        dataRule: {
          customerFeedback: [{ required: true, message: '客户反馈不能为空', trigger: 'blur' }],
          arrivedTime: [{ required: true, message: '送达时间不能为空', trigger: 'change' }]
        }
      }
    },
    methods: {
      async init (orderId, orderCode) {
        if (!orderId) return
        this.fileList = []
        this.dataForm = {
          orderId: orderId,
          orderCode: orderCode
        }
        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/order/arrived`),
              method: 'post',
              data: this.$http.adornData({...this.dataForm})
            }).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
