<template>
  <div class="orderEntry 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;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row :gutter="10">
            <el-col :span="24" style="padding: 0 0 10px 80px">
              <el-col :span="6" style="width: 306px; padding: 0 0 0 0">
                <period-date-picker
                  :start="parmValue.starttime"
                  :end="parmValue.endtime"
                  :type="1"
                  :width="'140px'"
                  :size="searchSize"
                  @timeReturned="timeReturned($event)"
                ></period-date-picker>
              </el-col>
              <el-col :span="8" style="width: 400px">
                <el-input
                  :size="searchSize"
                  v-model="input"
                  placeholder="关键字"
                >
                  <el-select
                    style="width: 125px"
                    v-model="select"
                    slot="prepend"
                    placeholder="请选择"
                  >
                    <el-option label="确认单编号" value="1"></el-option>
                    <el-option label="确认单业务员" value="2"></el-option>
                    <el-option label="采购单编号" value="3"></el-option>
                    <el-option label="采购单业务员" value="4"></el-option>
                  </el-select>
                </el-input>
              </el-col>
              <el-col :span="3" style="width: 60px; float: right">
                <el-button
                  v-if="powers.some((item) => item == '002')"
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 10px"
                  @click="searchList"
                >
                  刷新
                </el-button></el-col
              >
              <el-col :span="3" style="width: 60px; float: right">
                <el-button
                  v-if="powers.some((item) => item == '003')"
                  :size="searchSize"
                  type="success"
                  style="float: right; margin-left: 10px"
                  @click="openModal('add', false)"
                >
                  添加
                </el-button>
              </el-col>
            </el-col>
            <el-col :span="24" style="padding: 0">
              <el-col :span="8" style="width: 470px">
                <search-select
                  :code="code1"
                  :placeholder="'确认单购买方公司名称'"
                  @end="searchChange1"
                  :type="'1'"
                  :size="searchSize"
                />
              </el-col>
              <el-col :span="8" style="width: 470px">
                <search-select
                  :code="code2"
                  :placeholder="'采购单供应商名称'"
                  @end="searchChange2"
                  :type="'2'"
                  :size="searchSize"
                />
              </el-col>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #expand="{ scope }">
        <el-form
          label-position="right"
          size="mini"
          inline
          label-width="130px"
          class="demo-table-expand"
        >
          <el-row>
            <!-- <el-col :span="8">
              <el-form-item size="mini" label="客户属性">
                <span>{{ scope.row.khzzxz }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item size="mini" label="客户分公司">
                <span>{{ scope.row.khcomp }}</span>
              </el-form-item>
            </el-col> -->
            <!-- <el-col :span="8">
              <el-form-item size="mini" label="平台">
                <span>{{ scope.row.suppitem }}</span>
              </el-form-item>
            </el-col> -->
            <el-col :span="12">
              <el-form-item size="mini" label="商品名称">
                <span>{{ scope.row.product_name }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="部门">
                <span>{{ scope.row.depart }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="单价">
                <span>{{ scope.row.sale_price }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="数量">
                <span>{{ scope.row.order_num }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="单据号">
                <span>{{ scope.row.sequenceNo }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="PO编号">
                <span>{{ scope.row.poNo }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="税点">
                <span>{{ scope.row.tax }}%</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="确认单类型">
                <span>{{ scope.row.qrd_type }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item size="mini" label="已回款总金额">
                <span>{{ scope.row.apay_fee }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="未回款总金额">
                <span>{{ scope.row.wpay_fee }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item size="mini" label="确认单开票金额">
                <span>{{ scope.row.ainv_fee }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="确认单未开票金额">
                <span>{{ scope.row.winv_fee }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <template #status="{ scope }">
        <el-tag
          :type="scope.row.status == '0' ? 'warning' : ''"
          :size="size"
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #operation="{ scope }">
        <el-tooltip
          effect="dark"
          content="详情"
          v-if="powers.some((item) => item == '007')"
          placement="top"
        >
          <i
            class="el-icon-view tb-icon"
            @click="openModal(scope.row.id, true)"
          ></i>
        </el-tooltip>
        <el-tooltip
          effect="dark"
          content="修改"
          v-if="powers.some((item) => item == '005')"
          placement="top"
        >
          <i
            class="el-icon-edit tb-icon"
            @click="openModal(scope.row.id, false)"
          ></i>
        </el-tooltip>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
    <!-- 弹窗 新增/修改 -->
    <add-edit
      :id="modelId"
      :show-model="showModel"
      :is-detail="isDetail"
      :contector="contector"
      :name="name"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/network/orderEntry";
import ExTable from "@/components/ExTableNew.vue";
import mixinPage from "@/mixins/elPaginationHandle";
import addEdit from "./addEdit";
import PeriodDatePicker from "@/components/PeriodDatePicker";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "orderEntry",
  components: {
    ExTable,
    addEdit,
    PeriodDatePicker,
  },
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "orderEntry"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  data() {
    return {
      code1: [],
      code2: [],
      select: "1",
      input: "",
      isDetail: false,
      // 状态
      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;
        },
      },
      showModel: false,
      loading: false,
      modelId: 0,
      parmValue: {
        qrdNo: "", //确认单编号
        cgdNo: "", //采购单编号
        sale_name: "", //业务员
        cgd_sale: "", //采购员
        endtime: "", //完成时间
        starttime: "", //开始下单时间
        khNo: "", //客户code
        supplierNo: "", //供应商编号
        page: 1, // 页码
        size: 15, // 每页显示条数
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          type: "expand",
          label: "",
          _slot_: "expand",
        },
        {
          prop: "qrdNo",
          label: "确认单编号",
          width: "165px",
          // fixed: "left",
        },
        {
          prop: "sale_total",
          label: "销售总额",
          width: "100px",
        },
        {
          prop: "sale_name",
          label: "业务员",
          width: "70px",
        },
        {
          prop: "khNo",
          label: "企业客户编码",
          width: "125px",
        },
        {
          prop: "khName",
          label: "企业客户",
          "min-width": "190px",
        },
        {
          prop: "companyNo",
          label: "确认单销售公司编码",
          width: "175px",
        },
        {
          prop: "supperinfo",
          label: "确认单销售公司",
          "min-width": "190px",
        },

        {
          prop: "cgdNo",
          label: "采购单号",
          width: "165px",
        },
        {
          prop: "supplierNo",
          label: "供应商编号",
          width: "140px",
        },
        {
          prop: "supplier",
          label: "供应商",
          "min-width": "190px",
        },
        // {
        //   prop: "suppitem",
        //   label: "平台",
        // },
        // {
        //   prop: "status",
        //   label: "状态",
        //   width: "90px",
        //   _slot_: "status",
        // },
        // {
        //   prop: "poNo",
        //   label: "po编号",
        // },

        {
          prop: "ordertime",
          label: "创建时间",
          width: "140px",
        },
        {
          prop: "",
          label: "操作",
          width: "85px",
          fixed: "right",
          _noset_: true,
          _slot_: "operation",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    async searchList() {
      this.loading = true;
      console.log("12345");
      this.parmValue.qrdNo = this.select === "1" ? this.input : "";
      this.parmValue.cgdNo = this.select === "2" ? this.input : "";
      this.parmValue.sale_name = this.select === "3" ? this.input : "";
      this.parmValue.cgd_sale = this.select === "4" ? this.input : "";
      this.parmValue.khNo = this.code1.join(",");
      this.parmValue.supplierNo = this.code2.join(",");
      const res = await asyncRequest.list(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach((v1) => {
          v1.apay_fee = this.setnum(v1.apay_fee);
          v1.wpay_fee = this.setnum(v1.wpay_fee);
          v1.ainv_fee = this.setnum(v1.ainv_fee);
          v1.winv_fee = this.setnum(v1.winv_fee);
        });
        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;
    },
    setnum(s) {
      let str = "";
      if (isNaN(s)) {
        str = "0";
      } else if (typeof s == "undefined") {
        str = "0";
      } else if (!s && typeof s != "undefined" && s != 0) {
        str = "0";
      } else {
        str = parseFloat(s + "").toFixed(2);
      }
      return str;
    },
    async searchChange1(e) {
      this.code1 = JSON.parse(JSON.stringify(e));
      this.parmValue.page = 1;
      await this.searchList();
    },
    async searchChange2(e) {
      this.code2 = JSON.parse(JSON.stringify(e));
      this.parmValue.page = 1;
      await this.searchList();
    },
    async timeReturned(e) {
      if (e.startTime !== "") {
        this.parmValue.starttime = e.startTime;
      } else {
        this.parmValue.starttime = "";
      }

      if (e.endTime !== "") {
        this.parmValue.endtime = e.endTime;
      } else {
        this.parmValue.endtime = "";
      }
      if (this.parmValue.starttime !== "" && this.parmValue.endtime !== "") {
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    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>
.orderEntry {
  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>
