<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="6vh"
    width="1040px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    append-to-body
    @close="closeModel"
  >
    <el-card style="margin-top: -15px">
      <el-row>
        <el-col :span="24" style="padding-bottom: 10px">
          <el-select
            v-model="parmValue.send_status"
            clearable
            :size="searchSize"
            placeholder="发货状态"
            style="width: 120px"
            @change="
              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-input
            placeholder="采购单编号"
            style="width: 300px; margin-left: 10px"
            v-model="parmValue.sequenceNo"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click.native="(parmValue.page = 1), searchList()"
            ></el-button>
          </el-input>

          <el-button
            class="fr"
            type="warning"
            @click.native="
              (parmValue.sequenceNo = ''), (parmValue.page = 1), searchList()
            "
            >重置</el-button
          >
        </el-col>
        <el-col :span="24">
          <el-table
            tooltip-effect="dark"
            ref="multipleTable"
            v-loading="loading"
            :data="tableData"
            :size="'mini'"
            stripe
            border
            row-key="index"
            height="500"
            @selection-change="handleSelectionChange"
            :span-method="objectSpanMethod"
            style="width: 100%"
          >
            <el-table-column
              type="selection"
              width="40"
              fixed="left"
              align="center"
            >
            </el-table-column>
            <el-table-column label="采购单信息" align="center">
              <el-table-column
                prop="cgdNO"
                label="采购单编号"
                show-overflow-tooltip
                width="135px"
              />
              <el-table-column
                prop="purchasePrice"
                label="货款"
                show-overflow-tooltip
              />
              <el-table-column
                prop="inv_company_name"
                label="回票公司"
                show-overflow-tooltip
                width="180px"
              />
              <el-table-column
                prop="name"
                label="供货商"
                show-overflow-tooltip
              />
              <el-table-column
                prop="contector"
                label="联系人"
                width="65"
                show-overflow-tooltip
              />
              <el-table-column
                prop="ownerName"
                label="采购员"
                width="65"
                show-overflow-tooltip
              />

              <el-table-column label="发货状态" width="85">
                <template slot-scope="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>
                  <!-- <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
        <el-button type="text" size="small">编辑</el-button> -->
                </template>
              </el-table-column>
            </el-table-column>
            <el-table-column label="商品基础信息" align="center">
              <el-table-column
                label="商品编号"
                prop="goodNo"
                show-overflow-tooltip
              />
              <el-table-column
                label="名称"
                prop="goodName"
                show-overflow-tooltip
              />

              <el-table-column
                prop="goodUnit"
                label="单位"
                show-overflow-tooltip
              />
              <el-table-column
                prop="orderNum"
                label="数量"
                show-overflow-tooltip
              />
              <el-table-column
                prop="isDiff"
                label="含有工差"
                show-overflow-tooltip
              />
              <el-table-column
                prop="workDay"
                label="工期"
                show-overflow-tooltip
              />
            </el-table-column>
            <el-table-column label="商品费用信息" align="center">
              <el-table-column
                prop="pay_price"
                label="成本合计"
                show-overflow-tooltip
              />
              <el-table-column
                prop="pay_fee"
                label="已付款"
                show-overflow-tooltip
              />
              <el-table-column
                prop="wait_fee"
                label="待付款"
                show-overflow-tooltip
              />
              <el-table-column
                prop="taxPoint"
                label="税率"
                show-overflow-tooltip
              />
              <el-table-column
                prop="nakedPrice"
                label="成本裸价"
                show-overflow-tooltip
              />
              <el-table-column
                prop="markPrice"
                label="加标费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="packPrice"
                label="包装费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="certPrice"
                label="证书费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="openPrice"
                label="开模费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="craftPrice"
                label="工艺费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="postPrice"
                label="物流费"
                show-overflow-tooltip
              />
              <el-table-column
                prop="diff_fee"
                label="工差金额"
                show-overflow-tooltip
              />
              <el-table-column
                prop="diff_info"
                label="工差"
                show-overflow-tooltip
              />
            </el-table-column>
            <el-table-column label="商品属性" align="center">
              <el-table-column
                label="类型"
                prop="goodType"
                show-overflow-tooltip
              />
              <el-table-column
                label="品牌"
                prop="goodBank"
                show-overflow-tooltip
              />
              <el-table-column
                label="型号"
                prop="goodModel"
                show-overflow-tooltip
              />
              <el-table-column
                label="材质"
                prop="goodMaterial"
                show-overflow-tooltip
              />

              <el-table-column
                label="颜色"
                prop="goodColor"
                show-overflow-tooltip
              />

              <el-table-column
                prop="goodDesc"
                label="描述"
                show-overflow-tooltip
              />
            </el-table-column>
            <el-table-column label="贵金属属性" align="center">
              <el-table-column
                prop="goodCat"
                label="种类"
                show-overflow-tooltip
              />

              <el-table-column
                prop="weight"
                label="重量"
                show-overflow-tooltip
              />
              <el-table-column
                prop="price"
                label="实时价格"
                show-overflow-tooltip
              />
            </el-table-column>
          </el-table>

          <el-row :gutter="10">
            <el-col :span="18">
              <div
                class="Pagination"
                style="text-align: center; margin-top: 12px"
              >
                <el-pagination
                  :current-page="parmValue.page"
                  :page-sizes="[10, 20, 30, 40, 50]"
                  :page-size="parmValue.size"
                  :size="searchSize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="count"
                  @size-change="handleSizeChange"
                  @current-change="handlePageChange"
                />
              </div>
            </el-col>
            <el-col :span="6" style="text-align: right">
              <div style="margin-top: 12px">
                <el-button
                  v-if="!isDetail"
                  type="primary"
                  size="medium"
                  @click="submitForm"
                  >保 存
                </el-button>
                <el-button @click="showModelThis = false" size="medium">{{
                  isDetail ? "关 闭" : "取 消"
                }}</el-button>
              </div>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/purchase/orderRecord";
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
export default {
  name: "orderRecord",
  props: [
    "showModel",
    "id",
    "isDetail",
    "companyNo",
    "companyNoCode",
    "companyNoName",
  ],
  mixins: [mixinPage, resToken],
  data() {
    return {
      loading: false,
      title: "选择采购单商品",
      loading: false,
      count: 0,
      send_status_list: [
        {
          value: "1",
          label: "未发货",
        },
        {
          value: "2",
          label: "部分发货",
        },
        {
          value: "3",
          label: "全部发货",
        },
      ],
      parmValue: {
        name: "", // 账户
        code: "", // code
        sequenceNo: "",
        inv_company: "",
        inv_status: "2",
        send_status: "",
        page: 1, // 页码
        size: 10, // 每页显示条数
      },
      showModelThis: this.showModel,
      tableData: [],
      activeOptions: [],
      multipleSelection: [],
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    //格式化表格数据
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      // if (columnIndex === 1) {
      //   if (row.dingwei === 0) {
      //     return {
      //       rowspan: row.total,
      //       colspan: 1,
      //     };
      //   } else {
      //     return {
      //       rowspan: 0,
      //       colspan: 0,
      //     };
      //   }
      // } else {
      //   return {
      //     rowspan: 1,
      //     colspan: 1,
      //   };
      // }
    },
    closeModel() {
      console.log("closeModel!!");
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    async initForm() {
      await this.searchList();
    },
    async submitForm() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning("请选择商品！");
      } else {
        if (this.companyNoCode !== "") {
          let isok = true;

          this.multipleSelection.forEach((v1) => {
            if (v1.inv_company !== this.companyNoCode) {
              isok = false;
            }
          });
          if (!isok) {
            this.$message.error(
              `只能选择开票公司为${this.companyNoName}的采购单！`
            );
            return;
          }
        }
        this.showModelThis = false;
        this.$emit("refresh", this.multipleSelection);
      }
    },
    async searchList() {
      this.loading = true;
      this.parmValue.code = this.id;
      const res = await asyncRequest.paymentcgdlist(this.parmValue);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        // console.log(this.tableData);
        this.count = Number(res.data.count);
        this.formatGoodList();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.count = 0;
      }
      this.loading = false;
      console.log(this.loading);
    },
    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.nakedPrice = v1.nakedPrice
          ? parseFloat(v1.nakedPrice + "").toFixed(2)
          : "-";
        v1.markPrice = v1.markPrice
          ? parseFloat(v1.markPrice + "").toFixed(2)
          : "-";
        v1.price = v1.price ? parseFloat(v1.price + "").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) : "-";

        return v1;
      });
    },
    setdata(e, index) {
      let model = JSON.parse(JSON.stringify(e));
      model.index = index;
      model.role_name += index;
      delete model["children"];
      return model;
    },
  },
};
</script>

   <style lang="scss" scoped>
.orderRecord {
  .setWidth {
    width: 100% !important;
  }
}
</style>
