<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="{ selection }">
        <div style="width: 100%">
          <el-row style="padding: 0 0 10px 80px">
            <el-col :span="8" style="width: 470px">
              <el-select
                v-model="code"
                multiple
                filterable
                remote
                :multiple-limit="1"
                reserve-keyword
                :size="searchSize"
                style="width: 100%"
                placeholder="供应商名称"
                :remote-method="remoteMethod"
                :loading="selectLoading"
                @change="selectChange"
              >
                <el-option
                  v-for="item in activeOptions"
                  :key="item.code"
                  :label="item.name + '--------' + item.contector"
                  :value="item.code"
                />
              </el-select>
            </el-col>

            <el-col
              :span="3"
              style="width: 140px; float: right"
              v-if="powers.some((item) => item == '041')"
            >
              <el-button
                :size="searchSize"
                type="primary"
                class="fr"
                @click="setBilling(selection)"
              >
                批量设置回票信息</el-button
              ></el-col
            >
            <el-col
              :span="3"
              style="width: 140px; float: right"
              v-if="powers.some((item) => item == '042')"
            >
              <el-button
                :size="searchSize"
                type="primary"
                style="float: right"
                @click="examBilling(selection)"
              >
                批量审核回票信息
              </el-button>
            </el-col>
          </el-row>
          <el-row style="padding: 0 0 10px 0">
            <el-col :span="8" style="width: 120px">
              <el-select
                v-model="parmValue.send_status"
                clearable
                :size="searchSize"
                placeholder="发货状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in send_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8" style="width: 130px; padding-left: 10px">
              <el-select
                v-model="parmValue.payinfo"
                clearable
                :size="searchSize"
                placeholder="付款状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in pay_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8" style="width: 130px; padding-left: 10px">
              <el-select
                v-model="parmValue.invinfo"
                clearable
                :size="searchSize"
                placeholder="回票状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in inv_status_list"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col :span="8" style="width: 130px; padding-left: 10px">
              <el-select
                v-model="parmValue.th_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="8" style="width: 160px; padding: 0 0 0 10px">
              <el-select
                v-model="parmValue.inv_status"
                clearable
                :size="searchSize"
                placeholder="回票信息状态"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-col>
            <el-col
              :span="3"
              style="width: 140px; float: right"
              v-if="powers.some((item) => item == '050')"
            >
              <el-button
                :size="searchSize"
                type="primary"
                style="float: right"
                @click="statusConfirm(selection)"
              >
                批量设置采购单无需对账状态
              </el-button>
            </el-col>
          </el-row>
          <el-row :gutter="10">
            <el-col :span="5" style="width: 390px">
              <el-input
                clearable
                style="width: 100%"
                :size="searchSize"
                placeholder="关键字"
                v-model="input"
              >
                <el-select
                  v-model="selectK"
                  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="3" style="width: 84px; float: right">
              <el-button
                type="primary"
                icon="el-icon-download"
                class="fr"
                v-if="powers.some((item) => item == '051')"
                @click="batchExport(selection)"
                :size="searchSize"
                style="margin-left: 10px"
                >导出</el-button
              >
            </el-col>
            <el-col :span="3" style="width: 66px; float: right">
              <el-button
                :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"
                :size="searchSize"
                @click="restSearch"
              >
                重置
              </el-button>
            </el-col>
          </el-row>
        </div>
      </template>
      <template #expand="{ scope }">
        <el-form
          label-position="left"
          size="mini"
          inline
          label-width="70px"
          class="demo-table-expand"
        >
          <el-row>
            <el-col :span="4">
              <el-form-item size="mini" label="商品编号">
                <span>{{ scope.row.goodNo }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item size="mini" label="商品名称">
                <span>{{ scope.row.goodName }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="商品描述">
                <span>{{ scope.row.goodDesc }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="单位">
                <span>{{ scope.row.goodUnit }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="数量">
                <span>{{ scope.row.orderNum }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="含有工差">
                <span>{{ scope.row.isDiff }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="工期">
                <span>{{ scope.row.workDay }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="成本合计">
                <span>{{ scope.row.pay_price }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="税率">
                <span>{{ scope.row.taxPoint }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="成本裸价">
                <span>{{ scope.row.nakedPrice }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="加标费">
                <span>{{ scope.row.markPrice }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="包装费">
                <span>{{ scope.row.packPrice }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="证书费">
                <span>{{ scope.row.certPrice }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="开模费">
                <span>{{ scope.row.openPrice }}</span>
              </el-form-item>
            </el-col>

            <el-col :span="4">
              <el-form-item size="mini" label="物流费">
                <span>{{ scope.row.postPrice }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="工差">
                <span>{{ scope.row.diff_fee }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="商品类型">
                <span>{{ scope.row.goodType }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="商品品牌">
                <span>{{ scope.row.goodBank }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="商品型号">
                <span>{{ scope.row.goodModel }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="商品材质">
                <span>{{ scope.row.goodMaterial }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="商品颜色">
                <span>{{ scope.row.goodColor }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="金属种类">
                <span>{{ scope.row.goodCat }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="金属重量">
                <span>{{ scope.row.weight }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item size="mini" label="金属价格">
                <span>{{ scope.row.price }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </template>
      <template #inv_status="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.inv_status == '3'
              ? 'danger'
              : scope.row.inv_status == '2'
              ? 'success'
              : 'warning'
          "
          v-text="
            (options.find((item) => item.value == scope.row.inv_status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #th_info="{ scope }">
        <el-tag
          size="mini"
          :type="scope.row.th_info == '1' ? 'success' : 'danger'"
          v-text="
            (
              th_status_list.find((item) => item.value == scope.row.th_info) ||
              {}
            ).label || '--'
          "
        ></el-tag>
      </template>
      <template #send_info="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.send_info == '3'
              ? 'success'
              : scope.row.send_info == '2'
              ? 'warning'
              : 'danger'
          "
          v-text="
            (
              send_status_list.find(
                (item) => item.value == scope.row.send_info
              ) || {}
            ).label || '--'
          "
        ></el-tag>
      </template>
      <template #payinfo="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.payinfo == '3'
              ? 'success'
              : scope.row.payinfo == '2'
              ? 'warning'
              : 'danger'
          "
          v-text="
            (
              pay_status_list.find((item) => item.value == scope.row.payinfo) ||
              {}
            ).label || '--'
          "
        ></el-tag>
      </template>
      <template #invinfo="{ scope }">
        <el-tag
          size="mini"
          :type="
            scope.row.invinfo == '3'
              ? 'success'
              : scope.row.invinfo == '2'
              ? 'warning'
              : 'danger'
          "
          v-text="
            (
              inv_status_list.find((item) => item.value == scope.row.invinfo) ||
              {}
            ).label || '--'
          "
        ></el-tag>
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
    <!-- 设置回票信息 -->
    <set-edit
      :tableData="selection"
      :show-model="showModel1"
      @refresh="searchList"
      @cancel="showModel1 = false"
    />
    <!-- 审核回票信息 -->
    <exam-edit
      :tableData="selection"
      :show-model="showModel2"
      @refresh="searchList"
      @cancel="showModel2 = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/purchase/porder";
import ExTable from "@/components/ExTableNew.vue";
import mixinPage from "@/mixins/elPaginationHandle";
import { switchingTime, formatJson } from "@/utils/publicMethods";
import resToken from "@/mixins/resToken";
import setEdit from "./setEdit";
import { mapGetters } from "vuex";
import examEdit from "./examEdit";
import {
  columns,
  selectKlist,
  options,
  th_status_list,
  send_status_list,
  pay_status_list,
  inv_status_list,
  tHeader,
  filterVal,
} from "./tableVariable";
export default {
  name: "porder",
  components: {
    ExTable,
    setEdit,
    examEdit,
  },
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "porder"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      selectK: "1",
      input: "",
      //输入框搜索类型
      selectKlist: selectKlist,
      code: [],
      showModel1: false,
      showModel2: false,
      selectLoading: false,
      activeOptions: [],
      selection: [],
      //回票状态
      options: options,
      //退货状态
      th_status_list: th_status_list,
      //发货状态
      send_status_list: send_status_list,
      //付款状态
      pay_status_list: pay_status_list,
      //回票状态
      inv_status_list: inv_status_list,
      // 状态
      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: {
        name: "", // 账户
        code: "", // code
        sequenceNo: "",
        inv_company: "",
        inv_status: "",
        send_status: "",
        payinfo: "",
        invinfo: "",
        goodNo: "",
        th_status: "",
        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: columns,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    async restSearch() {
      this.selectK = "1";
      this.input = "";
      this.code = [];
      this.parmValue.code = this.code.join(",");
      this.parmValue.sequenceNo = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      this.parmValue.inv_company = "";
      this.parmValue.inv_status = "";
      this.parmValue.goodNo = "";
      this.parmValue.send_status = "";
      this.parmValue.owner = "";
      await this.searchList();
    },

    async statusConfirm(selection) {
      if (selection && selection.length === 0) {
        this.$message.error("请选择采购单！");
        return;
      }

      await this.$confirm(`确定要将选中的采购单改为无需对账状态？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          let id = "";
          selection.forEach((v1, i1) => {
            id += i1 === 0 ? v1.id : "," + v1.id;
          });
          const model = {
            id: id,
          };
          const res = await asyncRequest.paymentunpay(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("取消");
        });
    },
    async searchList() {
      this.loading = true;
      this.parmValue.sequenceNo = this.selectK === "1" ? this.input : "";
      this.parmValue.inv_company = this.selectK === "2" ? this.input : "";
      this.parmValue.owner = this.selectK === "3" ? this.input : "";
      this.parmValue.goodNo = this.selectK === "4" ? this.input : "";
      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;
    },
    async selectChange() {
      console.log(this.code);
      this.parmValue.code = this.code.join(",");
      this.formValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    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 = this.setNum(v1.purchasePrice);
        v1.totalPrice = this.setNum(v1.totalPrice);
        v1.pay_fee = this.setNum(v1.pay_fee);
        v1.wait_fee = this.setNum(v1.wait_fee);
        v1.price = this.setNum(v1.price);
        v1.nakedPrice = this.setNum(v1.nakedPrice);
        v1.markPrice = this.setNum(v1.markPrice);
        v1.packPrice = this.setNum(v1.packPrice);
        v1.certPrice = this.setNum(v1.certPrice);
        v1.openPrice = this.setNum(v1.openPrice);
        v1.craftPrice = this.setNum(v1.craftPrice);
        v1.postPrice = this.setNum(v1.postPrice);
        v1.weight = this.setNum(v1.weight);
        v1.diff_fee = this.setNum(v1.diff_fee);
        v1.price = this.setNum(v1.price);
        v1.inv_open_fee = this.setNum(v1.inv_open_fee);
        v1.inv_wait_fee = this.setNum(v1.inv_wait_fee);
        return v1;
      });
    },
    setNum(s) {
      return s ? parseFloat(s + "").toFixed(2) : "-";
    },
    async remoteMethod(query) {
      this.selectLoading = true;
      if (query !== "") {
        this.activeOptions = [];
        this.formValue.name = query;
        const res = await asyncRequest.supplierlist(this.formValue);
        if (res && res.code === 0 && res.data && res.data.list) {
          this.activeOptions = res.data.list;
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      } else {
        this.activeOptions = [];
      }
      this.selectLoading = false;
    },
    setlable(list, type) {
      return (list.find((item) => item.value == type) || {}).label || "";
    },
    /**
     *  批量导出
     * @param {Array} selection 当前选择项
     */
    batchExport(selection) {
      if (!this.loading) {
        if (selection.length == 0) {
          this.$message.warning("请至少选择一条数据！");
          return;
        }

        this.loading = true;
        let data = [].concat(...selection);
        let list = [];
        data.forEach((v1) => {
          let model = Object.assign({}, v1);
          model.send_info = this.setlable(this.send_status_list, v1.send_info);
          model.th_info = this.setlable(this.th_status_list, v1.th_info);
          model.payinfo = this.setlable(this.pay_status_list, v1.payinfo);
          model.invinfo = this.setlable(this.inv_status_list, v1.invinfo);
          model.inv_status = this.setlable(this.options, v1.inv_status);
          list.push(model);
        });
        const xlsName = `采购单导出`;
        import("@/vendor/Export2Excel").then((excel) => {
          const data = formatJson(filterVal, list);
          excel.export_json_to_excel({
            header: tHeader,
            data,
            filename: `${xlsName}`,
          });
          this.$message.success("采购单导出成功！");
          setTimeout(() => {
            this.loading = false;
          }, 500);
        });
      }
    },
  },
};
</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>
