<template>
  <div>
    <el-button size="mini" type="primary"   @click="show" >编 辑</el-button>
    <el-drawer
        title="编辑竞争对手"
        :visible.sync="dialogFormVisible"
        size="600px"
        direction="rtl"
        append-to-body
        @close="dialogFormVisible = false">
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules" ref="form" label-width="120px" label-position="right" size="mini">
            <el-col :span="24">
              <el-form-item label="品牌名称:" prop="brandname">
                <el-input v-model="form.brandname" placeholder="请输入品牌名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="企业名称:" prop="enterprisename">
                <el-popover
                    placement="bottom"
                    width="100%"
                    v-model="visible">
                  <div>
                    <ul class="enterprisePanel">
                      <li v-for="item in ENlist" :key="item.sys_enterpriseid" @click="chooseEnterprise(item)">
                        <p>{{item.enterprisename}}</p>
                        <small>{{item.province}}-{{item.city}}-{{item.county}}</small>
                      </li>
                    </ul>
                  </div>
                  <el-input slot="reference" v-model="form.enterprisename" @focus="queryEnterpriseArchives" @input.native="onChange"  placeholder="输入企业名称" clearable></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="优势信息:" >
                <el-input type="textarea" rows="5" v-model="form.advantage" placeholder="请输入优势信息"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="劣势信息:" >
                <el-input type="textarea" rows="5" v-model="form.inferiority" placeholder="请输入劣势信息"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="备注:" >
                <el-input type="textarea" rows="5" v-model="form.remarks" placeholder="请输入备注信息"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div class="fixed__btn__panel">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit" class="normal-btn-width">确 定</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
export default {
  name: "edit",
  props:['data','btnType'],
  data(){
    return {
      dialogFormVisible:false,
      form:{
        sa_competitorid:'',
        sys_enterpriseid:"",
        enterprisename:"",
        brandname:"",
        inferiority:"",
        advantage:"",
        remarks:""
      },
      rules:{
        brandname: [
          { required: true, message: '请输入品牌名称', trigger: 'blur' },
        ],
        enterprisename: [
          { required: true, message: '请输入企业名称', trigger: 'blur,change' },
        ]
      },
      visible:false,
      ENlist:[],
    }
  },
  methods:{
    show(){
      this.dialogFormVisible = true
      this.form = this.data
    },
    onSubmit(){
      if (this.form.sys_enterpriseid === ''){
        this.form.sys_enterpriseid = 0
      }
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": 20221018164102,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields()
          this.dialogFormVisible = false
        })
      })
    },
    async queryEnterpriseArchives () {
      const res = await this.$api.requested({
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
            "condition": this.form.enterprisename
          }
        }
      })
      this.ENlist = res.data
    },
    onChange () {
      this.visible = true
      this.debounce(this.queryEnterpriseArchives,500)()
    },
    debounce (fn, wait) {
      let timer = 0
      return function () {
        if (timer !== null) clearTimeout(timer)
        timer = setTimeout(fn, wait)
      }
    },
    chooseEnterprise (item) {
      this.visible = false
      this.form.enterprisename = item.enterprisename
      this.form.sys_enterpriseid = item.sys_enterpriseid

      item.province = `${item.province}-${item.city}-${item.county}`
      this.enterprise = item
    },
  }
}
</script>

<style scoped>
.enterprisePanel {
  max-height: 400px;
  overflow-y: scroll;
}
.enterprisePanel li{
  padding: 10px;
  cursor: pointer;
  line-height: 25px;
}
.enterprisePanel li small{
  color: #999;
}
.enterprisePanel li:hover{
  background: #f1f2f3;
}
</style>