<template>
  <div>
    <el-button size="mini" :type="btnType?btnType:'text'" @click="editBtn">编 辑</el-button>
    <el-drawer
        title="编辑项目"
        :visible.sync="dialogFormVisible"
        size="800px"
        direction="rtl"
        append-to-body
        @close="onClose">
      <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="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="mini">
                  <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="mini">
                  <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>
      <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: "add",
  inject:['enterpriseList','projectType','projectLead','projectPanl'],
  components:{},
  props:['data','btnType'],
  data(){
    return {
      dialogFormVisible:false,
      projectAddress:[],
      addChange:false,
      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'}
        ],
        province:[
          { required: true, message: '请选择项目地址', trigger: 'blur'}
        ]
      }
    }
  },
  methods:{
    /*省市县*/
    cascaderChange (val) {
      this.form = Object.assign({},this.form,{city:val[1],county:val[2]})
    },
    editBtn () {
      this.dialogFormVisible = true
      this.form = Object.assign({},this.form,this.data)
      this.form.province = [this.form.province,this.form.city,this.form.county]
      this.form.grade = parseInt(this.form.grade)
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        this.form.province = this.form.province[0]
        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
          this.addChange = 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
    },
    onClose(){
      this.dialogFormVisible = false
    }
  }
}
</script>

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