<template>
  <div>
    <el-button size="mini" type="primary" @click="dialogFormVisible = true">新 增</el-button>
    <el-dialog title="新 增" :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="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="sa_projstagetempid">
              <el-select v-model="form.sa_projstagetempid" placeholder="请选择当前项目模板" size="mini">
                <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="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="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="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="province">
              <el-cascader
                  style="width:100%"
                  v-model="form.province"
                  :options="basicData.data().areaData"
                  @change="cascaderChange" clearable>
              </el-cascader>
            </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="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:{},
  data(){
    return {
      dialogFormVisible:false,
      projectAddress:[],
      form:{
        "sa_projectid":0,
        "projectname":"",
        "projectnum":"",
        "address":"",//可选
        "grade":"",//可选
        "budgetary":"",//可选
        "remarks":"",//可选
        "projecttype":"",//可选
        "sa_projstagetempid":"",//可选
        "signdate_due":"",//可选
        "scale":"",//可选
        "signamount_due":"",//可选
        "province":"",
        "city":"",
        "county":"",
      },
      rules:{
        projectname:[
          { required: true, message: '输入项目名称', trigger: 'blur'},
        ],
        projectnum:[
          { required: true, message: '输入项目编号', trigger: 'blur'}
        ],
        projecttype:[
          { required: true, message: '请选择项目类型', trigger: 'blur'}
        ],
        sys_enterpriseid:[
          { required: true, message: '请选择企业', trigger: 'blur'},
        ],
        sa_projstagetempid:[
          { required: true, message: '请选择当前项目模板', trigger: 'blur'}
        ],
      }
    }
  },
  methods:{
    /*省市县*/
    cascaderChange (val) {
      this.form = Object.assign({},this.form,{province:val[0],city:val[1],county:val[2]})
    },
    onSubmit(){
      console.log(this.form)
      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
        })
      })
    },
    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>