<template>
  <view class="clueDetail_wrap">
    <view class="telPhone">
      <view class="left">
        <view class="phone"
          >电话:
          <show-real-text
            :real="receiptDetail.phone"
            :type="params.type"
            v-if="receiptDetail.phone"
          ></show-real-text>
        </view>
        <view
          class="copy_btn"
          v-if="params.type != '1'"
          @click="handleCopy(receiptDetail)"
          >复制</view
        >
      </view>
      <view class="right"> 发单人 : {{ receiptDetail.createNickName }} </view>
    </view>
    <view class="clueDetail_top_info">
      <view class="top_info_item">
        <view class="top">
          {{ defaultText(receiptDetail.clueOwnerName) }}
        </view>
        <view class="bottom"> 线索所属人 </view>
      </view>
      <view class="top_info_item">
        <view class="top">
          {{ defaultText(receiptDetail.brand) }}
        </view>
        <view class="bottom"> 品牌 </view>
      </view>
      <view class="top_info_item">
        <view class="top">
          {{ defaultText(receiptDetail.identificationName) }}
        </view>
        <view class="bottom"> 接单人 </view>
      </view>
      <view class="top_info_item">
        <view class="top">
          {{ crmFormCategoryFormat(receiptDetail.category) }}
        </view>
        <view class="bottom"> 类别 </view>
      </view>
      <view class="top_info_item">
        <view class="top">
          {{ crmFormTacticFormat(receiptDetail.tactic) }}
        </view>
        <view class="bottom"> 采用战术 </view>
      </view>
      <view class="top_info_item">
        <view class="top">
          {{ crmFollowStatusFormat(receiptDetail.status) }}
        </view>
        <view class="bottom"> 跟进状态 </view>
      </view>
    </view>

    <view class="clue_state_wrap">
      <view class="top_left">阶段:</view>
      <view class="steps_wrap">
        <ld-select
          v-model="receiptDetail.state"
          :list="crmFormStateDict"
          value-key="dictValue"
          label-key="dictLabel"
          placeholder="点击选择阶段"
          @change="handleStateConfirm"
        >
        </ld-select>
      </view>
    </view>

    <view class="clue_tag_wrap">
      <view class="clue_tag_add_btn" @click="handleAddClueTag">
        + 添加标签
      </view>
      <u-tag
        :text="tag.name"
        plain
        plainFill
        :closable="true"
        @close="hanldeTagClose(tag)"
        borderColor="#fff"
        v-for="tag in receiptDetail.tags"
        :key="tag.id"
        style="margin-left: 10px; margin-bottom: 10px"
        :bgColor="tag.color"
        color="#fff"
      ></u-tag>
    </view>

    <yui-tabs
      :tabs="tabs"
      v-model="activeIndex"
      :lineWidth="'120rpx'"
      :isLazyRender="false"
      color="#108cff"
      titleActiveColor="#108cff"
      :swipeable="true"
      :swiper="false"
      :ellipsis="false"
      :scroll-threshold="3"
    >
      <template #chatFile>
        <upload-file
          :clueId="clueId"
          :sourceId="orderId"
          ref="uploadFile1"
          type="2"
          orderFileType="1"
          isDuplicate="1"
        ></upload-file>
      </template>
      <template #quoteFile>
        <upload-file
          :clueId="clueId"
          :sourceId="orderId"
          ref="uploadFile2"
          type="2"
          orderFileType="2"
          isDuplicate="1"
        ></upload-file>
      </template>
      <template #hdImageFile>
        <upload-file
          :clueId="clueId"
          :sourceId="orderId"
          ref="uploadFile3"
          type="2"
          orderFileType="3"
          isDuplicate="1"
        ></upload-file>
      </template>
      <template #otherFile>
        <upload-file
          :clueId="clueId"
          :sourceId="orderId"
          ref="uploadFile4"
          type="2"
          orderFileType="4"
          isDuplicate="1"
        ></upload-file>
      </template>
      <template #frontendFile>
        <upload-file
          :clueId="clueId"
          ref="uploadFile5"
          type="6"
          isDuplicate="1"
        ></upload-file>
      </template>
      <template #frontendFollow>
        <clue-follow
          :clueId="clueId"
          ref="clueFollow"
          type="4"
        ></clue-follow>
      </template>
      <template #followRecord>
        <clue-follow
          :clueId="clueId"
          ref="follow"
          type="5"
        ></clue-follow>
      </template>
      <!-- <template #receiptInfo>
        <receipt-form-list
          :sendFormId="receiptDetail.id"
          :clueId="receiptDetail.clueId"
          :receiptDetail="receiptDetail"
          ref="receiptFormList"
        ></receipt-form-list>
      </template>
      <template #commissionInfo>
        <commission-form-list
          :sendFormId="receiptDetail.id"
          :clueId="receiptDetail.clueId"
          ref="commissionFormList"
        ></commission-form-list>
      </template> -->
    </yui-tabs>

    <u-tabbar
      class="clueDetail_tabber"
      :fixed="true"
      inactiveColor="#ffffff"
      :placeholder="true"
      :safeAreaInsetBottom="true"
    >
      <u-tabbar-item
        text="拨打电话"
        icon="../../static/clueDetail/icon-phone.png"
        @click="handleCallPhone"
      ></u-tabbar-item>
      <u-tabbar-item
        v-if="receiptDetail.status === '1'"
        text="接单"
        icon="../../static/orderDetail/jd.png"
        @click="handleOrderForm"
      ></u-tabbar-item>
      <u-tabbar-item
        v-if="receiptDetail.status === '2'"
        text="收单"
        icon="../../static/orderDetail/sd.png"
        @click="handleReceiptForm"
      ></u-tabbar-item>
      <u-tabbar-item
        v-if="receiptDetail.status === '2'"
        text="未收"
        icon="../../static/orderDetail/ws.png"
        @click="handleDenialForm"
      ></u-tabbar-item>
      <u-tabbar-item
        v-if="receiptDetail.status === '1'"
        text="撤销"
        icon="../../static/orderDetail/cx.png"
        @click="handleDelete"
      ></u-tabbar-item>
      <u-tabbar-item
        text="添加跟进"
        icon="../../static/caseDetail/icon-follow.png"
        @click="handleAddFollow"
      ></u-tabbar-item>
    </u-tabbar>

    <group-select
      class="clueTagsSelect"
      :list="clueTagGroupVoList"
      scrollHeight="720rpx"
      groupName="groupName"
      groupChild="clueTagDataList"
      label-key="name"
      value-key="id"
      placeholder="请选择线索标签"
      v-model="checkTags"
      multiple
      clearable
      ref="clueTag"
      @confirm="handleClueTagConfirm"
    ></group-select>
  </view>
