<template>
  <div class="outOrderTable">
    <!-- {{wsend_num}}--{{powers.some((item) => item == '051')}} -->
    <!-- v-if="wsend_num && powers.some((item) => item == '051')" -->
    <el-table
      ref="addrForm"
      :data="tableData"
      border
      :size="'mini'"
      max-height="300px"
      style="width: 100%"
      row-key="key"
    >
      <el-table-column
        show-overflow-tooltip
        prop="outCode"
        label="发货申请单号"
        width="155"
      />

      <el-table-column
        show-overflow-tooltip
        prop="send_status"
        label="状态"
        width="105"
      >
        <template slot-scope="scope">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.send_status == '0' ? 'warning' : ''"
            v-text="
              (
                statusOptions.find(
                  (item) => item.id == scope.row.send_status
                ) || {}
              ).label || '--'
            "
          ></el-tag>
        </template>
      </el-table-column>

      <el-table-column
        show-overflow-tooltip
        prop="receipt_quantity"
        label="发货数量"
        width="80"
      />
      <el-table-column
        show-overflow-tooltip
        prop="contactor"
        label="联系人"
        width="80"
      />
      <el-table-column
        show-overflow-tooltip
        prop="mobile"
        label="联系电话"
        width="110"
      />
      <el-table-column
        show-overflow-tooltip
        prop="addr"
        label="收货地址"
        min-width="170"
      >
        <template slot-scope="scope">
          {{ scope.row.addr_info }}{{ scope.row.addr }}
        </template>
      </el-table-column>
      <el-table-column
        show-overflow-tooltip
        prop="post_name"
        label="物流公司"
        min-width="110"
      />
      <el-table-column
        show-overflow-tooltip
        prop="post_code"
        label="物流单号"
        width="160"
      />
      <el-table-column
        show-overflow-tooltip
        prop="sendtime"
        label="发货时间"
        width="145"
      />
      <el-table-column fixed="right" width="150">
        <template slot="header" slot-scope="scope">
          <span>操作</span>
          <el-tooltip
            style="margin: 3px 0 0 8px"
            effect="dark"
            class="fr"
            v-if="
              (status === '0' || status === '1') &&
              powers.some((item) => item == '048') &&
              sitem.send_type !== '1'
            "
            content="添加"
            placement="top"
          >
            <i
              class="el-icon-circle-plus-outline tb-icon"
              style="color: #63cbe7; magin: 0 0 0 10px"
              @click="openHouseModal()"
            ></i>
          </el-tooltip>
          <el-tooltip
            class="fr"
            style="margin: 3px 0 0 8px"
            effect="dark"
            v-if="
              (status === '0' || status === '1') &&
              powers.some((item) => item == '048') &&
              sitem.send_type !== '1'
            "
            content="导入收货地址"
            placement="top"
          >
            <i
              class="el-icon-upload2 tb-icon"
              style="color: #63cbe7"
              @click="addrmodel = true"
            ></i>
          </el-tooltip>
          <el-tooltip
            class="fr"
            style="margin: 3px 0 0 8px"
            effect="dark"
            v-if="
              (status === '0' || status === '1') &&
              powers.some((item) => item == '048') &&
              sitem.send_type !== '1'
            "
            content="下载收货地址模板"
            placement="top"
          >
            <a class="downloadAddr" href="./static/收货地址模板.xlsx">
              <i
                class="el-icon-download tb-icon"
                style="color: #63cbe7; margin: 0"
              ></i>
            </a>
          </el-tooltip>

          <el-tooltip
            style="margin: 3px 0 0 0"
            effect="dark"
            content="退货"
            class="fr"
            v-if="
              wsend_num &&
              wsend_num !== '0' &&
              powers.some((item) => item == '051')
            "
            placement="top"
          >
            <i
              class="el-icon-delete tb-icon"
              style="color: #63cbe7; magin: 0 0 0 10px"
              @click="openModal()"
            ></i>
          </el-tooltip>
        </template>

        <template slot-scope="scope">
          <el-tooltip
            effect="dark"
            content="查看"
            placement="top"
            v-if="powers.some((item) => item == '007')"
          >
            <i
              class="el-icon-view tb-icon"
              @click="
                routeGoto('sellOutOrderDetail', { id: scope.row.outCode })
              "
            ></i>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <out-order-add-model
      :showModel="modelShowModel"
      :id="modelId"
      :sitem="modelItem"
      @refresh="refresh"
      @cancel="closeModel"
    />
    <in-addr-model
      :show-model="addrmodel"
      @cancel="addrmodel = false"
      @refresh="addrRefresh"
    />

    <add-model
      :id="returnId"
      :sitem="returnItem"
      :show-model="returnModel"
      :is-detail="false"
      @refresh="refresh"
      @cancel="returnModel = false"
    />

    <!-- <add-edit
      :id="modelId"
      :sitem="s_sitem"
      :show-model="showModel"
      :is-detail="isDetail"
      @refresh="searchList"
      @cancel="showModel = false"
    /> -->
  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/salesOrder/detail";
