<template>
  <el-dialog
    v-loading="loading || detailLoading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="500px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="handleClose"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :rules="rules"
            size="mini"
          >
            <el-form-item label="发货单编号:" prop="outCode">
              <el-input v-model="ruleForm.outCode" placeholder="发货单编号" />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="24" style="text-align: right">
          <el-button
            v-if="id !== '007'"
            type="primary"
            @click="submitForm"
          >保 存</el-button>
          <el-button @click="showModelThis = false">
            {{ id == "007" ? "关 闭" : "取 消" }}
          </el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from '@/apis/service/deliveryWorkOrderSupplement/deliveryWorkOrderSupplement'
import companyHelper from '@/mixins/companyHelper'
import resToken from '@/mixins/resToken'

const defaultData = { outCode: '' }

export default {
  name: 'Terrace',
  mixins: [resToken, companyHelper],
  props: ['showModel', 'id', 'sitem'],
  data() {
    return {
      loading: false,
      detailLoading: false,
      title: '发货工单补录',
      khName: '',
      accounts: [],
      cat_id_name: '',
      showModelThis: this.showModel,
      pickerOptions: {
        disabledDate: (time) => time.getTime() > new Date()
      },
      names: [],
      unit_name: '',
      companyName: '',
      ruleForm: { ...defaultData },
      rules: {
        outCode: [
          {
            required: true,
            message: '请输入发货单编号',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  watch: {
    showModel: function(val) {
      this.showModelThis = val
    },
    showModelThis(val) {
      if (!val) {
        this.$refs.ruleForm.resetFields()
        this.$emit('cancel')
      }
    }
  },
  methods: {
    async submitForm() {
      await this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          if (this.loading) {
            return
          }

          const res = await asyncRequest.supplement(this.ruleForm)
          this.loading = false
          if (res && res.code === 0) {
            const title = '添加成功'
            this.$notify.success({
              title,
              message: ''
            })
            this.showModelThis = false
            // 刷新
            this.$emit('refresh')
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout()
          } else {
            this.$message.warning(res.message)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.terrace {
}
</style>
