<template>
  <div>
    <el-dialog title="详情" :visible.sync="dialogTableVisible" width="1280px">
      <detailDatatable
        :newTime="newTime"
        v-if="newTime !== '' && sitem"
        :sitem="sitem"
        :columns="columns"
        :border="true"
      >
        <template slot="status">
          <el-tag
            :size="'mini'"
            :type="sitem.status == '0' ? 'warning' : ''"
            v-text="
              (statusOptions.find((item) => item.value == sitem.status) || {}).label ||
              '--'
            "
          ></el-tag>
        </template>
        <template slot="good_price">
          <span
            v-if="
              newTime !== '' &&
              private_field &&
              private_field.length > 0 &&
              private_field.some((item) => item == '1')
            "
            >{{ sitem.good_price }}</span
          >
          <span v-else>******</span>
        </template>

        <template slot="return_price">
          <span
            v-if="
              newTime !== '' &&
              private_field &&
              private_field.length > 0 &&
              private_field.some((item) => item == '1')
            "
            >{{ sitem.return_price }}</span
          >
          <span v-else>******</span>
        </template>
      </detailDatatable>
    </el-dialog>

    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="listColumns"
      :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 #order_type="{ scope }">
        <p
          v-text="
            (cg_order_type_options.find((item) => item.id == scope.row.order_type) || {})
              .label || '--'
          "
        ></p>
      </template>
      <template #operation="{ scope }">
        <el-tooltip effect="dark" content="详情" placement="top">
          <i class="el-icon-view tb-icon" @click="dilog(scope.row.returnCode)"></i>
        </el-tooltip>
      </template>
    </ex-table>
  </div>
</template>

<script>
import { columns, statusOptions, listColumns } 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/purchaseIn/purchaseReturn";

import { mapGetters } from "vuex";
import detailDatatable from "../detail-data-table";

export default {
  mixins: [mixinPage, resToken],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size", "private_field"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "standingBookDetail"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  components: {
    detailDatatable,
  },
  props: ["queryId"],
  data() {
    return {
      listColumns,
      loading: true,
      dialogTableVisible: false,
      parmValue: {
        cgdNo: "", //采购单编号
        returnCode: "", //退货单编号
        good_type_code: "", //商品属性编号
        good_name: "", //商品名称
        status: "", //节点状态
        status: "", //节点状态
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },

      cg_order_type_options,
      statusOptions,
      status: "",
      orderCode: "",

      //——————————————————————
      columns,
      newTime: "",
      sitem: null,
      loading: false,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async initData(returnCode) {
      this.loading = true;
      const res = await asyncRequest.detail({ returnCode: returnCode });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        const { can, status, bk_code } = this.sitem;
        this.status = status;
        this.orderCode = bk_code;
        if (can && can.length > 0) {
          this.sitem.class_cat = "";
          can.forEach((x, i) => {
            this.sitem.class_cat += i === 0 ? x.name : "/" + x.name;
          });
        }
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.sitem = {};
        this.status = 0;
      }
      this.loading = false;
    },

    async searchList() {
      //通过采购编号去查询所有列表、当采购编号为空时，所有涉及字段不展示
      this.parmValue.cgdNo = this.queryId;
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
        this.tableData.forEach((v) => {
          v.good_class = "";
          if (v.can && v.can.length > 0) {
            v.can.forEach((x, i) => {
              v.good_class += i === 0 ? x.name : "/" + x.name;
            });
          }
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    dilog(returnCode) {
      this.dialogTableVisible = true;
      this.initData(returnCode);
    },
  },
};
</script>

<style></style>
