<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="仓库名称" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入仓库名称"/>
      </el-form-item>
      <el-form-item label="仓库属性" prop="phone">
        <el-input v-model="ruleForm.phone" placeholder="请输入仓库属性"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProviceCity, getDictionary } from '@/api/commonAPI'
import { getOrder, addOrder, editOrder } from '@/api/order/orderSearch'
import { validateTel } from '@/utils/validate'
import { delFiles, filterContent } from '@/utils'


export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    callid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      ruleForm: {
        unique_id: '', //		否	string	callid
        ordercode: '',
        sourceid: '', //	否	string	来源id
        typeid: '', //	否	string	类型id
        name: '', //	否	string	姓名
        phone: '', //	是	string	号码
      },
      rules: {
        name: [{
          required: true,
          message: '请输入客户姓名',
          trigger: 'blur'
        }],
      },
      loading: false
    }
  },
  created() {
    if (this.callid) {
      this.ruleForm.unique_id = this.callid
    }
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.province = this.provinceCity && this.provinceCity[0]
          this.ruleForm.city = this.provinceCity && this.provinceCity[1]
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          // 添加
          if (!this.rowid) {
            addOrder(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                if (this.callid) {
                  this.$parent.getOrderRecord() // 重新加载来电弹屏工单记录
                } else {
                  this.$parent.getList() // 重新加载父级数据
                }
                this.$message.success('恭喜你，工单信息添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editOrder(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，工单信息编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      if (!this.rowid) {
        if (this.ruleForm.files.length > 0) {
          for (let i = this.ruleForm.files.length - 1; i >= 0; i--) {
            delFiles(this.ruleForm.files[i].filesmallurl, this.ruleForm.files[i].fileurl)
            this.ruleForm.files.splice(i, 1)
          }
        }
        this.$refs.upload.clearFiles()	// 清空已上传的文件列表（该方法不支持在 before-upload 中调用）
      }
      this.provinceCity = []
      this.$refs.ruleForm.resetFields()
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select{
	width: 100%;
}
.order_form .uploadFiles .el-upload-list--picture{
	margin-right: -1%;
}
.order_form .el-upload-list--picture li{
	width: 24%;
	margin-right: 1%;
	float: left;
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>

</style>
