<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="媒体名称" prop="mediaName">
        <el-input v-model="ruleForm.mediaName" placeholder="请输入媒体名称"></el-input>
      </el-form-item>
      <el-form-item label="媒体电话" prop="mediaPhone">
        <el-input v-model="ruleForm.mediaPhone" placeholder="请输入媒体电话" onkeyup="value=value.replace(/[^\d.]/g,'')"></el-input>
      </el-form-item>
      <el-form-item label="媒体地区" >
          <el-cascader
        v-model="provinceCity"
        :options="provinceCityDatas"
        :props="props"
        placeholder="请选择地区"
        class="form_select"
        clearable
        filterable
        change-on-select
      />
        <!-- <el-input v-model="ruleForm.mediaName" placeholder="请输入媒体地区"></el-input> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getProviceCityTown } from '@/api/commonAPI'
import { getMediaDetail, addMediaManagment, editMediaManagment} from '@/api/mediaCenter/mediaInformation'
import { validateTel } from '@/utils/validate'
export default {
  name: 'AddOrEdit',
  props: {
    rowid: {
      type: String,
      default: ''
    },
    callid: {
      type: Number,
      default: 0
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      provinceCityDatas: [], // 省市下拉数据
      provinceCity: [], // 省市下拉绑定的值
      props: {
        // 自定义省市下拉数据的key值
        value: "text",
        label: "text",
        children:'children'
      },
      ruleForm: {
        unique_id: '', //		否	string	callid
        mediaName: '', //媒体名称
        mediaPhone: '', //媒体电话
      },
      rules: {
        mediaName: [{
          required: true,
          trigger: 'blur',
          message: '请输入媒体名称',
        }],
        mediaPhone: [{
          required: true,
          trigger: 'blur',
          message: '请输入媒体电话',
        }],
      },
      loading: false
    }
  },
  created() {
      this.getProCity();
    if (this.callid) {
      this.ruleForm.unique_id = this.callid
    }
    Promise.all([])
      .then(() => {
        if (this.rowid) {
          this.ruleForm.ordercode = this.rowid
          this.getDetail(this.rowid)
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          return new Promise(response=>{
            const params={
              F_ID:this.rowid?this.rowid:'',
              F_Name:this.ruleForm.mediaName,//媒体名称
              F_Telephone:this.ruleForm.mediaPhone,//媒体电话
              F_Province:this.provinceCity && this.provinceCity[0],//省
              F_City:this.provinceCity[1] && this.provinceCity[1],//市
              F_County:this.provinceCity[2] && this.provinceCity[2],//县
            }
             // 添加
            if (!this.rowid) {
              addMediaManagment(params).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.btn_search() // 重新加载父级数据
                  }
                  this.$message.success('恭喜你，添加成功!')
                }
              }).catch(() => {
                this.loading = false
              })
              return
            }
            // 编辑
            editMediaManagment(params).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() {
      this.$refs.ruleForm.resetFields()
    },
    // 详情
    getDetail(rid) {
        console.log(rid)
      getMediaDetail(rid).then(response => {
        if (response.state.toLowerCase() === 'success') {
          const res = response.data
          this.ruleForm.mediaName = res.F_Name
          this.ruleForm.mediaPhone = res.F_Telephone
          if(res.F_Province){
            this.provinceCity.push(res.F_Province)
          }
          if(res.F_City){
            this.provinceCity.push(res.F_City)
          }
          if(res.F_County){
            this.provinceCity.push(res.F_County)
          }
        }
      })
    },
    // 获取省市下拉数据
    getProCity() {
      return new Promise(resolve => {
        getProviceCityTown().then(response => {
          if (response.state.toLowerCase() === "success") {
            this.provinceCityDatas = response.data;
          }
        });
        resolve();
      });
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form {
  .form_select {
  	width: 100%;
  }
  .form_date {
    width: 100%;
  }
}
</style>