<template>
  <div >
    <el-button @click="editBtn" :type="disabled?'':'primary'" :disabled="disabled" size="mini">编 辑</el-button>
    <el-drawer size="40%" :with-header="false" append-to-body :visible.sync="drawer" v-if="form.sa_contractid">
      <p class="normal-title normal-panel" style="border-bottom:1px solid #cccccc;padding:16px">编辑{{$route.query.type == '框架' ? '经销商合作' : $route.query.type}}合同</p>
      <div class="container normal-panel">
        <!--合作协议-->
        <el-row v-if="$route.query.type == '框架'">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" label-width="100px" size="small">
            <el-col :span="24">
              <el-form-item label="标题:" prop="title">
                <el-input type="text" size="small" v-model="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="经销商:" prop="enterprisename">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="agentVisible"
                  width="400">
                  <selectAgent ref="member" :param="agentListPrams" :radio="true" @onSelect="agentChange" @onCancel="agentVisible = false"></selectAgent>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('agentVisible')" v-model="form.enterprisename"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="业务员:" prop="salername">
                  <el-input disabled type="text" size="small" v-model="form.salername"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="折扣(%):" prop="discountrate">
                <el-input type="text" size="small" v-model="form.discountrate"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同有效期:" prop="begdate">
                <el-input style="display:none" v-model="form.begdate"></el-input>
                <time-select ref="timeSelect" class="inline-16" @clearSelect="clearTime" @timeChange="timeChange"></time-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约日期:" prop="signdate">
                <el-date-picker
                  v-model="form.signdate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同模板:">
                <el-input disabled type="text" size="small" :value="$route.query.type == '框架' ? '经销合作' : $route.query.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同描述:" prop="remarks">
                <el-input type="textarea" size="small" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <!--经销项目-->
        <el-row v-if="$route.query.type == '经销项目'">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" label-width="100px" size="small">
            <el-col :span="24">
              <el-form-item label="标题:" prop="title">
                <el-input type="text" size="small" v-model="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="经销商:" prop="enterprisename">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="agentVisible"
                  width="400">
                  <selectAgent ref="member" :param="agentListPrams" :radio="true" @onSelect="agentChange" @onCancel="agentVisible = false"></selectAgent>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('agentVisible')" v-model="form.enterprisename"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目名称:" prop="sa_projectid">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="projectVisible"
                  width="600">
                  <seleteProject ref="project" type="1" :sys_enterpriseid='form.sys_enterpriseid' @onSelect="projectChange" @onCanel="projectVisible=false"></seleteProject>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('projectVisible')" v-model="form.projectname"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="业务员:" prop="salername">
                  <el-input disabled type="text" size="small" v-model="form.salername"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同有效期:" prop="begdate">
                <input type="text" style="display:none" v-model="form.begdate">
              <time-select ref="timeSelect" class="inline-16" @clearSelect="clearTime" @timeChange="timeChange"></time-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约日期:" prop="signdate">
                <el-date-picker
                  v-model="form.signdate"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约金额(元):">
                <el-input type="text" disabled size="small" placeholder="产品清单总额(自动计算)" v-model="form.signamount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同模板:">
                <el-input disabled type="text" size="small" :value="$route.query.type == '框架' ? '经销合作' : $route.query.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同描述:" prop="hrid">
                <el-input type="textarea" size="small" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <!--直销项目-->
        <el-row v-if="$route.query.type == '直销项目'">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" label-width="100px" size="small">
            <el-col :span="24">
              <el-form-item label="标题:" prop="title">
                <el-input type="text" size="small" v-model="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="客户" prop="enterprisename">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="accountVisible"
                  width="400">
                  <selectAgent ref="member" :param="agentListPrams" :radio="true" @onSelect="accountChange" @onCancel="accountVisible = false"></selectAgent>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('accountVisible')" v-model="form.enterprisename"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目:" prop="sa_projectid">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="projectVisible"
                  width="600">
                  <seleteProject type="1" @onSelect="projectChange" @onCanel="projectVisible=false"></seleteProject>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('projectVisible')" v-model="form.projectname"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目地址:" prop="">
                <el-input disabled type="text" size="small" v-model="address"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="业务员:" prop="salername">
                  <el-input disabled type="text" size="small" v-model="form.salername"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收货人:" prop="projectname">
                <el-popover
                    placement="bottom"
                    trigger="manual"
                    v-model="receiptVisible"
                    width="600">
                  <seleteReceipt ref="receipt" type="1" @onSelect="receiptChange" @onCanel="receiptVisible = false"></seleteReceipt>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('receiptVisible')" v-model="form.name"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="收货人电话:" prop="">
                <el-input disabled type="text" size="small" v-model="form.phonenumber"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约日期:" prop="signdate">
                <el-date-picker
                  v-model="form.signdate"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同有效期:" prop="begdate">
                <input type="text" style="display:none" v-model="form.begdate">
              <time-select ref="timeSelect" class="inline-16" @clearSelect="clearTime" @timeChange="timeChange"></time-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约金额(元):">
                <el-input type="text" disabled size="small" placeholder="产品清单总额(自动计算)" v-model="form.signamount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同模板:">
                <el-input disabled type="text" size="small" :value="$route.query.type == '框架' ? '经销合作' : $route.query.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同描述:" prop="hrid">
                <el-input type="textarea" size="small" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <!--工具使用协议-->
        <el-row v-if="$route.query.type == '工具借用'">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" label-width="100px" size="small">
            <el-col :span="24">
              <el-form-item label="标题:" prop="title">
                <el-input type="text" size="small" v-model="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="客户" prop="enterprisename">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="accountVisible"
                  width="400">
                  <selectAgent ref="member" :param="agentListPrams" :radio="true" @onSelect="accountChange" @onCancel="accountVisible = false"></selectAgent>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('accountVisible')" v-model="form.enterprisename"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="业务员:" prop="salername">
                  <el-input disabled type="text" size="small" v-model="form.salername"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约日期:" prop="signdate">
                <el-date-picker
                  v-model="form.signdate"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同有效期:" prop="begdate">
                <input type="text" style="display:none" v-model="form.begdate">
              <time-select ref="timeSelect" class="inline-16" @clearSelect="clearTime" @timeChange="timeChange"></time-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="单套价格(元):">
                <el-input type="text" readonly size="small" placeholder="系统自动计算" v-model="form.signamount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同模板:">
                <el-input disabled type="text" size="small" :value="$route.query.type == '框架' ? '经销合作' : $route.query.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同描述:" prop="hrid">
                <el-input type="textarea" size="small" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <!--居间协议-->
        <el-row v-if="$route.query.type == '居间'">
          <el-form :model="form" :rules="rules" ref="form" label-position="right" label-width="135px" size="small">
            <el-col :span="24">
              <el-form-item label="标题:" prop="title">
                <el-input type="text" size="small" v-model="form.title"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="居间服务商:" prop="sys_enterpriseid">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="accountVisible"
                  width="400">
                  <selectAgent ref="member" :param="agentListPrams" :radio="true" @onSelect="accountChange" @onCancel="accountVisible = false"></selectAgent>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('accountVisible')" v-model="form.enterprisename"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目合同:" prop="ascription_title">
                <el-popover
                  placement="bottom"
                  trigger="manual"
                  v-model="concratVisible"
                  width="600">
                  <seleteProjectContract ref="projectContract" @onSelect="concratChange" @onCanel="concratVisible=false"></seleteProjectContract>
                  <el-input readonly type="text" slot="reference" size="small" @focus="showChange('concratVisible')" v-model="form.ascription_title"></el-input>
                </el-popover>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="项目名称:" prop="sa_projectid">
                <el-input disabled type="text" size="small" v-model="form.projectname"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="业务员:" prop="salername">
                  <el-input disabled type="text" size="small" v-model="form.salername"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="签约日期:" prop="signdate">
                <el-date-picker
                  v-model="form.signdate"
                  value-format="yyyy-MM-dd"
                  type="date"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="计算方式">
                <el-radio-group v-model="form.calculatemodel">
                  <el-radio :label="1">按订单金额比例计算</el-radio>
                  <el-radio :label="2">按居间产品折扣计算</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.calculatemodel==1">
              <el-form-item label="订单金额比例(%):" prop="orderratio" >
                <el-input type="text" size="small" v-model.number="form.orderratio"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24" v-if="form.calculatemodel==2">
              <el-form-item label="居间产品折扣(%):" prop="productdiscount">
                <el-input type="text" size="small" v-model.number="form.productdiscount"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同有效期:" prop="begdate">
                <input type="text" style="display:none" v-model="form.begdate">
              <time-select ref="timeSelect" class="inline-16" @clearSelect="clearTime" @timeChange="timeChange"></time-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同模板:">
                <el-input disabled type="text" size="small" :value="$route.query.type == '框架' ? '经销合作' : $route.query.type"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="合同描述:" prop="hrid">
                <el-input type="textarea" size="small" v-model="form.remarks"></el-input>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
        <div class="fixed__btn__panel">
          <el-button size="small" @click="onCancel" 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>
