<template>
  <div :class="type === '2' ? 'poolAdd poolDetail' : 'poolAdd'">
    <el-form
      ref="form"
      :model="form"
      :rules="type === '2' ? rules1 : rules"
      label-width="140px"
      class="special-el-form"
    >
      <h3>项目基础信息</h3>
      <el-divider></el-divider>
      <el-form-item label="项目名称" prop="projectName">
        <el-input v-model="form.projectName" placeholder="请输入项目名称" />
      </el-form-item>

      <el-form-item label="渠道" prop="channel">
        <div class="el-input__inner inputSimulation" @click="handleChannelItem">
          {{
            form.tProjectChannel && form.tProjectChannel.channelName
              ? form.tProjectChannel.channelName
              : "请选择渠道"
          }}
        </div>
        <channelItem
          ref="channelItem"
          @getChannelInfo="getChannelInfo"
        ></channelItem>
        <!-- <el-input v-model="form.channel" placeholder="请输入渠道" /> -->
      </el-form-item>
      <el-form-item label="初次接触日期" prop="contactDate">
        <el-date-picker
          clearable
          v-model="form.contactDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择初次接触日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="所属组别">
        <el-select
          v-model="form.tProjectChannel.channelGroup"
          disabled
          placeholder="请选择所属组别"
        >
          <el-option
            v-for="dict in dict.type.project_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属行业" prop="industry">
        <el-select v-model="form.industry" placeholder="请选择所属行业">
          <el-option
            v-for="dict in dict.type.CUSTOMER_TRADE"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="投资负责人" prop="investHead">
        <div class="el-input__inner inputSimulation" @click="openSelectDept">
          {{ form.investHead ? form.investHead : "请选择投资负责人" }}
        </div>
        <selecDept
          ref="selecDepts"
          @getDeptUserInfo="getDeptUserInfo"
          :deptId="deptId"
        ></selecDept>
      </el-form-item>

      <el-form-item
        label="项目情况描述"
        prop="description"
        class="special-el-form-item"
      >
        <el-input
          rows="4"
          type="textarea"
          v-model="form.description"
          placeholder="请输入项目情况描述"
        />
      </el-form-item>
      <h3>项目公司信息</h3>
      <el-divider></el-divider>
      <el-form-item label="公司名称" prop="tProjectCompany.companyName">
        <el-input
          v-model="form.tProjectCompany.companyName"
          placeholder="请输入公司名称"
        />
      </el-form-item>
      <el-form-item label="统一社会信用代码" prop="tProjectCompany.companyCode">
        <el-input
          v-model="form.tProjectCompany.companyCode"
          placeholder="请输入企业统一社会信用代码"
        />
      </el-form-item>
      <el-form-item label="营业开始时间" prop="tProjectCompany.startTime">
        <el-date-picker
          clearable
          v-model="form.tProjectCompany.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择营业开始时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="营业结束时间" prop="tProjectCompany.endTime">
        <el-date-picker
          clearable
          v-model="form.tProjectCompany.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择营业结束时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="注册资本" prop="tProjectCompany.registeredCapital">
        <el-input
          v-model="form.tProjectCompany.registeredCapital"
          placeholder="请输入注册资本"
        />
      </el-form-item>
      <el-form-item label="注册地址" prop="tProjectCompany.registeredAddress">
        <el-input
          v-model="form.tProjectCompany.registeredAddress"
          placeholder="请输入注册地址"
        />
      </el-form-item>
      <el-form-item label="实缴资本" prop="tProjectCompany.paidCapital">
        <el-input
          v-model="form.tProjectCompany.paidCapital"
          placeholder="请输入实缴资本"
        />
      </el-form-item>
      <el-form-item label="办公地址" prop="tProjectCompany.businessAddress">
        <el-input
          v-model="form.tProjectCompany.businessAddress"
          placeholder="请输入办公地址"
        />
      </el-form-item>
      <el-form-item label="实控人" prop="tProjectCompany.actualBod">
        <el-input
          v-model="form.tProjectCompany.actualBod"
          placeholder="请输入实控人"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tProjectCompany.phone">
        <el-input
          v-model="form.tProjectCompany.phone"
          placeholder="请输入联系电话"
          maxlength="11"
        />
      </el-form-item>
      <el-form-item label="所属类型" prop="tProjectCompany.type">
        <el-input
          v-model="form.tProjectCompany.type"
          placeholder="请输入所属类型"
        />
      </el-form-item>
      <el-form-item label="附件" prop="listFile">
        <fileItem :id="form.id" @getFileList="getFileList"></fileItem>
      </el-form-item>
      <h3>项目公司联系人信息</h3>
      <el-divider></el-divider>
      <el-form-item label="联系人姓名" prop="tProjectContacts.name">
        <el-input
          v-model="form.tProjectContacts.name"
          placeholder="请输入联系人姓名"
        />
      </el-form-item>
      <el-form-item label="职位" prop="tProjectContacts.position">
        <el-input
          v-model="form.tProjectContacts.position"
          placeholder="请输入职位"
        />
      </el-form-item>
      <el-form-item label="联系电话/微信" prop="tProjectContacts.contact">
        <el-input
          v-model="form.tProjectContacts.contact"
          placeholder="请输入联系电话/微信"
        />
      </el-form-item>
      <h3>项目融资信息</h3>
      <el-divider></el-divider>
      <el-form-item label="项目融资阶段" prop="financingStage">
        <el-input
          v-model="form.financingStage"
          placeholder="请输入项目融资阶段"
        />
      </el-form-item>
      <el-form-item label="预期融资金额(万元)" prop="financingMoney">
        <el-input
          v-model="form.financingMoney"
          placeholder="请输入预期融资金额"
        />
      </el-form-item>
      <el-form-item label="投前估值(万元)" prop="investValuation">
        <el-input v-model="form.investValuation" placeholder="请输入投前估值" />
      </el-form-item>
      <el-form-item label="预计投资金额(万元)" prop="investMoney">
        <el-input v-model="form.investMoney" placeholder="请输入预计投资金额" />
      </el-form-item>
      <el-form-item label="投资价值" prop="investWorth">
        <el-input v-model="form.investWorth" placeholder="请输入投资价值" />
      </el-form-item>
      <el-form-item
        label="历史融资"
        prop="previousFinancing"
        class="special-el-form-item"
      >
        <el-input
          rows="4"
          type="textarea"
          v-model="form.previousFinancing"
          placeholder="请输入历史融资"
        />
      </el-form-item>
      <el-form-item
        label="融资条件"
        prop="financingCondition"
        class="special-el-form-item"
      >
        <el-input
          rows="4"
          type="textarea"
          v-model="form.financingCondition"
          placeholder="请输入融资条件"
        />
      </el-form-item>
      <el-form-item
        label="投资亮点"
        prop="investSparkle"
        class="special-el-form-item"
      >
        <el-input
          rows="4"
          type="textarea"
          v-model="form.investSparkle"
          placeholder="请输入投资亮点"
        />
      </el-form-item>

      <el-form-item
        label="股权架构"
        prop="shareholding"
        class="special-el-form-item"
      >
        <el-input
          rows="4"
          type="textarea"
          v-model="form.shareholding"
          placeholder="请输入股权架构"
        />
      </el-form-item>
      <el-form-item label="备注" prop="mark" class="special-el-form-item">
        <el-input
          :readonly="type === '2' ? true : false"
          rows="4"
          type="textarea"
          v-model="form.mark"
          placeholder="请输入备注"
        />
      </el-form-item>
      <el-form-item class="btnList" v-if="type === '1'">
        <el-button @click="goBack">返 回</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import {
  listPool,
  getPool,
  delPool,
  addPool,
  updatePool,
} from "@/api/invest/pool";
import fileItem from "./fileItem";
import channelItem from "./channelItem";
import selecDept from "../components/selecDept";
export default {
  props: {
    id: {
      type: String,
    },
    type: {
      type: String,
      default: "1", //1可编辑  2不可编辑
    },
  },
  components: { fileItem, channelItem, selecDept },
  dicts: ["CUSTOMER_TRADE", "project_group"],
  data() {
    return {
      deptId: process.env.VUE_APP_DEPTID,
      title: "新增项目",
      baseUrl: process.env.VUE_APP_BASE_API,

      // 表单参数
      form: {
        id: null,
        projectName: null,
        projectGroup: null,
        industry: null,
        recordDate: null,
        projectCode: null,
        channel: null,
        contactDate: null,
        projectDate: null,
        decisionDate: null,
        tProjectCompany: {
          companyName: null,
          companyCode: null,
          startTime: null,
          endTime: null,
          registeredCapital: null,
          registeredAddress: null,
          paidCapital: null,
          businessAddress: null,
          actualBod: null,
          phone: null,
          type: null,
        },
        tProjectContacts: {
          name: null,
          position: null,
          contact: null,
        },
        tProjectChannel: {
          channelName: null,
          channelGroup: null,
        },

        description: null,
        projectContacts: null,
        investHead: null,
        previousFinancing: null,
        financingStage: null,
        financingCondition: null,
        financingMoney: null,
        financingDate: null,
        investSparkle: null,
        investValuation: null,
        investMoney: null,
        investType: null,
        investPloy: null,
        investWorth: null,
        shareholding: null,
        projectStage: null,
        projectState: null,
        mark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        listFile: null,
      },
      fileList: [],
      // 表单校验
      rules1: {},
      rules: {
        projectName: [{ required: true, trigger: "blur", message: "请输入" }],
        channel: [{ required: true, trigger: "blur", message: "请输入" }],
        contactDate: [{ required: true, trigger: "blur", message: "请输入" }],
        // projectGroup: [
        //   { required: true, trigger: "change", message: "请选择" },
        // ],
        industry: [{ required: true, trigger: "change", message: "请选择" }],
        investHead: [{ required: true, trigger: "blur", message: "请输入" }],
        // description: [{ required: true, trigger: "blur", message: "请输入" }],
        "tProjectCompany.companyName": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.companyCode": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.startTime": [
          { required: true, trigger: "blur", message: "请选择" },
        ],
        "tProjectCompany.endTime": [
          { required: true, trigger: "blur", message: "请选择" },
        ],
        "tProjectCompany.registeredCapital": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.registeredAddress": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.paidCapital": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.businessAddress": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.actualBod": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectCompany.phone": [
          { required: true, trigger: "blur", message: "请输入手机号" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        "tProjectCompany.type": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        // files: [{ required: true, trigger: "blur", message: "请输入" }],
        "tProjectContacts.name": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectContacts.position": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        "tProjectContacts.contact": [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        financingStage: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        financingMoney: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        investValuation: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        investMoney: [{ required: true, trigger: "blur", message: "请输入" }],
        investWorth: [{ required: true, trigger: "blur", message: "请输入" }],
        // previousFinancing: [{ required: true, trigger: "blur", message: "请输入" }],
        financingCondition: [
          { required: true, trigger: "blur", message: "请输入" },
        ],
        investSparkle: [{ required: true, trigger: "blur", message: "请输入" }],
        shareholding: [{ required: true, trigger: "blur", message: "请输入" }],
        // mark: [{ required: true, trigger: "blur", message: "请输入" }]
      },
    };
  },
  mounted() {
    console.log("000",this.deptId)
    if (this.id) {
      this.getPoolDetail(this.id);
    } else {
      this.reset();
    }
  },
  methods: {
    // 选择部门人
    openSelectDept() {
      this.$refs.selecDepts.show();
    },
    getDeptUserInfo(info) {
      if (info.length > 0) {
        this.form.investHead = info[0].nickName;
        this.$refs.form.clearValidate(["investHead"]);
      }
    },
    // 获取渠道信息
    getChannelInfo(info) {
      if (info.length > 0) {
        this.form.channel = info[0].id;
        this.form.tProjectChannel.channelName = info[0].channelName;
        this.form.tProjectChannel.channelGroup = info[0].channelGroup;
        this.$refs.form.clearValidate(["channel"]);
      }
    },
    handleChannelItem() {
      this.$refs.channelItem.showChannelItem = true;
    },
    getPoolDetail(id) {
      getPool(id).then((response) => {
        this.form = response.data;
      });
    },
    // 获取fileList
    getFileList(fileList) {
      this.fileList = fileList;
    },
    /** 提交按钮 */
    submitForm() {
      let that = this;
      this.$refs["form"].validate((valid, rules) => {
        if (valid) {
          this.form.listFile = this.fileList;
          if (this.form.id != null) {
            updatePool(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              setTimeout(() => {
                that.goBack();
              }, 1500);
            });
          } else {
            this.form.projectStage = "1";
            this.form.projectState = "a";
            addPool(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              setTimeout(() => {
                that.goBack();
              }, 1500);
            });
          }
        } else {
          for (var ruleName in rules) {
            let _rule = rules[ruleName];
            let message = _rule[0].message;
            this.$message.warning(message, 1);
            break;
          }
          //验证不通过回滚到不通过的第一个位置
          this.$nextTick(() => {
            let isError = document.getElementsByClassName("is-error");
            isError[0].scrollIntoView({
              block: "center",
              behavior: "smooth",
            });
          });
        }
      });
    },
    goBack() {
      this.$router.go(-1);
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectName: null,
        projectGroup: null,
        industry: null,
        recordDate: null,
        projectCode: null,
        channel: null,
        contactDate: null,
        projectDate: null,
        decisionDate: null,
        tProjectCompany: {
          companyName: null,
          companyCode: null,
          startTime: null,
          endTime: null,
          registeredCapital: null,
          registeredAddress: null,
          paidCapital: null,
          businessAddress: null,
          actualBod: null,
          phone: null,
          type: null,
        },
        tProjectContacts: {
          name: null,
          position: null,
          contact: null,
        },
        tProjectChannel: {
          channelName: null,
          channelGroup: null,
        },
        description: null,
        projectContacts: null,
        investHead: null,
        previousFinancing: null,
        financingStage: null,
        financingCondition: null,
        financingMoney: null,
        financingDate: null,
        investSparkle: null,
        investValuation: null,
        investMoney: null,
        investType: null,
        investPloy: null,
        investWorth: null,
        shareholding: null,
        projectStage: null,
        projectState: null,
        mark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        listFile: null,
      };
      this.resetForm("form");
    },
  },
};
</script>
<style lang="scss" scoped>
.poolAdd {
  h2 {
    margin-left: 30px;
  }
  .special-el-form {
    h3 {
      width: 100%;
      margin-bottom: -10px;
      font-weight: bold;
    }
    .btnList {
      width: 100%;
      display: flex;
      justify-content: center;
      padding-bottom: 40px;
    }
  }
}
.poolDetail {
  position: relative;
  cursor: not-allowed;
}
.poolDetail::after {
  content: "";
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 120%;
}
</style>