<template>
  <div>
    <el-button size="mini" :type="btnType?btnType:'text'" @click="editBtn">编 辑</el-button>
    <el-dialog title="新 增" append-to-body :visible.sync="dialogFormVisible" width="50%">
      <div slot="title" style="font-size: 15px">
        编辑项目商机
      </div>
      <el-row :gutter="20">
        <el-form :model="form" :rules="rules" ref="form" label-width="120px" label-position="left" size="mini">
          <el-col :span="24">
            <el-form-item label="企业名称" prop="sys_enterpriseid">
              <el-select v-model="form.sys_enterpriseid" placeholder="请选择企业" size="small" @change="getProjectAddres()">
                <el-option
                  v-for="item in enterpriseList()"
                  :key="item.sys_enterpriseid"
                  :label="item.enterprisename"
                  :value="item.sys_enterpriseid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectname">
              <el-input v-model="form.projectname" placeholder="请输入项目名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目编号" prop="projectnum">
              <el-input v-model="form.projectnum" placeholder="请输入项目编号"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目预算" prop="budgetary">
              <el-input v-model="form.budgetary" placeholder="项目预算"></el-input>
            </el-form-item>
          </el-col>
          
          <el-col :span="12">
            <el-form-item label="项目规模" prop="scale">
              <el-input v-model="form.scale" placeholder="请输入项目规模"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预计签约金额" prop="signamount_due">
              <el-input v-model="form.signamount_due" placeholder="请输入预计签约金额"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="详细地址" prop="address">
              <el-input v-model="form.address" placeholder="请输入详细地址"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="当前项目模板" prop="sa_projstagetempid">
              <el-select v-model="form.sa_projstagetempid" placeholder="请选择当前项目模板" size="small">
                <el-option
                  v-for="item in projectPanl()"
                  :key="item.sa_projstagetempid"
                  :label="item.stagetempname"
                  :value="item.sa_projstagetempid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目地址" prop="sa_project_addressid">
              <el-select v-model="form.sa_project_addressid" placeholder="请先选择企业" size="small">
                <el-option
                  v-for="(item,index) in projectAddress"
                  :key="index"
                  :label="item.address"
                  :value="item.contactsid">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目等级" prop="grade">
              <el-select v-model="form.grade" placeholder="请选择项目等级" size="small">
                <el-option
                  v-for="item in projectLead()"
                  :key="item.rowindex"
                  :label="item.value"
                  :value="item.rowindex">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目类型" prop="projecttype">
              <el-select v-model="form.projecttype" placeholder="请选择项目类型" size="small">
                <el-option
                  v-for="(item,index) in projectType()"
                  :key="index"
                  :label="item.value"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="预计签约时间" prop="signdate_due">
              <el-date-picker
                v-model="form.signdate_due"
                type="date"
                placeholder="选择日期"
                @change="timeChange">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <div class="dialog-footer">
        <el-button size="small" @click="dialogFormVisible = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="warning" @click="onSubmit" class="normal-btn-width btn-warning">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "add",
  inject:['enterpriseList','projectType','projectLead','projectPanl'],
  components:{},
  props:['data','btnType'],
  data(){
    return {
      dialogFormVisible:false,
      projectAddress:[],
      addChange:false,
      form:{
        "sa_projectid":0,
        "sys_enterpriseid":"",
        "projectname":"",
        "projectnum":"",
        "address":"",//可选
        "grade":"",//可选
        "budgetary":"",//可选
        "remarks":"",//可选
        "projecttype":"",//可选
        "sa_projstagetempid":"",//可选
        "signdate_due":"",//可选
        "scale":"",//可选
        "signamount_due":"",//可选
        "sa_project_addressid":""//选择地址
      },
      rules:{
        projectname:[
          { required: true, message: '输入项目名称', trigger: 'blur'},
        ],
        projectnum:[
          { required: true, message: '输入项目编号', trigger: 'blur'}
        ],
        sa_project_addressid:[
          { required: true, message: '请选择项目地址', trigger: 'blur'},
        ],
        projecttype:[
          { required: true, message: '请选择项目类型', trigger: 'blur'}
        ],
        sys_enterpriseid:[
          { required: true, message: '请选择企业', trigger: 'blur'},
        ],
        grade:[
          { required: true, message: '请选择项目等级', trigger: 'blur'}
        ],
        sa_projstagetempid:[
          { required: true, message: '请选择当前项目模板', trigger: 'blur'}
        ],
      }
    }
  },
  methods:{
    editBtn () {
      this.dialogFormVisible = true
      if (this.data.sys_enterpriseid) {
        this.form = Object.assign({},this.form,this.data)
        this.form.grade = parseInt(this.form.grade)
        this.getProjectAddres()
        this.addChange = true
      } else {
        this.form = Object.assign({},this.form,this.data)
        this.form.grade = parseInt(this.form.grade)
      }
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20221020144202",
          "version":1,
          "content": this.form
        })
        this.tool.showMessage(res,()=>{
          this.$emit('onSuccess')
          this.$refs['form'].resetFields();
          this.dialogFormVisible = false
        })
      })
    },
    /* 获取项目地址 */
    async getProjectAddres () {
      if (this.addChange) this.form.sa_project_addressid = ''
      let res = await this.$api.requested({
        "id": "20221018153302",
        "content": {
            "sys_enterpriseid": this.form.sys_enterpriseid,
            "pageNumber": 1,
            "pageSize": 999999,
            "where": {
                "condition": ""
            }
        }
      })
      this.projectAddress = res.data
      console.log(this.projectAddress);
    },
    timeChange(e) {
      if(!e) {
        this.form.signdate_due = ''
      } 
      let result = this.changeTimeFormat(e.getTime())
 
      this.form.signdate_due = result
    },
    changeTimeFormat(time) {
      var date = new Date(time);
      var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
      var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
      return date.getFullYear() + "-" + month + "-" + currentDate
    }
  }
}
</script>

<style scoped>
/deep/.el-select {
  height: 28px !important;
  width: 100%;
}
/deep/.el-input__inner {
  height: 28px !important;
}
</style>