<template>
  <div class="salesOrderDetail pagePadding">
    <div
      style="width: 100%"
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <div class="tl" style="padding: 10px 0 50px 0">
        <span> {{ status }}----{{ powers }}</span>

        <el-button
          v-if="status !== '0'"
          style="margin: 0 0 0 10px"
          class="fr"
          type="primary"
          plain
          :size="'mini'"
          @click="openModal('add', false, 'sell')"
          >新建销售退货单
        </el-button>

        <!-- <el-button
          class="fr"
          type="primary"
          plain
          :size="'mini'"
          @click="openModal('add', false, 'service')"
          >新建售后申请单
        </el-button> -->

        <el-button
          type="primary"
          plain
          :size="'mini'"
          @click="statusConfirm('1', '发起审核流程')"
          v-if="status === '0' && powers.some((item) => item == '012')"
          >发起审核流程
        </el-button>

        <el-button
          @click="statusConfirm('0', '取消审核流程')"
          plain
          :size="'mini'"
          v-if="status === '1' && powers.some((item) => item == '014')"
          >取消审核流程</el-button
        >
        <el-button
          @click="statusConfirm('-1', '作废该条信息')"
          type="danger"
          plain
          :size="'mini'"
          v-if="
            status !== '' &&
            status !== '3' &&
            powers.some((item) => item == '015')
          "
          >作废该条信息</el-button
        >
      </div>
      <status-bar
        v-if="newTime !== ''"
        :newTime="newTime"
        :options="statusOptions"
        :status="status"
      />
      <el-collapse v-model="activeNames">
        <el-collapse-item title="销售订单信息" name="0">
          <add-form
            :newTime="newTime"
            v-if="newTime !== ''"
            :sitem="sitem"
            :id="queryId"
            @refresh="initData()"
          />
        </el-collapse-item>
        <el-collapse-item title="采购反馈物流" name="1">
          <feedback-form
            :newTime="newTime"
            v-if="newTime !== '' && status == 1"
            :sitem="sitem"
            :id="queryId"
            @refresh="initData()"
          />
        </el-collapse-item>
        <el-collapse-item title="准备发货" name="6" v-if="status === '2'">
          <el-button
            class="fr"
            style="margin: 0 0 10px 0"
            type="primary"
            plain
            :size="'mini'"
            @click="statusConfirm('3', '开始发货')"
            v-if="status === '2' && powers.some((item) => item == '026')"
            >开始发货
          </el-button>
        </el-collapse-item>
        <el-collapse-item title="销售出库单" name="3">
          <!-- && status == 3 -->
          <order-out-table
            :newTime="newTime"
            v-if="newTime !== '' && (status == 3 || status == 4)"
            :sitem="sitem"
            :id="queryId"
            @refresh="initData()"
          />
        </el-collapse-item>
        <el-collapse-item title="审批记录" name="10">
          <process-time-line
            v-if="newTime !== ''"
            :newTime="newTime"
            :type="'XSQRD'"
            :orderCode="orderCode"
          />
        </el-collapse-item>
      </el-collapse>
      <add-edit
        :id="modelId"
        :sitem="s_sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
      <!-- <edit-add
        :id="modelId"
        :sitem="sitem"
        :show-model="showModell"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      /> -->
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/salesOrder/detail";
import addForm from "./components/addForm";
import feedbackForm from "./components/feedback-form";
import orderOutTable from "./components/order-out-table";
import addEdit from "./addEdit.vue";
export default {
  name: "salesOrderDetail",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
    addForm,
    feedbackForm,
    orderOutTable,
    // editAdd,
  },
  computed: {
    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 [];
      }
    },
  },
  mounted() {
    console.log(this.$route);
  },
  data() {
    return {
      finishStatus: "finish",
      activeNames: ["-1", "0", "1", "2", "3", "4", "5", "6", "10"],
      statusOptions: [
        { value: "0", label: "发起流程" },
        { value: "1", label: "反馈物流" },
        { value: "2", label: "同意下单" },
        { value: "3", label: "填写发货信息" },
        { value: "4", label: "发货中" },
        { value: "5", label: "发货完成" },
      ],
      sitem: null,
      s_sitem: null,
      orderCode: "",
      status: "",
      newTime: "",
      loading: false,
      queryId: "",
    };
  },
  mounted() {
    this.status = "";
    this.queryId = this.$route.query.id;
    this.initData();
  },
  methods: {
    async statusConfirm(status, message) {
      await this.$confirm(`确定要${message}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          if (status === "-1") {
            await this.deleteById(message);
          } else {
            await this.setStatus(status, message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    getNewTime() {
      this.newTime = new Date().valueOf() + "";
      console.log(this.orderCode);
      console.log(this.newTime);
    },

    async deleteById(message) {
      const res = await asyncRequest.delete({ id: this.queryId });
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });

        this.routeGoto("check", {});
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },

    async setStatus(status, message, remark) {
      let model = {
        id: this.queryId,
        remark: remark || "",
        status: status,
      };
      const res = await asyncRequest.status(model);
      this.loading = false;
      if (res && res.code === 0) {
        this.$notify.success({
          title: message + "成功！",
          message: "",
        });
        this.initData();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async examForm(e) {
      if (!this.loading) {
        let type = e.state === "1" ? "2" : "0";
        await this.setStatus(type, "提交入库方审核", e.remark);
      }
    },
    async initData() {
      this.loading = true;
      const res = await asyncRequest.detail({ id: this.queryId });
      if (res && res.code === 0 && res.data) {
        this.sitem = res.data;
        this.status = this.sitem.status;
        this.orderCode = this.sitem.orderCode;
        this.getNewTime();
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
      this.loading = false;
    },
    openModal(id, isDetail, sitem) {
      if (sitem == "sell") {
        this.showModel = true;
      } else {
        this.showModell = true;
      }
      console.log(id, isDetail, sitem);

      this.modelId = id;
      this.isDetail = isDetail;
      // this.sitem = sitem;
      // this.sitem.info[0].edit = false;
      this.s_sitem = this.sitem;
    },
  },
};
</script>
   <style lang="scss" scoped>
.salesOrderDetail {
}
</style>
   
   