import outOrderAddModel from "./out-order-add-model";
import addModel from "@/views/sellOut/sellReturn/components/addModel";
import inAddrModel from "@/components/in-addr-model";
import { mapGetters } from "vuex";

export default {
  name: "salesOrderDetail",
  mixins: [mixinPage, resToken],
  props: ["newTime", "id", "sitem"],
  components: { outOrderAddModel, addModel, inAddrModel },
  watch: {
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),

    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "salesOrderDetail"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },

  data() {
    return {
      orderCode: "",
      outCode: "",
      is_all: false,
      addrmodel: false,
      // 状态
      statusOptions: [
        { id: "0", label: "待公司采购" },
        { id: "1", label: "待库管发货" },
        { id: "2", label: "已发货待收货" },
        { id: "3", label: "已收货" },
        { id: "4", label: "已全部退货" },
      ],
      showGoodsModel: false,
      stock_code: "",
      tableData: [],
      loading: false,
      queryId: "",
      status: "",
      showModel: null,
      modelId: "",
      modelItem: null,
      s_sitem: null,
      returnId: "",
      returnItem: null,
      returnModel: false,
      wsend_num:"",
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    closeModel() {
      this.modelId = "";
      this.modelShowModel = false;
    },
    refresh() {
      this.$emit("refresh");
      this.modelId = "";
      this.modelShowModel = false;
    },
    async initForm() {
      this.status = "";
      this.queryId = this.$route.query.id;
      // this.rulesThis = this.rules;
      this.resetForm();
    },
    getNewTime() {
      this.newTime = new Date().valueOf();
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        this.wsend_num = true;
        const { addrs, status, wsend_num } = this.sitem;
        this.status = status;
        this.wsend_num=wsend_num||""
        // console.log(this.status);
        this.tableData = JSON.parse(JSON.stringify(addrs));
      
      });
    },
    async addrRefresh(e) {
      if (!this.loading) {
        this.loading = true;
        const { arrive_time, orderCode, wsend_num } = this.sitem;
        const { list } = e;
        console.log(list);
        let t_num = 0,
          add_num = 0;
        let model = {
          arrtime: arrive_time,
          orderCode: orderCode,
          addrlist: [],
        };
        list.forEach((a) => {
          add_num += a.receipt_quantity * 1;
          let item = {
            contactor: a.contactor,
            mobile: a.mobile,
            addr: a.addr,
            addr_code: a.addr_code,
            receipt_quantity: a.receipt_quantity,
          };
          model.addrlist.push(item);
        });
        if (wsend_num * 1 < t_num + add_num) {
          this.$message.warning("导入地址总数量不能大于未发货总数量！");
          this.loading = false;
          return;
        }
        let res = await asyncRequest.saleaddr(model);
        this.loading = false;
        if (res && res.code === 0) {
          this.$notify.success({
            title: "反馈物流费用成功！",
            message: "",
          });
          this.showModelThis = false;
          // 刷新
          this.$emit("refresh");
        } else if (res && res.code >= 100 && res.code <= 104) {
          await this.logout();
        } else {
          this.$message.warning(res.message);
        }
      }

      // this.tableData = [];
      // this.tableData.push(...list);
    },
    editRow(index) {
      let findex = this.stockForm.good_stock.findIndex((v) => v.edit === true);
      if (findex !== -1) {
        this.$message.warning("当前已有发货仓库信息在编辑，请保存后再试！");
        return;
      } else {
        this.stockForm.good_stock[index].edit = true;
      }
    },
    checkStockRow(index) {
      let total = parseInt(this.stockForm.good_stock[index].usable_stock),
        num = parseInt(this.stockForm.good_stock[index].num);
      if (total === 0) {
        this.$message.warning("该仓库已无该商品库存！不能销售！");
        return;
      } else {
        if (num > total) {
          this.$message.warning("销售数量不能大于可用库存！");
          return;
        } else {
          this.stockForm.good_stock[index].edit = false;
        }
      }
    },

    //省市区保存某一行
    checkRow(rowIndex) {
      this.$refs.addrForm.validate((valid) => {
        if (valid) {
          this.tableData[rowIndex].edit = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 省市区删除行操作
    deleteRow(index, rows) {
      rows.splice(index, 1);
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          const { order_addr } = JSON.parse(JSON.stringify(this.addrForm));
          let model = {
            id: this.queryId,
            order_addr: [],
          };
          order_addr.forEach((v2) => {
            let model2 = {
              post_fee: v2.post_fee,
              id: v2.id,
            };
            model.order_addr.push(model2);
          });
          let res = await asyncRequest.salefee(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "反馈物流费用成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh");
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    openHouseModal() {
      this.modelId = "add";
      this.modelItem = this.sitem;
      this.modelShowModel = true;
    },
    openModal() {
      this.returnId = "add";
      this.returnItem = this.sitem;
      this.returnModel = true;
      console.log(addModel);
    },
  },
};
</script>
   <style lang="scss" scoped>
</style>
   