<template>
  <el-form ref="formRef" class="supplier-form" size="mini" label-width="80px" :model="formData" :rules="rules">
    <!-- <el-form-item label="是否退货" prop="is_th">
      <el-select v-model="formData.is_th" style="width:100%">
        <el-option label="是" value="1" />
        <el-option label="否" value="2" />
      </el-select>
    </el-form-item> -->

    <template v-if="displayExpressField">
      <el-form-item label="物流公司" prop="company">
        <search-express
          :value="formData.company"
          :placeholder="'物流公司'"
          :names="''"
          :order_source="order_source"
          :is-detail="false"
          size="mini"
          @searchChange="handleCompany"
        />
      </el-form-item>

      <el-form-item label="物流单号" prop="post_code">
        <el-input v-model="formData.post_code" placeholder="物流单号" />
      </el-form-item>

      <el-form-item label="物流费用" prop="post_fee">
        <digital-input
          :values="formData.post_fee"
          :placeholder="'物流费用'"
          :min="0"
          :disabled="type === 'view' || type === 'post_fee'"
          :max="100000000000"
          :position="'right'"
          :precision="2"
          :size="'mini'"
          :controls="false"
          :append="'元'"
          @reschange="number_change($event, 'post_fee')"
        />
      </el-form-item>
    </template>

    <el-form-item>
      <div style="width:100%;display:flex;justify-content:flex-end">
        <el-button type="primary" @click="submit">提交</el-button>
      </div>
    </el-form-item>
  </el-form>
</template>
<script>
import { isnumber, isNumeric } from '../../../../utils/validate'

const validateCode = (rule, value, callback) => {
  const { required } = rule
  const l = value.length
  if (required) {
    if (value === '') {
      callback(new Error('物流单号不能为空!'))
    } else if (l < 9 || l > 20) {
      callback(new Error('仅支持纯数字或字母数字组合(9~20位)!'))
    } else if (isnumber(value)) {
      callback()
    } else if (!isNumeric(value)) {
      console.log(!isNumeric(value))
      callback(new Error('仅支持纯数字或字母数字组合(9~20位)!'))
    } else {
      callback()
    }
  } else {
    callback()
  }
}

export default {
  name: 'SupplierExam',
  mixins: [],
  props: [],
  data() {
    return {
      company: '',
      formData: {
        is_th: '1',
        company: '',
        post_fee: '',
        post_code: ''
      },
      statusOptions: [
        { value: '4', label: '通过' }, // -> 待客户退货
        { value: '2', label: '驳回' } // -> 待采购审核
      ]
    }
  },
  computed: {
    displayExpressField() {
      return this.formData.is_th === '1'
    },
    rules() {
      return ({
        company: [
          {
            type: 'array',
            required: true,
            trigger: 'change',
            message: '请选择物流公司'
          }
        ],
        post_code: [
          {
            required: true,
            validator: validateCode,
            trigger: 'blur'
          }
        ],
        post_fee: [
          {
            required: true,
            trigger: 'change',
            message: '请输入快递费用'
          }
        ]
      })
    }
  },
  mounted() {
  },
  methods: {
    handleCompany(e) {
      if (e && e.code) {
        this.company = e.label
        this.formData.company = [e.code]
      }
      this.$refs.formRef.validateField('company')
    },
    addr_selectChange(e) {
      this.formData.addr_code = e || []
      this.$refs.formRef.validateField('addr_code')
    },
    submit() {
      this.$refs.formRef.validate(isValid => {
        if (!isValid) return

        const params = {
          ...this.formData,
          company: this.company
        }

        this.$emit('changeStatus', params)
      })
    },
    number_change(e, key) {
      this.formData[key] = e + '' || '0'
      this.$refs.formData.validateField(key)
    }
  }
}
</script>

<style lang="scss" scoped>
.supplier-form{
  ::v-deep(.el-form-item){
    width: 600px;
  }
}
</style>
