import AntModal from '@/components/pt/dialog/AntModal'
import { getWaybill, addWaybill, updateWaybill } from '@/api/system/waybill'

export default {
  name: 'CreateForm',
  props: {
    statusOptions: {
      type: Array,
      required: true
    } },
  components: {
    AntModal },
  data () {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: '添加运单管理',
      // 表单参数
      form: {},
      rules: {
        waybillNo: [{ required: true, message: '运单号不能为空', trigger: 'blur' }],
        dataSource: [{ required: true, message: '运单来源不能为空', trigger: 'blur' }],
        flightNo: [{ required: true, message: '航班号不能为空', trigger: 'blur' }],
        flightType: [{ required: true, message: '航班类型不能为空', trigger: 'blur' }],
        flightDate: [{ required: true, message: '航班日期不能为空', trigger: 'blur' }],
        agentCode: [{ required: true, message: '交货或发货代理人编码不能为空', trigger: 'blur' }],
        goodsName: [{ required: true, message: '品名不能为空', trigger: 'blur' }],
        agentLevel: [{ required: true, message: '代理人信用等级', trigger: 'blur' }]
      }
    }
  },
  filters: {},
  created () {},
  computed: {},
  watch: {},
  mounted () {},
  methods: {
    onClose () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
      this.$emit('close')
    },
    // 表单重置
    reset () {
      this.form = {
        id: undefined,
        waybillNo: undefined,
        dataSource: undefined,
        flightNo: undefined,
        flightType: undefined,
        flightDate: undefined,
        organization: undefined,
        agentCode: undefined,
        goodsName: undefined,
        goodsType: undefined,
        specialGoodsCode: undefined,
        diffProductSn: undefined,
        shipper: undefined,
        origin: undefined,
        destination: undefined,
        carrierCode: undefined,
        isImportantFlights: undefined,
        agentLevel: undefined,
        riskClassification: undefined,
        riskRating: undefined,
        status: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.open = true
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const waybillId = row.id
      getWaybill(waybillId).then(response => {
        this.form = response.data
        this.formTitle = '修改运单管理'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
            const saveForm = JSON.parse(JSON.stringify(this.form))
			if (this.form.id !== undefined) {
				updateWaybill(saveForm).then(response => {
					this.$message.success('修改成功', 3)
					this.open = false
					this.$emit('ok')
					this.$emit('close')
				})
              } else {
				addWaybill(saveForm).then(response => {
					this.$message.success('新增成功', 3)
					this.open = false
					this.$emit('ok')
					this.$emit('close')
				})
			}
        } else {
          return false
        }
      })
    },
    back () {
      const index = '/system/waybill/index'
      this.$router.push(index)
    }
  }
}
