<template>
  <div>
    
    <show-data-table
      :newTime="newTime"
      v-if="newTime !== '' && sitem"
      :sitem="sitem"
      :columns="columns"
    >
      <template slot="company">
        <span>{{ sitem.company }}</span>
        <el-popover placement="top" width="300" trigger="hover">
          <ul>
            <li>
              <span>销售方公司编号：</span><span>{{ sitem.companyNo }}</span>
            </li>
          </ul>
          <i class="el-icon-warning-outline fr" slot="reference"></i>
        </el-popover>
      </template>
      <template slot="khName">
        <span>{{ sitem.khName }}</span>
        <el-popover placement="top" width="300" trigger="hover">
          <ul>
            <li>
              <span>购买方公司编号：</span><span>{{ sitem.khNo }}</span>
            </li>
          </ul>
          <i class="el-icon-warning-outline fr" slot="reference"></i>
        </el-popover>
      </template>

      <template slot="ladder">
        <el-table
                :data="sitem.ladder"
                :size="'mini'"
                border
                stripe
                style="width: 100%"
              >
                <el-table-column label="商品阶梯" width="70px">
                  <template slot-scope="scope">
                    {{ scope.$index + 1 }}</template
                  >
                </el-table-column>
                <el-table-column prop="good_type" label="商品类型" width="80px">
                  <template slot-scope="scope">
                    <el-tag
                      :size="'mini'"
                      v-text="
                        (
                          statusOptions.find(
                            (item) => item.value == scope.row.good_type
                          ) || {}
                        ).label || '--'
                      "
                    ></el-tag
                  ></template>
                </el-table-column>
                <el-table-column
                  prop="budget_price"
                  label="预算单价"
                  width="110"
                />
                <el-table-column prop="num" label="购买数量" width="110" />
                <el-table-column prop="cat_name" label="商品分类" />
                <el-table-column prop="good_img" label="图片" width="50">
                  <template slot-scope="scope">
                    <div
                      v-if="scope.row.good_img"
                      style="width: 20px; height: 20px"
                      class="hover"
                      v-viewer
                    >
                      <img
                        :src="scope.row.good_img"
                        style="display: inline-block; width: 100%; height: 100%"
                        alt=""
                      />
                    </div>
                  </template>
                </el-table-column>
                <el-table-column prop="good_name" label="商品名称" />

             
              </el-table>
      </template>
   

      <template slot="wsm">
        <span>{{ sitem.wsm_supplier }}/{{ sitem.wsm_name }}</span>
        <el-popover placement="top" width="300" trigger="hover">
          <ul>
            <li>
              <span>仓库公司编号：</span><span>{{ sitem.wsm_supplierNo }}</span>
            </li>
            <li>
              <span>仓库名称编号：</span><span>{{ sitem.wsm_code }}</span>
            </li>
          </ul>
          <i class="el-icon-warning-outline fr" slot="reference"></i>
        </el-popover>
      </template>

     
    </show-data-table>
    <div class="project-setPlan-title">
        销售业务项目方案
        <span
          style="margin: 0 0 0 50px; font-size: 13px; color: #e4393c"
          v-if="total !== 0 && table_type === 'list'"
          >{{ plan_show.status === "1" ? "已选定" : "" }}方案总金额：{{
            plan_show.sale_total
          }}元</span
        >
      
      </div>
    <div class="project-setPlan-backGood-table">
        <el-table
          :data="plan_show.feedback"
          :size="'mini'"
          border
          stripe
          :ref="'multipleTable' + index"
          style="width: 100%"
        >
          <el-table-column
            prop="pgNo"
            label="要求编码"
            width="155px"
            show-overflow-tooltip
          />
          <el-table-column
            prop="origin_sale_price"
            label="系统售价单价"
            width="110"
            show-overflow-tooltip
          />

          <el-table-column
            prop="new_sale_price"
            label="当前销售单价"
            width="110"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <digital-input
                v-if="table_type === 'add'"
                :values="scope.row.new_sale_price"
                :placeholder="'售价'"
                :min="scope.row.new_sale_price"
                :max="100000000000"
                :position="'right'"
                :precision="2"
                :size="'mini'"
                :controls="false"
                :append="''"
                @reschange="
                  moq_num_change($event, scope.$index, 'new_sale_price')
                "
              />
              <span v-else>{{ scope.row.sale_price }}元</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="origin_num"
            label="竞价数量"
            width="110"
            show-overflow-tooltip
          />
          <el-table-column
            prop="num"
            label="购买数量"
            width="110"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <digital-input
                v-if="table_type === 'add'"
                :values="scope.row.origin_num"
                :placeholder="'购买数量'"
                :min="scope.row.origin_num"
                :max="100000000000"
                :position="'right'"
                :precision="0"
                :size="'mini'"
                :controls="false"
                :append="''"
                @reschange="moq_num_change($event, scope.$index, 'num')"
              />
              <span v-else>{{ scope.row.num }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="data_source" label="商品来源" width="85">
            <template slot-scope="scope">
              <el-tag
                :size="'mini'"
                :type="scope.row.data_source === '1' ? 'success' : ''"
                >{{
                  scope.row.data_source === "1" ? "平台商品" : "采反商品"
                }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="good_img"
            label="商品图片"
            min-width="242px"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <div v-if="scope.row.good_img && scope.row.good_img.length > 0">
                <img
                  v-for="(si, sii) in scope.row.good_img"
                  :key="si + sii"
                  :src="si"
                  class="hover fl"
                  style="
                    display: inline-block;
                    width: 20px;
                    height: 20px;
                    margin: 0 2px 0 0;
                  "
                  alt=""
                  v-viewer
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="good_name"
            label="商品名称"
            min-width="120"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{ scope.row.good_name }}</span>
              <span v-for="(si, sii) in scope.row.specinfo" :key="si.id + sii">
                {{ sii === 0 ? "_" : "-" }}{{ si.spec_name }}[{{
                  si.spec_value_name
                }}]
              </span>
            </template>
          </el-table-column>
          <el-table-column
            prop="class_cat"
            label="商品分类"
            show-overflow-tooltip
          />
          <!-- <el-table-column
            prop="expiry_day"
            label="信息有效期"
            width="85"
            show-overflow-tooltip
          />
          <el-table-column
            prop="work_day"
            label="制作工期"
            width="70"
            show-overflow-tooltip
          />
          <el-table-column
            prop="delivery_day"
            label="物流时间"
            width="70"
            show-overflow-tooltip
          /> -->
        </el-table>
      </div>
  </div>
</template>

<script>
import {columns, statusOptions,} from "./columns";
import { cg_order_type_options } from "@/assets/js/statusList";

//————————————————
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/project";


export default {
  mixins: [mixinPage, resToken],
  props:["queryId"],
  data() {
    return {
      cg_order_type_options,
      statusOptions,
      cost_detailArr: [],
      allPlanList: [],
      index: 0,
      total: 0,
      table_type: "list",
      ladder: [],

      plan_show: {
        sale_total: "0",
        feedback: [],
      },
      projectNo: "",
      status: "",

      //——————————————————————
      columns,
      newTime: "",
      sitem:null,
      loading:false,
    };
  },
  mounted(){
    this.initData()
    this.get_plan("1")
  },
  methods:{
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData() {
      this.loading = true;
      const { code, message, data } = await asyncRequest.detail({
        projectNo: this.queryId,
      });
      this.loading = false;
      console.log(data);
      if (code === 0) {
        this.sitem = JSON.parse(JSON.stringify(data));
        const { status } = this.sitem;
        this.status = status;

        this.sitem.ladder.forEach((e) => {
            e.cat_name = "";
            const { cat_info } = e;
            if (cat_info && cat_info.length > 0) {
              cat_info.forEach((b, bi) => {
                e.cat_name += bi !== 0 ? "_" + b.name : b.name;
              });
            }
          });
        this.getNewTime();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },

    //获取全部方案
    async get_plan(type) {
      let model = {
        projectNo: this.queryId,
      };
      const { code, data, message } = await asyncRequest.get_project_plan(
        model
      );
      if (code === 0) {
        this.allPlanList = JSON.parse(JSON.stringify(data));
        this.total = this.allPlanList.length;
        if (type && this.allPlanList.length > 0) {
          await this.change_plan("1");
        }
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    //切换当前展示的方案
    async change_plan(type) {
      this.table_type = "list";
      this.plan_show = {
        sale_total: "0",
        status: "0",
        feedback: [],
      };
      if (type) {
        this.index = 0;
      }
      if (this.status === "4") {
        this.allPlanList.forEach((a, ai) => {
          if (a.status === "1") {
            this.index = ai;
          }
        });
      }
      const { sale_total, feedback, status } = JSON.parse(
        JSON.stringify(this.allPlanList[this.index])
      );
      this.plan_show = {
        sale_total: sale_total,
        status: status,
        feedback: JSON.parse(JSON.stringify(feedback)),
      };
      this.plan_show.feedback.map((v) => {
        v.class_cat = "";
        if (v.cat_info && v.cat_info.length > 0) {
          v.cat_info.forEach((x, i) => {
            v.class_cat += i === 0 ? x.name : "_" + x.name;
          });
        }
        if (v.good_img) {
          v.good_img =
            Object.prototype.toString.call(v.good_img) === "[object Array]"
              ? v.good_img
              : (v.good_img || "").split(",");
        }
        return v;
      });

    
      for (let i = 0; i < this.plan_show.feedback.length; i++) {
        this.$set(this.plan_show.feedback[i], i, this.plan_show.feedback[i]);
      }
    },
  }
};
</script>

<style lang="scss" scoped>
  .project-setPlan-title{
    // margin:20px 0 !important;
    padding:20px 50px !important;
    background: #f3f3f3;
  } 
</style>