<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 label="修改供应商审批结果" value="10" />
        <el-option label="认同供应商审批结果" value="11" />
      </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="return_wsm">
        <search-stock
          wsmtype="5"
          :size="'mini'"
          :value="formData.return_wsm"
          placeholder="退货仓库"
          :is-detail="true"
          :is-relation="true"
          :names="wsm_code_name"
          :company-no="currentCompany"
          @searchChange="stockChange"
        />
      </el-form-item>

      <el-form-item label="">
        <el-input v-model="expressInfo" placeholder="退货仓库收货信息展示" disabled />
      </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-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 companyHelper from '@/mixins/companyHelper'
import { replaceTextWrapAndSpace } from '@/utils'
export default {
  name: 'ApprovalResults',
  mixins: [companyHelper],
  props: ['sitem'],
  data() {
    return {
      company: '',
      formData: {
        status: '10',
        remark: ''
      },
      wsm_code: '',
      wsm_name: '',
      expressInfo: ''
    }
  },
  computed: {
    displayExpressField() {
      console.log(this.sitem)
      return this.formData.status === '11'
    },
    rules() {
      const { status } = this.formData
      return ({
        status: [
          {
            required: true,
            trigger: 'change',
            message: '请选择退货仓库'
          }
        ],
        remark: [
          {
            required: status === '10',
            trigger: 'change',
            message: '请输入审核备注'
          }
        ]
      })
    }
  },
  watch: {
    'formData.status'(status) {
      // this
      this.formData = {
        status,
        remark: '',
        return_wsm: ''
      }
    }
  },
  methods: {
    async stockChange(e) {
      const { code, label, contactor, mobile, addr_cn } = e
      this.expressInfo = `${contactor}-${mobile}-${addr_cn}`
      this.wsm_name = label || ''
      this.wsm_code = code || ''
      this.formData.return_wsm = code ? [code] : []
      this.$refs.formRef.validateField('return_wsm')
    },
    submit() {
      this.$refs.formRef.validate(isValid => {
        if (!isValid) return
        const { return_wsm } = this.formData

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

        const params = {
          ...this.formData,
          return_wsm: Array.isArray(return_wsm) ? return_wsm[0] : return_wsm
        }
        this.$emit('changeStatus', params)
      })
    }
  }
}
</script>

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