<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="wsm_code">
        <search-stock
          :size="'mini'"
          :value="formData.wsm_code"
          placeholder="退货仓库"
          :is-detail="true"
          :is-relation="true"
          :names="wsm_code_name"
          :company-no="currentCompany"
          wsmtype="4"
          @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>
</template>
<script>
import companyHelper from '@/mixins/companyHelper'
export default {
  name: 'ApprovalResults',
  mixins: [companyHelper],
  props: ['sitem'],
  data() {
    return {
      company: '',
      formData: {
        status: '10',
        remark: '',
        wsm_code: ''
      },
      wsm_code: '',
      wsm_name: '',
      wsm_code_name: '',
      expressInfo: ''
    }
  },
  computed: {
    displayExpressField() {
      return this.formData.status === '11'
    },
    rules() {
      const { status } = this.formData
      return ({
        wsm_code: [
          {
            type: 'array',
            required: true,
            trigger: 'change',
            message: '请选择退货仓库'
          }
        ],
        status: [
          {
            required: true,
            trigger: 'change',
            message: '请选择退货仓库'
          }
        ],
        remark: [
          {
            required: status === '10',
            trigger: 'change',
            message: '请输入审核备注'
          }
        ]
      })
    }
  },
  watch: {
    'formData.status'(status) {
      // this
      this.formData = {
        status,
        remark: '',
        wsm_code: ''
      }
    }
  },
  mounted() {
  },
  methods: {
    async stockChange(e) {
      const { code, label, contactor, mobile, addr_cn } = e
      this.expressInfo = `${contactor}-${mobile}-${addr_cn}`
      this.wsm_code_name = label || ''
      this.wsm_code = code || ''
      this.formData.wsm_code = code ? [code] : []
      this.$refs.formRef.validateField('wsm_code')
    },

    submit() {
      this.$refs.formRef.validate(isValid => {
        if (!isValid) return

        const { status } = this.formData
        let _status = status

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

        const { wsm_code } = this.formData
        const params = {
          ...this.formData,
          status: _status,
          wsm_code: Array.isArray(wsm_code) ? wsm_code[0] : wsm_code
        }

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

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