</template>

<script>
import { cloneDeep } from "lodash";
import { selectDictLabel } from "@/utils/util";
import uploadFile from "../tabs/uploadFile/index.vue";
import clueFollow from "../tabs/followRecord/index.vue";
// import receiptFormList from "../tabs/receiptFormList/receiptFormList.vue";
// import commissionFormList from "../tabs/commissionFormList/commissionFormList.vue";
export default {
  components: {
    uploadFile,
    clueFollow,
    // receiptFormList,
    // commissionFormList,
  },
  props: {
    orderId: {
      type: [String, Number],
      required: true,
    },
    clueId: {
      type: [String, Number],
      required: true,
    },
    params: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      showModal: false,
      showStateSelect: false,
      receiptDetail: {},
      checkTags: [],
      clueTagGroupVoList: [],
      crmFormCategoryDict: [],
      crmFormTacticDict: [],
      crmFormStateDict: [],
      crmHandelStatusDict: [],
      tabs: [
        {
          label: "聊天附件",
          slot: "chatFile",
        },
        {
          label: "报价附件",
          slot: "quoteFile",
        },
        {
          label: "高清图附件",
          slot: "hdImageFile",
        },
        {
          label: "其他附件",
          slot: "otherFile",
        },
        {
          label: "前端附件",
          slot: "frontendFile",
        },
        {
          label: "前端跟进",
          slot: "frontendFollow",
        },
        {
          label: "跟进记录",
          slot: "followRecord",
        },
        // {
        //   label: "收单信息",
        //   slot: "receiptInfo",
        // },
        // {
        //   label: "收单分成",
        //   slot: "commissionInfo",
        // },
      ],
      activeIndex: 0,
    };
  },
  methods: {
    async hanldeTagClose(tag) {
      const { id, tags } = this.receiptDetail;
      const copyTags = cloneDeep(tags);
      if (id == null) {
        uni.$u.toast("修改异常");
        return;
      }
      const index = copyTags.findIndex((v) => v.id === tag.id);
      if (index !== -1) {
        copyTags.splice(index, 1);
        const allTags = copyTags.map((v) => v.id).join(",");
        await uni.$u.api.updateTags({
          id: id,
          allTags,
        });
        this.receiptDetail.tags = copyTags;
        this.checkTags = this.receiptDetail.tags.map((v) => v.id);
      }
    },
    async handleClueTagConfirm() {
      const allTags = this.checkTags.join(",");
      await uni.$u.api.updateTags({
        id: this.receiptDetail.id,
        allTags,
      });
      this.getDetail();
    },
    handleAddClueTag() {
      this.$refs.clueTag.showModal();
    },
    async handleStateConfirm(e) {
      const state = e.value;
      await uni.$u.api.updateOrderState({
        id: this.receiptDetail.id,
        state,
      });
      uni.$u.toast("操作成功");
    },
    defaultText(text) {
      return text ? text : "-";
    },
    crmFormCategoryFormat(v) {
      return selectDictLabel(this.crmFormCategoryDict, v);
    },
    crmFormTacticFormat(v) {
      return selectDictLabel(this.crmFormTacticDict, v);
    },
    crmFollowStatusFormat(v) {
      return selectDictLabel(this.crmHandelStatusDict, v);
    },
    selectDictLabel,
    handleCopy(item) {
      uni.setClipboardData({
        data: item.phone,
        success: function () {
          uni.$u.toast("复制成功");
        },
      });
    },
    // 添加联系人
    handleCallPhone() {
      uni.makePhoneCall({
        phoneNumber: this.receiptDetail.phone,
        success: () => {
          this.$store.commit("call/SET_FORM", {
            clueId: this.receiptDetail.clueId,
            type: "3",
            callee: this.receiptDetail.phone,
          });
        },
      });
    },
    // 添加跟进记录
    handleAddFollow() {
      uni.navigateTo({
        url: `/pages/addFollow/index?orderId=${this.orderId}`,
      });
    },
    handleUploadRecord() {
      uni.navigateTo({
        url: `/pages/uploadRecord/index?clueId=${this.orderId}`,
      });
    },
    // 接单操作
    async handleOrderForm() {
      if (this.receiptDetail.status === "2") {
        uni.$u.toast("当前订单已经被接单");
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确定要接单吗？",
        success: async (res) => {
          if (res.confirm) {
            try {
              await uni.$u.api.oderForm({
                status: "2",
                id: this.receiptDetail.id,
              });
              uni.$u.toast("接单成功");
              this.getDetail(); // 刷新详情
            } catch (error) {
              uni.$u.toast("接单失败");
            }
          }
        },
      });
    },
    // 收单操作
    async handleReceiptForm() {
      if (
        this.receiptDetail.status === "3" ||
        this.receiptDetail.status === "4"
      ) {
        uni.$u.toast("当前订单已经被收单或未收");
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确定要收单吗？",
        success: async (res) => {
          if (res.confirm) {
            try {
              await uni.$u.api.oderForm({
                status: "3",
                id: this.receiptDetail.id,
              });
              uni.$u.toast("收单成功");
              this.getDetail(); // 刷新详情
            } catch (error) {
              uni.$u.toast("收单失败");
            }
          }
        },
      });
    },
    // 未收操作
    async handleDenialForm() {
      if (
        this.receiptDetail.status === "3" ||
        this.receiptDetail.status === "4"
      ) {
        uni.$u.toast("当前订单已经被收单或未收");
        return;
      }
      uni.showModal({
        title: "提示",
        content: "确定要标记为未收吗？",
        success: async (res) => {
          if (res.confirm) {
            try {
              await uni.$u.api.oderForm({
                status: "4",
                id: this.receiptDetail.id,
              });
              uni.$u.toast("标记未收成功");
              this.getDetail(); // 刷新详情
            } catch (error) {
              uni.$u.toast("操作失败");
            }
          }
        },
      });
    },
    // 撤销操作
    async handleDelete() {
      uni.showModal({
        title: "提示",
        content: "是否确定撤销?",
        success: async (res) => {
          if (res.confirm) {
            try {
              await uni.$u.api.deleteOrder([this.receiptDetail.id]);
              uni.$u.toast("撤销成功");
              // 撤销后返回上一页
              setTimeout(() => {
                uni.navigateBack();
              }, 1500);
            } catch (error) {
              uni.$u.toast("撤销失败");
            }
          }
        },
      });
    },
    // 初始化详情页
    async handleInit() {
      this.getDetail();
      this.$getDicts("crm_form_category").then((res) => {
        this.crmFormCategoryDict = res;
      });
      this.$getDicts("crm_form_tactic").then((res) => {
        this.crmFormTacticDict = res;
      });
      this.$getDicts("crm_form_state").then((res) => {
        this.crmFormStateDict = res;
      });
      this.$getDicts("crm_follow_status").then((res) => {
        this.crmHandelStatusDict = res;
      });
      uni.$u.api
        .getClueTagGroupVoList({
          tagGroupApplication: "2",
        })
        .then(({ data }) => {
          this.clueTagGroupVoList = data;
        });
    },
    getDetail() {
      uni.$u.api.getClueSendFormVoByOrderId({
          id: this.orderId,
        })
        .then((res) => {
          this.receiptDetail = res.data;
          this.checkTags = this.receiptDetail.tags
            ? this.receiptDetail.tags.map((v) => v.id)
            : [];
        });
    },
  },
  created() {
    this.handleInit();
  },
};
</script>

