<template>
  <el-form ref="formRef" class="supplier-form" size="mini" label-width="80px" :model="formData" :rules="rules">
    <el-form-item label="审核状态" prop="status">
      <el-select v-model="formData.status" style="width:100%">
        <el-option v-for="opt in statusOptions" :key="opt.value" :value="opt.value" :label="opt.label" />
      </el-select>
    </el-form-item>
    <el-form-item label="审核备注" prop="remark">
      <el-input v-model="formData.remark" type="textarea" />
    </el-form-item>

    <template v-if="displayExpressField">
      <el-form-item label="收货人" prop="contactor">
        <el-input v-model="formData.contactor" />
      </el-form-item>
      <el-form-item label="联系电话" prop="mobile">
        <el-input v-model="formData.mobile" :maxlength="11" />
      </el-form-item>
      <el-form-item label="退货地址" prop="addr_code">
        <select-area
          :placeholder="'省/市/区'"
          :value="formData.addr_code"
          :is-detail="true"
          :size="'mini'"
          @selectChange="addr_selectChange"
        />
      </el-form-item>

      <el-form-item prop="addr" label="详细地址">
        <el-input v-model="formData.addr" placeholder="详细地址" />
      </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 { replaceTextWrapAndSpace } from '@/utils'
import { hasSpace, isAddr, isMobile, isSpecialSymbol } from '@/utils/validate'

const validatename = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('收件人不能为空！'))
  } else {
    callback()
  }
}
const validatemobile = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('手机号不能为空！'))
  } else {
    if (!isMobile(value)) {
      callback(new Error('手机号格式不正确！'))
    } else {
      callback()
    }
  }
}

const validateAddr = (rule, value, callback) => {
  if (value === '') {
    callback(new Error('详细地址不能为空！'))
  } else {
    if (hasSpace(value)) {
      callback(new Error('不能出现回车/换行符！'))
    } else if (isSpecialSymbol(value)) {
      callback(new Error('不能使用英文特殊字符！'))
    } else if (isAddr(value)) {
      callback()
    } else {
      callback(new Error('详细地址填写不规范！'))
    }
  }
}
export default {
  name: 'SupplierExam',
  mixins: [],
  props: ['sitem'],
  data() {
    return {
      formData: {
        status: '4',
        remark: '',
        addr_code: '',
        mobile: '',
        contactor: ''
      },
      statusOptions: [
        { value: '4', label: '确认修改,供应商同意退货' },
        { value: '2', label: '不同意修改,希望业务公司承担' }
      ]
    }
  },
  computed: {
    displayExpressField() {
      return this.formData.status === '4' && (this.sitem && this.sitem.is_receive !== '0')
    },
    rules() {
      const { status } = this.formData
      return ({
        addr: [
          {
            required: true,
            trigger: 'change',
            validator: validateAddr
            // message: '想输入详细地址'
          }
        ],
        status: [
          {
            required: true,
            trigger: 'change',
            message: '请选择审核状态'
          }
        ],
        remark: [
          {
            required: status === '2',
            trigger: 'change',
            message: '请输入备注'
          }
        ],
        contactor: [
          {
            required: true,
            trigger: 'blur',
            validator: validatename
          }
        ],
        mobile: [
          {
            required: true,
            trigger: 'blur',
            validator: validatemobile
          }
        ],
        addr_code: [
          {
            type: 'array',
            required: true,
            trigger: 'change',
            message: '请选择省市区'
          }
        ]
      })
    }
  },
  methods: {
    addr_selectChange(e) {
      this.formData.addr_code = e || []
      this.$refs.formRef.validateField('addr_code')
    },
    submit() {
      this.$refs.formRef.validate(isValid => {
        if (!isValid) return
        const { addr_code, status } = this.formData
        let _status = status

        if (status === '4' && String(this.sitem.is_receive) === '0') {
          _status = '5'
        }

        this.formData.remark = replaceTextWrapAndSpace(this.formData.remark)

        const params = {
          ...this.formData,
          status: _status,
          addr_code: addr_code ? addr_code.join(',') : ''
        }

        this.$emit('changeStatus', params)
      })
    }
  }
}
</script>

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