import AntModal from '@/components/pt/dialog/AntModal'
import { getCargoDetail, addCargoDetail, updateCargoDetail } from '@/api/system/cargoDetail'

export default {
  name: 'CreateForm',
  props: {
    typeOptions: {
      type: Array,
      required: true
    },
    statusOptions: {
      type: Array,
      required: true
    },
    thdmOptions: {
      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: '添加X光机难以辨识货物清单',
      // 表单参数
      form: {
        status: '1'
      },
      rules: {
        keywords: [{ 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,
        type: undefined,
        keywords: undefined,
        specialCode: undefined,
        status: undefined
      }
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset()
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset()
      this.open = true
      this.spinning = !this.spinning
      const cargoDetailId = row.id
      getCargoDetail(cargoDetailId).then(response => {
        this.form = response.data
        this.formTitle = '修改X光机难以辨识货物清单'
        this.spinning = !this.spinning
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.type = 1
      this.$refs.form.validate(valid => {
        if (valid) {
            const saveForm = JSON.parse(JSON.stringify(this.form))
			if (this.form.id !== undefined) {
				updateCargoDetail(saveForm).then(response => {
					this.$message.success('修改成功', 3)
					this.open = false
					this.$emit('ok')
					this.$emit('close')
				})
              } else {
				addCargoDetail(saveForm).then(response => {
					this.$message.success('新增成功', 3)
					this.open = false
					this.$emit('ok')
					this.$emit('close')
				})
			}
        } else {
          return false
        }
      })
    },
    back () {
      const index = '/system/cargodetail/index'
      this.$router.push(index)
    }
  }
}