<style lang="scss" scoped>
.clueTagsSelect {
  height: 0;
  overflow: hidden;
}

.clue_tag_wrap {
  display: flex;
  align-items: center;
  background-color: #fff;
  padding: 0 10px;
  margin: 20px 0;
  flex-wrap: wrap;
  min-height: 50px;

  .clue_tag_add_btn {
    font-size: 14px;
    color: #108cff;
  }
}

.clue_state_wrap {
  background-color: #fff;
  padding: 10px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;

  .top_left {
    font-size: 16px;
    flex: 0 0 50px;
  }

  .steps_wrap {
    flex: 1;
  }
}

.clueDetail_tabber {
  ::v-deep .u-tabbar__content {
    background: #108cff;
    border-top-right-radius: 10px;
    border-top-left-radius: 10px;
  }
}

.clueDetail_wrap {
  .telPhone {
    display: flex;
    background: #fff;
    padding: 20px;
    justify-content: space-between;

    .left {
      display: flex;
    }

    .copy_btn {
      color: #4fa5fe;
      margin-left: 10px;
    }
  }

  .clueDetail_top_info {
    display: flex;
    flex-wrap: wrap;
    background-color: #ffffff;
    padding-top: 20px;
    margin: 18px 0;

    .top_info_item {
      width: 33.33%;
      text-align: center;
      margin-bottom: 20px;

      .top {
        font-size: 15px;
        color: #202020;
        margin-bottom: 5px;
        font-weight: bold;
      }

      .bottom {
        font-size: 15px;
        color: #c0c0c7;
      }
    }
  }
}
</style>


