<template>
  <div class="goreport">
    <div class="form gaisideshurukuang">
      <uni-forms
        ref="baseForm"
        style="font-size: 26rpx"
        :modelValue="form"
        label-width="160rpx"
      >
        <uni-forms-item
          label="走访类型："
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div style="height: 73rpx; line-height: 73rpx">
            <picker
              style="float: left; width: 100%; height: 40rpx; color: #777"
              :range="dic_SelectList.interviewType"
              :value="0"
              @change="bindPickerChange"
              range-key="label"
            >
              {{
                !form.interviewType
                  ? "请选择"
                  : dic_SelectList.interviewType[form.interviewTypeIndex].label
              }}
            </picker>
          </div>
        </uni-forms-item>
        <uni-forms-item
          label="走访公司"
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div
            @click="chosseAboutCompany()"
            style="height: 73rpx; line-height: 73rpx"
          >
            {{ !form.companyName ? "请选择走访公司" : form.companyName }}
          </div>
        </uni-forms-item>
        <uni-forms-item
          label="走访时间："
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div style="line-height: 73rpx">
            <!-- <picker
              style="float: left; width: 100%; height: 40rpx; color: #777"
              mode="time"
              :value="form.interviewTime"
              @change="getInterviewTime"
            >
              {{ !form.interviewTime ? "请选择走访时间" : form.interviewTime }}
            </picker> -->
            <uni-datetime-picker
              type="datetime"
              v-model="form.interviewTime"
              :border="false"
            />
          </div>
        </uni-forms-item>
        <uni-forms-item
          label="同行人："
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div style="display: flex; justify-content: space-between">
            <div style="margin-top: 15rpx">
              <div v-for="(item, index) in isSelectPeers" class="isSelectPeers">
                {{ item.name }}
                <icon type="clear" size="18" @click="deletePeer(index)" />
              </div>
            </div>
            <picker
              style="margin-top: 20rpx"
              @change="selectPeers"
              :value="index"
              range-key="truename"
              :range="peersList"
            >
              <span style="color: #1d18bc">添加</span>
            </picker>
          </div>
        </uni-forms-item>
        <uni-forms-item
          label="估值："
          v-if="form.interviewTypeIndex == 1"
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div style="height: 73rpx; display: flex; align-items: center">
            <input
              type="number"
              v-model="form.valuation"
              placeholder="请输入估值"
            />
          </div>
        </uni-forms-item>
        <uni-forms-item
          v-if="form.interviewTypeIndex == 1"
          label="融资情况"
          class="mt42rpx pd42rpx border_bottom_ccc"
        >
          <div style="height: 73rpx; display: flex; align-items: center">
            <input
              type="text"
              v-model="form.financingSituation"
              placeholder="请输入融资情况"
            />
          </div>
        </uni-forms-item>
      </uni-forms>
    </div>

    <div class="form" style="margin-bottom: 146rpx">
      <van-row>
        <van-col :span="24"> 走访记录 </van-col>
        <van-col :span="24">
          <div class="margint16rpx">
            <textarea
              type="textarea"
              inputBorder
              maxlength="5000"
              class="textarea_info"
              autoHeight
              style="
                background: #f9f9f9;
                margin-top: 16rpx;
                margin-top: 16rpx;
                border: 1px solid #cccccc;
                border-radius: 4px;
                padding: 10rpx;
                height: auto;
                min-height: 500rpx;
                width: 100%;
              "
              v-model="form.interviewRecord"
              placeholder="请输入"
            ></textarea>
          </div>
        </van-col>
      </van-row>
    </div>
    <van-popup :show="show" bind:close="onClose" round position="bottom">
      <div style="display: flex; justify-content: space-between">
        <div style="margin: 32rpx" @click="closePopup()">取消</div>
        <div style="margin: 32rpx" @click="getSelectValue()">确认</div>
      </div>
      <uni-easyinput
        v-model="companyName"
        @input="filtedCompanyAllList"
        placeholder="请输入内容"
      ></uni-easyinput>
      <picker-view
        indicator-style="height: 50px;"
        style="width: 100%; height: 300px"
        @change="getRowValue"
        :value="value"
      >
        <picker-view-column>
          <view
            v-for="(item, index) in companyAllList"
            :key="index"
            style="line-height: 50px; text-align: center"
            >{{ item.label }}</view
          >
        </picker-view-column>
      </picker-view>
    </van-popup>
    <view class="btn-view">
      <div
        class="btn"
        @click="takeParamsGoBack(1)"
        style="margin-left: 10rpx; width: 80%"
      >
        添加
      </div>
    </view>
  </div>
