<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">
        <search-area
          :value="code"
          :is-detail="true"
          size="mini"
          :names="label"
          placeholder="收货省市区"
          :level="3"
          @searchChange="areaChange($event)"
        />
      </el-form-item>

      <el-form-item label="详细地址" prop="addr">
        <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 {
      code: [],
      label: [],
      formData: {
        status: '4',
        remark: '',
        contactor: '',
        mobile: '',
        addr_code: '',
        addr: ''
      },
      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: '请选择省市区'
          }
        ]
      })
    }
  },
  mounted() {
  },
  methods: {
    addr_selectChange(e) {
      this.formData.addr_code = e || []
      this.$refs.formRef.validateField('addr_code')
    },
    async areaChange(e) {
      const { code, area_code, label } = e
      this.formData.addr_code = code ? code.split(',') : []
      this.code = area_code ? [area_code] : []
      this.label = label ? [label] : []
      this.$refs.formRef.validateField('addr_code')
    },
    submit() {
      this.$refs.formRef.validate(isValid => {
        if (!isValid) return
        const { status, addr_code } = this.formData
        let _status = status

        if (this.sitem.is_receive === '0' && status === '4') {
          _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>
