<template>
  <div class="zixunOrder pagePadding">
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <!-- <template #good_img="{ 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> -->
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .type || ''
          "
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #specinfo="{ scope }">
        <span v-for="(si, sii) in scope.row.specinfo" :key="si.specid + sii">
          <span v-if="sii !== 0"></span>{{ si.spec_name }}[{{
            si.spec_value_name
          }}]</span
        >
      </template>

      <template #operation="{ scope }">
        <!-- 状态 -->
        <el-tooltip
          v-if="powers.some((item) => item == '007')"
          effect="dark"
          content="详情"
          placement="top"
        >
          <i class="el-icon-view tb-icon" @click="show_view(scope.row)"></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers &&
            powers.length > 0 &&
            powers.some((item) => item == '068') &&
            is_project === '0' &&
            scope.row.reject &&
            scope.row.reject.id
          "
          effect="dark"
          content="上次议价驳回原因"
          placement="top"
        >
          <i
            class="el-icon-warning tb-icon"
            @click="show_reject(scope.row)"
          ></i>
        </el-tooltip>

        <el-tooltip
          v-if="
            powers &&
            powers.length > 0 &&
            powers.some((item) => item == '068') &&
            is_project === '0' &&
            (status === '2' ||
              status === '7' ||
              (status === '3' &&
                scope.row.status !== '3' &&
                scope.row.status !== '4' &&
                scope.row.status !== '6'))
          "
          effect="dark"
          content="发起议价流程"
          placement="top"
        >
          <i
            class="el-icon-s-flag tb-icon"
            @click="wantBargaining(scope.row)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers &&
            powers.length > 0 &&
            powers.some((item) => item == '069') &&
            is_project === '0' &&
            scope.row.status !== '6' &&
            (status == '3' || status == '2' || status == '7')
          "
          effect="dark"
          content="选择该商品"
          placement="top"
        >
          <i
            class="el-icon-s-check tb-icon"
            @click="change_goods(scope.row.bidNo)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="
            powers &&
            powers.length > 0 &&
            powers.some((item) => item == '070') &&
            is_project === '0' &&
            scope.row.status === '6' &&
            status == '4'
          "
          effect="dark"
          content="下单"
          placement="top"
        >
          <i
            class="el-icon-shopping-cart-2 tb-icon"
            @click="buy_goods(scope.row)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    <add-edit-bargain
      :showModel="wantBmodel"
      :sitem="wantBsitem"
      @cancel="wantBmodel = false"
      @refresh="(wantBmodel = false), $emit('resGoodOk')"
    />
    <buy-good-modal
      :showModel="bayGoodShowModel"
      :sitem="bayGoodItem"
      @cancel="bayGoodShowModel = false"
      @refresh="(bayGoodShowModel = false), $emit('resGoodOk')"
    />

    <show-goods-data-table-modal
      :showModel="visible"
      :type="'3'"
      :title="'采返商品详情'"
      :skuCode="row.skuCode"
      :spuCode="row.spuCode"
      :iscgd="true"
      @cancel="visible = false"
    />

    <el-dialog
      title="上次议价驳回原因"
      :visible.sync="show_reject_model"
      width="30%"
      @close="show_reject_model = false"
      append-to-body
    >
      <ul
        v-for="(item, index) in show_reject_item"
        :key="item.id + index"
        style="margin: -20px 0 0 0"
      >
        <li style="line-height: 20px; padding: 0 0 16px 0">
          驳回节点：{{
            (baoptions.find((it) => it.id == item.status) || {}).label || "--"
          }}
        </li>
        <li>驳回理由：{{ item.remark }}</li>
      </ul>
    </el-dialog>
  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/zixunOrder";