</template>

  <script>
import multipleSelect from "../../../subPackages/components/momo-multipleSelect/momo-multipleSelect";

import {
  getByCodes,
  addInterview,
  getBySaveStatus,
  editInterview,
  getCompanyAll,
  getQyList,
} from "@/js_sdk/http.js";
import vanRow from "../../../../wxcomponents/weapp/dist/row";
import vanCol from "../../../../wxcomponents/weapp/dist/col";

// import { findYuanQuList } from "@/js_sdk/http.js";
const form = {
  id: null,
  isStatus: null,
  interviewTime: null,
  interviewType: null,
  interviewTypeIndex: null,
  companyId: null,
  companyIndex: null,
  projectType: "",
  interviewRecord: "",
  associationCompanyString: [],
};
const isSelectPeers = [];
export default {
  components: {
    vanRow,
    vanCol,
    multipleSelect,
  },
  data() {
    return {
      companyAllList: [],
      isSelectPeers: [],
      companyName: "",
      value: 0,
      isSelectedIndex: null,
      isStatus: 2,
      range: [
        { value: 2, text: "是" },
        { value: 1, text: "否" },
      ],
      defaultSelected: [],
      dic_key: ["interviewType"],
      dic_SelectList: {},
      companyIndex: null,
      companyAllList: [],
      show: false,
      peersList: [],
      active: 0,
      form: { ...form },
      index: 0,
    };
  },
  computed: {},
  mounted() {
    this.getCompanyAll();
    this.getByCodes();
    this.getQyList();
  },
  methods: {
    deletePeer(i) {
      this.isSelectPeers.splice(i, 1);
    },
    selectPeers(e) {
      console.log(this.peersList[e.detail.value]);
      let data = this.peersList[e.detail.value];
      console.log(e.detail.value);
      for (let i = 0; i < this.isSelectPeers.length; i++) {
        if (data.truename == this.isSelectPeers[i].name) {
          this.$showToast("请勿重复添加");
          return;
        }
      }
      this.isSelectPeers.push({ name: data.truename, id: data.id });
    },
    async getQyList() {
      let data = await getQyList();
      this.peersList = data;
      console.log("dataadataasfdsagd", data);
    },
    closePopup() {
      this.show = false;
      this.form.companyIndex = null;
      this.form.companyId = null;
    },
    async filtedCompanyAllList() {
      // this.value = 0;
      // this.isSelectedIndex = 0;

      let companyAllList = await getCompanyAll(this.companyName);
      this.companyAllList = companyAllList.map((e) => {
        return {
          label: e.qymc,
          value: e.id,
        };
      });

      console.log("当前的value", this.value);
      console.log("companyAllList", this.companyAllList);
      // if (this.companyName && this.companyName.length > 0) {
      //   // companyAllList = this.companyAllList.filter((e) =>
      //   //   e.label.includes(this.companyName)
      //   // );

      //   if (companyAllList.length > 0 && companyAllList[0].value) {
      //     let index = this.companyAllList.findIndex(
      //       (e) => e.value == companyAllList[0].value
      //     );
      //     this.form.companyIndex = index;
      //   } else {
      //     this.form.companyIndex = null;
      //     this.form.companyId = null;
      //   }
      // } else {
      //   companyAllList = this.companyAllList;
      // }
      return companyAllList;
    },
    getRowValue(e) {
      console.log(e);
      // this.value = e.detail.value[0];
      this.isSelectedIndex = e.detail.value[0];
    },
    getSelectValue() {
      console.log(this.value, "this.value");
      console.log("this.isSelectedIndex", this.isSelectedIndex);
      if (this.isSelectedIndex == null) {
        this.form.companyIndex = 0;
        this.form.companyId = this.companyAllList[this.value].value;
        this.form.companyName = this.companyAllList[this.value].label;
        console.log("11111111", this.form.companyName);
      } else {
        let index = this.companyAllList.findIndex(
          (e) => e.value == this.companyAllList[this.isSelectedIndex].value
        );
        this.form.companyIndex = index;
        this.form.companyId = this.companyAllList[index].value;
        this.form.companyName = this.companyAllList[index].label;
        console.log("企业名称", this.form.companyName);
        console.log("企业id", this.form.companyId);
        console.log();
      }

      this.show = false;
    },
    changeLog(e) {},
    async getBySaveStatus() {
      const token = uni.getStorageSync("laocui_user_info");
      let data = await getBySaveStatus(JSON.parse(token).user.id);
      console.log("this.companyAllList", this.companyAllList);
      let companyIndex = this.companyAllList.findIndex(
        (e) => e.value == data.companyId
      );
      console.log("companyIndex", companyIndex);
      this.form.valuation = data.valuation;
      this.form.financingSituation = data.financingSituation;

      this.form.companyName =
        companyIndex != -1 ? this.companyAllList[companyIndex].label : "";
      this.form.id = data.id ? data.id : null;
      this.form.isStatus = Number(data.isStatus);
      this.form.interviewTime = data.interviewTime ? data.interviewTime : "";
      this.form.interviewRecord = data.interviewRecord
        ? data.interviewRecord
        : "";
      if (
        data.id &&
        data.interviewType != null &&
        data.interviewType != "null"
      ) {
        this.getInterviewLableByApiList(data);
      }
      if (data.id && data.companyId != null) {
        this.getCompanyIdByApiList(data);
      }
      if (data.companionsUserName.length > 0) {
        let isSelectPeers = data.companionsUserName.split(",").map((e) => {
          return { name: e };
        });
        for (let i = 0; i < data.companionsUserId.split(",").length; i++) {
          isSelectPeers[i].id = data.companionsUserId.split(",")[i];
        }
        console.log("isSelectPeers", isSelectPeers);
        this.isSelectPeers = isSelectPeers;
      } else {
        this.isSelectPeers = [];
      }
    },
    getCompanyIdByApiList(params) {
      this.form.companyId = params.companyId;

      let companyIndex = this.companyAllList.findIndex(
        (e) => e.value == params.companyId
      );
      this.form.companyIndex = companyIndex;
    },
    getInterviewLableByApiList(params) {
      let interviewTypeIndex = this.dic_SelectList.interviewType.findIndex(
        (e) => e.value == params.interviewType
      );
      this.form.interviewType =
        this.dic_SelectList.interviewType[interviewTypeIndex].value;
      this.form.interviewTypeIndex = interviewTypeIndex;
    },
    getInterviewTime(e) {
      this.form.interviewTime = e.detail.value;
    },
    async takeParamsGoBack(e) {
      console.log(this.form);
      const token = uni.getStorageSync("laocui_user_info");
      let data;
      if (e == 1) {
        if (this.form.companyId == null) {
          return this.$showToast("请选择走访公司");
        }
        if (this.form.interviewTime == null || this.form.interviewTime == "") {
          return this.$showToast("请选择走访时间");
        }
        if (this.form.interviewType == null) {
          return this.$showToast("请选择走访类型");
        }
      }
      // // console.log(this.form.interviewType);
      // return;
      // console.log("this.isSelectPeers", this.isSelectPeers);
      if (!this.form.valuation) {
        delete this.form.valuation;
      }
      if (!this.form.financingSituation) {
        delete this.form.financingSituation;
      }
      let companionsUserName = this.isSelectPeers
        .map((e) => {
          return e.name;
        })
        .join();
      let companionsUserId = this.isSelectPeers
        .map((e) => {
          return e.id;
        })
        .join();
      console.log(companionsUserId);
      // return;
      if (e == 0 && !this.form.id) {
        data = await addInterview({
          ...this.form,
          companionsUserName: companionsUserName,
          companionsUserId: companionsUserId,
          status: e,
          id: this.form.id == "null" ? "" : this.form.id,
          createdBy: JSON.parse(token).user.id,
          InterviewUserId: JSON.parse(token).user.id,
        });
      } else if (e == 0 && this.form.id) {
        data = await editInterview({
          ...this.form,
          status: e,
          companionsUserName: companionsUserName,
          companionsUserId: companionsUserId,
          createdBy: JSON.parse(token).user.id,
          InterviewUserId: JSON.parse(token).user.id,
        });
      } else if (e == 1 && this.form.id) {
        data = await editInterview({
          ...this.form,
          status: e,
          companionsUserName: companionsUserName,
          companionsUserId: companionsUserId,
          createdBy: JSON.parse(token).user.id,
          InterviewUserId: JSON.parse(token).user.id,
        });
      } else if (e == 1 && !this.form.id) {
        data = await addInterview({
          ...this.form,
          status: e,
          companionsUserName: companionsUserName,
          companionsUserId: companionsUserId,
          createdBy: JSON.parse(token).user.id,
          InterviewUserId: JSON.parse(token).user.id,
        });
      }

      if (data.code == 200) {
        if (e == 1) {
          this.$emit("changeActive");
          this.$showToast("提交成功");
          this.form = { ...form };
          this.isSelectPeers = [...isSelectPeers];
        }
        if (e == 0) {
          this.$showToast("保存成功");
        }
      }
    },
    getCompany(e) {
      this.form.companyIndex = e.detail.value;
      this.form.companyId = this.companyAllList[e.detail.value].value;
    },
    chosseAboutCompany() {
      this.show = true;
      // this.$refs.popup.open('top')
    },
    async getCompanyAll() {
      let companyAllList = await getCompanyAll();
      this.companyAllList = companyAllList.map((e) => {
        return {
          label: e.qymc,
          value: e.id,
        };
      });
      this.getBySaveStatus();
    },
    async getByCodes() {
      let data = await getByCodes(JSON.stringify(this.dic_key));
      this.dic_SelectList = this.$common.handleDicList(data);
      this.dic_SelectList.interviewType[0].id;
    },
    confirm(e) {
      let associationCompanyString = [];
      let associationCompany = [];
      for (var p in e) {
        associationCompanyString.push(e[p].label);
        associationCompany.push(e[p].value);
      }
      this.form.associationCompanyString = associationCompanyString.join(",");
      this.form.associationCompany = associationCompany.join(",");
    },
    bindPickerChange(e) {
      this.form.interviewTypeIndex = e.detail.value;
      this.form.interviewType =
        this.dic_SelectList.interviewType[e.detail.value].value;
    },
  },
  // onLoad: function (){
  // 	this.$refs.findYuanQuList()
  // },
};
</script>

  <style lang="scss">
