<template>
  <div class="porder pagePadding">
    <ex-table
      v-loading="loading"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
      :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 #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 0 80px">
            <el-col :span="8" style="width: 120px">
              <el-select
                v-model="parmValue.status"
                clearable
                :size="searchSize"
                placeholder="退货状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in th_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>

            <el-col :span="5" style="width: 350px; padding: 0 0 0 10px">
              <el-input
                clearable
                style="width: 100%"
                :size="searchSize"
                placeholder="关键字"
                v-model="parmValue.codeNo"
              >
                <el-select
                  v-model="parmValue.type"
                  slot="prepend"
                  style="width: 125px"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in selectKlist"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click.native="searchList"
                ></el-button>
              </el-input>
            </el-col>
            <el-col :span="8" style="width: 270px; padding: 0 0 0 10px">
              <el-input
                clearable
                style="width: 100%"
                :size="searchSize"
                placeholder="退货供应商名称"
                v-model="parmValue.company"
              >
              </el-input>
            </el-col>
            <el-col :span="3" style="width: 66px; float: right">
              <el-button
                v-if="powers.some((item) => item == '002')"
                :size="searchSize"
                type="primary"
                class="fr"
                style="margin-left: 10px"
                @click="searchList"
              >
                刷新
              </el-button>
            </el-col>
            <el-col :span="3" style="width: 66px; float: right">
              <el-button
                type="warning"
                class="fr"
                v-if="powers.some((item) => item == '024')"
                :size="searchSize"
                @click="restSearch"
              >
                重置
              </el-button>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #th_status="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.th_status == '3'
              ? 'danger'
              : scope.row.th_status == '2'
              ? 'success'
              : 'warning'
          "
          v-text="
            (
              th_status_list.find(
                (item) => item.value == scope.row.th_status
              ) || {}
            ).label || '--'
          "
        ></el-tag>
      </template>
      <template #is_th="{ scope }">
        <el-tag
          size="mini"
          :type="scope.row.is_th == '1' ? 'success' : 'warning'"
          >{{ scope.row.is_th == "1" ? "同意" : "不同意" }}</el-tag
        >
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/purchase/pordeRGoods";
import ExTable from "@/components/ExTableNew.vue";
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import { mapGetters } from "vuex";
export default {
  name: "pordeRGoods",
  components: {
    ExTable,
  },
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "pordeRGoods"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      selectKlist: [
        {
          code: "1",
          name: "采购单编码",
        },
        {
          code: "2",
          name: "确认单号",
        },
        {
          code: "3",
          name: "确认单产品单号",
        },
      ],
      code: [],
      showModel1: false,
      showModel2: false,
      selectLoading: false,
      activeOptions: [],
      selection: [],
      th_status_list: [
        {
          value: "1",
          label: "已完成",
        },
        {
          value: "2",
          label: "进行中",
        },
        {
          value: "3",
          label: "已取消",
        },
      ],
      // 状态
      statusOptions: [
        { id: "0", label: "禁用" },
        { id: "1", label: "启用" },
      ],
      pickerOptions: {
        // disabledDate是一个函数,参数是当前选中的日期值,这个函数需要返回一个Boolean值,
        disabledDate(time) {
          let date = new Date().valueOf();
          const one = 1000 * 60 * 60 * 24;
          date = parseInt(date / one) * one;
          return time.getTime() <= date;
        },
      },
      loading: false,
      modelId: 0,
      parmValue: {
        status: "",
        type: "1",
        company: "",
        codeNo: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      formValue: {
        name: "", // 账户
        page: 1, // 页码
        size: 100, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "thNo",
          label: "退货单号",
          width: "125px",
          fixed: "left",
        },
        {
          prop: "th_status",
          label: "退货状态",
          _slot_: "th_status",
          width: "75px",
        },

        {
          prop: "th_qrdNo",
          label: "确认单号",
          width: "135px",
        },
        {
          prop: "th_qrdcpNo",
          label: "确认单产品号",
          width: "155px",
        },
        {
          prop: "th_cgdNo",
          label: "采购单号",
          width: "135px",
        },
        {
          prop: "th_company",
          label: "退货供货商",
          "min-width": "120px",
        },
        {
          prop: "th_user",
          label: "申请人",
          width: "65px",
        },
        {
          prop: "th_num",
          label: "退货数量",
          width: "90px",
        },
        {
          prop: "th_qrd_fee",
          label: "退货确认单金额",
          width: "110px",
        },
        {
          prop: "th_cgd_fee",
          label: "退货采购单金额",
          width: "110px",
        },
        {
          prop: "th_post_fee",
          label: "快递费",
          width: "80px",
        },
        {
          prop: "is_th",
          label: "供货商支出退货",
          _slot_: "is_th",
          width: "110px",
        },
        {
          prop: "th_remark",
          label: "退回备注",
          width: "110px",
        },

        {
          prop: "createtime",
          label: "下单时间",
          width: "145px",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    async restSearch() {
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      this.parmValue.type = "1";
      this.parmValue.status = "";
      this.parmValue.codeNo = "";
      await this.searchList();
    },
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
        this.formatGoodList();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    setBilling(selection) {
      this.selection = [];
      if (selection && selection.length === 0) {
        this.$message.error("请选择采购单");
        return;
      }
      this.selection = selection;

      this.showModel1 = true;
      this.showModel2 = false;
      console.log(selection);
    },

    examBilling(selection) {
      if (selection && selection.length === 0) {
        this.$message.error("请选择采购单");
        return;
      }
      let isok = true;
      selection.forEach((v1) => {
        if (v1.inv_status !== "1") {
          isok = false;
        }
      });
      if (!isok) {
        this.$message.error("只有待审核的采购单才能进行审核操作！");
        return;
      }
      this.selection = selection;
      console.log(selection);
      this.showModel1 = false;
      this.showModel2 = true;
    },
    formatGoodList() {
      this.tableData.map((v1) => {
        v1.purchasePrice = v1.purchasePrice
          ? parseFloat(v1.purchasePrice + "").toFixed(2)
          : "-";
        v1.totalPrice = v1.totalPrice
          ? parseFloat(v1.totalPrice + "").toFixed(2)
          : "-";

        v1.pay_fee = v1.pay_fee ? parseFloat(v1.pay_fee + "").toFixed(2) : "-";
        v1.wait_fee = v1.wait_fee
          ? parseFloat(v1.wait_fee + "").toFixed(2)
          : "-";
        v1.price = v1.price ? parseFloat(v1.price + "").toFixed(2) : "-";
        v1.nakedPrice = v1.nakedPrice
          ? parseFloat(v1.nakedPrice + "").toFixed(2)
          : "-";
        v1.markPrice = v1.markPrice
          ? parseFloat(v1.markPrice + "").toFixed(2)
          : "-";

        v1.packPrice = v1.packPrice
          ? parseFloat(v1.packPrice + "").toFixed(2)
          : "-";
        v1.certPrice = v1.certPrice
          ? parseFloat(v1.certPrice + "").toFixed(2)
          : "-";

        v1.openPrice = v1.openPrice
          ? parseFloat(v1.openPrice + "").toFixed(2)
          : "-";
        v1.craftPrice = v1.certPrice
          ? parseFloat(v1.craftPrice + "").toFixed(2)
          : "-";

        v1.postPrice = v1.postPrice
          ? parseFloat(v1.postPrice + "").toFixed(2)
          : "-";
        v1.weight = v1.weight ? parseFloat(v1.weight + "").toFixed(2) : "-";

        v1.diff_fee = v1.diff_fee
          ? parseFloat(v1.diff_fee + "").toFixed(2)
          : "-";
        v1.price = v1.price ? parseFloat(v1.price + "").toFixed(2) : "-";

        v1.inv_open_fee = v1.inv_open_fee
          ? parseFloat(v1.inv_open_fee + "").toFixed(2)
          : "-";
        v1.inv_wait_fee = v1.inv_wait_fee
          ? parseFloat(v1.inv_wait_fee + "").toFixed(2)
          : "-";

        return v1;
      });
    },
    async statusConfirm(id, status) {
      await this.$confirm(`确定要改为${status === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            companyNo: 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.loading = false;
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.porder {
  background: rgba(242, 242, 242, 1);
  min-height: 100%;
  width: 100%;
  height: 100%;
  // background-color: $bg;
  overflow: hidden;

  // position: absolute;
  z-index: 2;
  background: #fff;
  .change {
    .title {
      border-width: 0px;
      width: 100%;
      height: 100px;
      // display: flex;
      font-weight: 400;
      font-style: normal;
      font-size: 22px;
      text-align: left;
      padding: 0;
      padding: 60px 0 0 100px;
      width: 100%;
      box-sizing: border-box;
    }
  }
}
</style>