import { feedbackListCol } from "../columns";
import { mapGetters } from "vuex";
import addEditBargain from "./addEditBargain";
import buyGoodModal from "./buyGoodModal";
import showGoodsDataTableModal from "@/components/show-goods-data-table-modal";
export default {
  name: "zixunOrder",
  mixins: [mixinPage, resToken],
  props: ["id", "newTime", "sitem"],
  components: {
    addEditBargain,
    buyGoodModal,
    showGoodsDataTableModal,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "zixunOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  watch: {
    newTime: function (val) {
      if (val) {
        const { is_project, status } = this.sitem;
        this.is_project = is_project;
        this.status = status;
        this.searchList();
      }
    },
  },
  data() {
    return {
      row: {},
      visible: false,
      view_spuCode: "",
      view_show: false,
      wantBmodel: false,
      wantBsitem: "",
      select: "1",
      s_input: "",
      status: "",
      bayGoodShowModel: false,
      bayGoodItem: {},
      is_project: false,
      customerCode: [], //客户公司code
      baoptions: [
        { id: "0", label: "待业务主管审核", type: "warning" },
        { id: "1", label: "业务主管通过", type: "success" },
        { id: "2", label: "待财务专员审批", type: "warning" },
        { id: "3", label: "业务主管驳回", type: "danger" },
        { id: "4", label: "待财务主管审核", type: "warning" },
        // { id: "5", label: "专员驳回", type: "" },
        { id: "6", label: "财务主管通过", type: "success" },
        { id: "7", label: "待BOSS审批", type: "warning" },
        { id: "8", label: "财务主管驳回", type: "danger" },
        { id: "9", label: "boss通过", type: "success" },
        { id: "10", label: "boss越线通过", type: "" },
        { id: "11", label: "boss驳回", type: "danger" },
      ],
      options: [
        { id: "0", label: "待发布竞标" },
        { id: "1", label: "招标进行中" },
        { id: "2", label: "招标已结束" },
        { id: "3", label: "待选择商品" },
        { id: "4", label: "已选商品待下单" },
        { id: "5", label: "已成功转单" },
        { id: "6", label: "已取消转单" },
        { id: "7", label: "招标已暂停" },
      ],
      sitem: null,
      // 状态
      statusOptions: [
        { id: "0", label: "采返已结束", type: "info" },
        { id: "1", label: "采返已结束", type: "info" },
        { id: "2", label: "采返已结束", type: "info" },
        { id: "3", label: "等待议价结果", type: "warning" },
        { id: "4", label: "等待议价结果", type: "warning" },
        { id: "5", label: "议价已结束", type: "" },
        { id: "6", label: "已选择该商品", type: "success" },
        // 0待提交1待任务结束 2 待咨询确认 3议价待财务定价4 待主管确认5 待咨询确认定价6 咨询确认
      ],
      loading: true,
      showModel: false,
      isDetail: false,
      show_reject_item: null,
      show_reject_model: false,
      modelId: 0,
      parmValue: {
        zxNo: "",
        infoNo: "",
        bidNo: "",
        projectNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: feedbackListCol,
    };
  },
  mounted() {
    console.log(this.sitem);
    const { is_project, status } = this.sitem;
    this.is_project = is_project;
    this.status = status;
    this.searchList();
  },

  methods: {
    show_view(row) {
      this.row = row;
      this.visible = true;
    },
    restSearch() {
      this.parmValue = {
        zxNo: "",
        infoNo: "",
        bidNo: "",
        projectNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },
    show_reject(row) {
      this.show_reject_item = [];
      this.show_reject_item.push(row.reject);
      this.show_reject_model = true;
    },
    wantBargaining(row) {
      this.wantBsitem = row;
      this.wantBmodel = true;
    },
    openModal(id, isDetail, sitem) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
    },

    // 列表搜索
    async searchList() {
      this.loading = true;
      this.parmValue.infoNo = this.id;

      const res = await asyncRequest.feedList(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },

    async change_goods(bidNo) {
      await this.$confirm("确定要选择该商品下单?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            bidNo: bidNo,
          };
          const res = await asyncRequest.bidscheck(model);
          if (res && res.code === 0) {
            this.loading = false;
            this.$notify.success({
              title: "选择商品成功！",
              message: "",
            });
            this.$emit("resGoodOk");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async statusConfirm(id, status) {
      let str = status === "1" ? "禁用" : "启用";
      await this.$confirm("确定要改为" + str + "?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            id: id,
            status: status === "1" ? "0" : "1",
          };
          const res = await asyncRequest.status(model);
          if (res && res.code === 0) {
            this.loading = false;
            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });
            await this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    buy_goods(row) {
      const { company, khname, platform_name, arrival_time, num } = this.sitem;
      this.bayGoodItem = JSON.parse(JSON.stringify(row));
      this.bayGoodItem.in_company = company;
      this.bayGoodItem.in_khname = khname;
      this.bayGoodItem.in_platform_name = platform_name;
      this.bayGoodItem.in_arrival_time = arrival_time;
      this.bayGoodItem.in_num = num;
      this.bayGoodShowModel = true;

      // console.log(row);
    },
    // 时间选择事件
    async handleTime(e) {
      if (e.startTime !== "") {
        this.parmValue.start = e.startTime;
      } else {
        this.parmValue.start = "";
      }
      if (e.endTime !== "") {
        this.parmValue.end = e.endTime;
      } else {
        this.parmValue.end = "";
      }
      if (this.parmValue.start !== "" && this.parmValue.end !== "") {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    async customerChange(e) {
      if (e && e.id) {
        this.customerCode = [e.code];
        this.parmValue.customer_code = e.code;
      } else {
        this.customerCode = [];
        this.parmValue.customer_code = "";
      }
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
  },
};
</script>
   <style lang="scss" scoped>
.zixunOrder {
}

/deep/ .el-dialog {
  width: 1024px;
}
</style>
   