.btn-view {
  width: 100%;
  height: 100rpx;
  padding: 20rpx 10%;
  background-color: #ffffff;
  position: fixed;
  bottom: 0;
  left: 0;
}
.btn {
  border-radius: 8rpx 8rpx 8rpx 8rpx;
  width: 40%;
  height: 90rpx;
  border-radius: 8rpx 8rpx 8rpx 8rpx;
  background: #1d18bc;
  line-height: 90rpx;
  color: white;
  text-align: center;
  float: left;
}
.isSelectPeers {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10rpx;
  width: 372rpx;
}
.gaisideshurukuang {
  font-size: 26rpx !important;
  .is-input-border {
    border-top: none !important;
    border-left: none !important;
    border-right: none !important;
    border: none;
    border-radius: 0 !important;
  }
  .uni-forms-item {
    border-bottom: 2rpx solid #cccccc !important;
    margin-top: 42rpx !important;
    padding-bottom: 42rpx !important;
  }
  .uni-forms-item__content {
    border-bottom: 2rpx solid #cccccc !important;
  }
}
.goreport {
  .textarea_info {
    textarea {
      background: #f9f9f9;
    }
  }
  .form {
    width: calc(100% - 128rpx);
    margin: 32rpx;
    background: #ffffff;
    padding: 32rpx;

    .label {
      height: 53rpx;
      line-height: 53rpx;
    }
    .is-input-border {
      border-top: none !important;
      border-left: none !important;
      border-right: none !important;
      border-radius: 0 !important;
    }
    .uni-forms-item {
      border-bottom: 2rpx solid #cccccc !important;
      margin-top: 0rpx !important;
      padding-bottom: 0rpx !important;
    }
  }
}
</style>