import selectPople from './selectSaler'
import selectAgent from './selectAgent'
import seleteProject from './seleteProject'
import selectModel from './seleteModel'
import TimeSelect from './TimeSelect'
import seleteProjectContract from './seleteProjectContract'
import seleteReceipt from './seleteReceipt'
import { log } from '@antv/g2plot/lib/utils'

export default {
  props:['data','disabled'],
  name: '',
  components:{selectPople,TimeSelect,seleteProject,selectAgent,selectModel,seleteProjectContract,seleteReceipt},
  data () {
    var checkNumber = (rule, value, callback) => {
      if (typeof +value != 'number') {
        return callback(new Error('请填写数字'));
      }
      callback()
    }
    var NumberSize = (rule, value, callback) => {
      if (+value > 100 || +value <= 1 && value) {
        return callback(new Error('折扣范围(1-100)'));
      }
      callback()
    }
    var checkTimer = (rule, value, callback) => {
      if (new Date(this.form.enddate).getTime() > new Date(value).getTime() && new Date(this.form.begdate).getTime() < new Date(value).getTime()) {
        callback()
      } {
        return callback(new Error('签约日期必须在合同有效期内'));
      }
    }
    return {
      drawer:false,
      agentVisible:false,
      salerVisible:false,
      projectVisible:false,
      accountVisible:false,
      modelVisible:false,
      concratVisible:false,
      receiptVisible:false,
      address:'',
      /* 新建居间项目时 所关联的居间商 */
      form: {
        "sa_contractid": 0,
        "sys_enterpriseid": "",
        "enterprisename":'',
        "projectname":'',
        "begdate": "",
        "enddate": "",
        "sa_projectid": "", //可选
        "type": "", //可选
        "discountrate": '', //可选
        "saler_hrid": '', //可选
        "signdate": "", //可选
        "signby": "", //可选
        "kickback": "", //可选
        "remarks": "", //可选
        "title": "",
        "signamount":'',
        "salername":'',
        "enterprisename":'',
        "sys_printmodelid": '',//可选
        "typemx":"",//可选
        "calculatemodel":1,
        "productdiscount":'',
        "orderratio":'',
        "ascription_title":'',
        'ascription_contractid':'',
        'name':'',
        "rec_contactsid":'',//收货联系人
        'phonenumber':''
      },
      rules: {
        title: [
          { required: true, message: '请填写标题', trigger: 'blur' },
        ],
        sys_enterpriseid: [
          { required: true, message: '请选择经销商', trigger: 'blur' },
        ],
        ascription_title: [
          { required: true, message: '请选择项目合同', trigger: 'change' },
        ],
        begdate: [
          { required: true, message: '请选择生效日期', trigger: 'blur' },
        ],
        sa_projectid: [
          { required: true, message: '请选择项目', trigger: 'change' },
        ],
        signamount: [
          { required: true, message: '请填写签约金额', trigger: 'change' },
          { validator:checkTimer, trigger:'change'}
        ],
        salername: [
          { required: true, message: '请选择业务员', trigger: 'change' },
        ],
        enterprisename: [
          { required: true, message: '请选择', trigger: 'change' },
        ],
        signdate: [
          { required: true, message: '请填写签约时间', trigger: 'change' },
        ],
        productdiscount: [
          { message: '必须为数字', type:'number', trigger: 'change' },
          { required: true, message: '请填写折扣', trigger: 'blur' },
        ],
        orderratio: [
          { message: '必须为数字', type:'number', trigger: 'change' },
          { required: true, message: '请填写折扣', trigger: 'blur' },
        ],
        enterprisename: [
          { required: true, message: '请选择经销商', trigger: 'change' },
        ],
        discountrate: [
          { validator: checkNumber, trigger: 'blur'},
          { validator: NumberSize, trigger: 'blur'}
        ],
      },
      
      salerListParams: {
        "id": 20221122153902,
        "content": {
            "pageNumber": 1,
            "pageSize": 20,
            "where": {
                "condition": ""
            }
        },
      },
      agentListPrams: {
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
              "condition": "",
              "type":1,
          }
        }
      },
      accountListPrams: {
        "id": 20220920083901,
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "where": {
              "condition": "",
              "type":2,
          }
        }
      }
    };
  },
  computed: {
  },
  watch: {
    drawer (val) {
      if(!val) {
        this.showChange(false)
      } else {
        if(this.$refs.project) {
          this.$refs.project.params.content.type = this.$route.query.type == '经销项目' ? '2' : '1'
          this.$refs.project.queryEnterpriseArchives()
        }
      }
    }
  },
  created () {
  },
  mounted () {
  },
  methods: {
    editBtn () {
      this.form = Object.assign({},this.form,this.data)
      this.address = this.form.province + this.form.city + this.form.county + this.form.address
      this.drawer = true
      this.$nextTick(() => {
        this.$refs.timeSelect.time = [this.form.begdate,this.form.enddate]
      })
      console.log(this.form);
      
    },
    async onSubmit () {
      this.$refs.form.validate(async val => {
        if (val) {
          if(this.$route.query.type == '居间') {
            /* 当合同关系发生改变时 */
            if (this.form.sys_enterpriseid != this.data.sys_enterpriseid || this.form.ascription_contractid != this.data.ascription_contractid) {
              /* 项目商机中是否存在该居间商 */
              let isChange = this.$refs.projectContract.ENlist
                .find(item => item.sa_contractid == this.form.ascription_contractid).parties
                .some(item2 => item2.sys_enterpriseid == this.form.sys_enterpriseid),

                contract = this.$refs.projectContract.ENlist
                .find(item => item.sa_contractid == this.form.ascription_contractid)
              if (isChange) {
                this.$confirm('当前项目已存在此居间商，请前往项目商机维护','提示',{
                  confirmButtonText:'确定',
                  cancelButtonText:'取消'
                }).then(() => {
                  JSON.parse(sessionStorage.getItem('module_info')).forEach(item1 => {
                    item1.modules.forEach(item2 => {
                      if(item2.systemmodulename == "项目管理") {
                        sessionStorage.setItem('active_modules',JSON.stringify(item2))
                        this.$store.dispatch('DrawerShowChange',false)
                        this.$router.push({path:'/projectChangeDetail',query:{id:contract.sa_projectid}})
                        return
                      }
                    })
                  })
                })
              } else {
                if (this.$route.query.type == '直销项目') {
                  this.form.type = '项目'
                  this.form.typemx = '直销'
                } else if (this.$route.query.type == '经销项目') {
                  this.form.type = '项目'
                  this.form.typemx = '经销'
                } else {
                  this.form.type = this.$route.query.type
                }
                this.form.discountrate ? this.form.discountrate =  (parseInt(this.form.discountrate) / 100).toFixed(4) : ''
                this.form.productdiscount ? this.form.productdiscount =  (parseInt(this.form.productdiscount) / 100).toFixed(4) : ''
                this.form.orderratio ? this.form.orderratio =  (parseInt(this.form.orderratio) / 100).toFixed(4) : ''
                let res = await this.$api.requested({
                  "id":20221121185302,
                  "content": this.form
                })
                this.tool.showMessage(res,() => {
                  this.$refs.form.resetFields()
                  this.drawer = false
                  this.$emit('onSuccess')
                })
              }
                                                                    
            }
          } else {
            if (this.$route.query.type == '直销项目') {
              this.form.type = '项目'
              this.form.typemx = '直销'
            } else if (this.$route.query.type == '经销项目') {
              this.form.type = '项目'
              this.form.typemx = '经销'
            } else {
              this.form.type = this.$route.query.type
            }
            this.form.discountrate ? this.form.discountrate =  (parseInt(this.form.discountrate) / 100).toFixed(4) : ''
            this.form.productdiscount ? this.form.productdiscount =  (parseInt(this.form.productdiscount) / 100).toFixed(4) : ''
            this.form.orderratio ? this.form.orderratio =  (parseInt(this.form.orderratio) / 100).toFixed(4) : ''
            let res = await this.$api.requested({
              "id":20221121185302,
              "content": this.form
            })
            this.tool.showMessage(res,() => {
              this.$refs.form.resetFields()
              this.drawer = false
              this.$emit('onSuccess')
            })
          }
        }
        
      })
    },
    showChange (key) {
      console.log(key,"编辑")
      this.agentVisible = false
      this.salerVisible = false
      this.projectVisible = false
      this.accountVisible = false
      this.modelVisible = false
      this.concratVisible = false
     /* key ? this[key] = true : ''
      if (key === 'projectVisible'){
        this.$refs.project.params.content.sys_enterpriseid = this.form.sys_enterpriseid
        this.$refs.project.queryEnterpriseArchives()
        this.$refs.form.validateField("enterprisename")
        this.$refs.form.validateField("sys_enterpriseid")
      }*/
      if (key === 'projectVisible'){
        if (this.form.sys_enterpriseid !== ''){
          this.$refs.project.params.content.sys_enterpriseid = this.form.sys_enterpriseid
          this.$refs.project.queryEnterpriseArchives()
          key ? this[key] = true : ''
        }else {
          this.$refs.form.validateField("enterprisename")
          this.$refs.form.validateField("sys_enterpriseid")
        }

      }else if (key === 'receiptVisible'){
        if (this.form.sys_enterpriseid !== ''){
          this.$refs.receipt.params.content.sys_enterpriseid = this.form.sys_enterpriseid
          this.$refs.receipt.queryReceipts()
          key ? this[key] = true : ''
        }else {
          this.$refs.form.validateField("enterprisename")
          this.$refs.form.validateField("sys_enterpriseid")
        }
      }else {
        key ? this[key] = true : ''
      }
    },
    onCancel () {
      this.drawer = false
      this.$refs.form.resetFlieds()
    },
    agentChange (data) {
      this.form.sys_enterpriseid = data[0].sys_enterpriseid
      this.form.enterprisename = data[0].enterprisename   
      this.agentVisible = false 
    },
    salerChange (data) {
      this.form.saler_hrid = data[0].hrid
      this.form.salername = data[0].name   
      this.salerVisible = false 
    },
    projectChange (data) {
      if (this.$route.query.type == '直销项目' || this.$route.query.type == '经销项目') {
        this.$confirm('变更项目并保存后,将清空"产品清单"并重新拉取,确定执行吗?','提示',{
          "confirmButtonText":"确定",
          "cancelButtonText":"取消",
          "type":"warning"
        }).then (() => {
          this.form.projectname = data.projectname
          this.form.sa_projectid = data.sa_projectid
          this.address = data.province + data.city + data.county + data.address
          this.projectVisible = false
        })
        
      }
    },
    receiptChange (data) {
      this.form.name = data.name
      this.form.phonenumber = data.phonenumber
      this.form.rec_contactsid = data.contactsid
      this.receiptVisible = false
    },
    accountChange (data) {
      this.form.enterprisename = data[0].enterprisename
      this.form.sys_enterpriseid = data[0].sys_enterpriseid
      this.accountVisible = false
    },
    centerChange (data) {
      this.form.enterprisename = data[0].enterprisename
      this.form.sys_enterpriseid = data[0].sys_enterpriseid
      this.agentVisible = false
    },
    modelChange (data) {
      this.form.modelname = data.modelname
      this.form.sys_printmodelid = data.sys_printmodelid
      this.modelVisible = false
    },
    concratChange (data) {
      this.form.ascription_title = data.projectname
      this.form.ascription_contractid = data.sa_contractid
      this.form.projectname = data.projectname
      this.form.sa_projectid = data.sa_projectid
      this.concratVisible = false
    },
    clearTime () {
      this.form.begdate = ''
      this.form.enddate = ''
    },
    timeChange (time) {
      this.form.begdate = time[0]
      this.form.enddate = time[1]
    },
  },
};
</script>

<style scoped>
* {
  box-sizing: border-box;
}
.header_info {
  background: #ffffff;
  padding: 16px 16px 0 16px;
  margin-bottom: 16px;
}
/deep/.el-select,.el-input,.el-date-editor {
  width: 100% !important;
}

</style>
