<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-input v-model="query.search" placeholder="名称" clearable style="margin-right: 5px;"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"></el-button>
      </div>
      <div class="right-panel">
        <el-button type="primary" icon="el-icon-plus" @click="addLinkPlanItem"
          v-auth="'pu_orderitem.create'">计划明细变更采购明细</el-button>
        <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'pu_orderitem.create'"></el-button>
      </div>
    </el-header>
    <el-card>
      <el-form label-width="120px">
        <el-row>
          <el-col :md="12" :sm="24">
            <el-form-item label="订单编号：">{{ orderObj.number }}</el-form-item>
          </el-col>
          <el-col :md="12" :sm="24">
            <el-form-item label="订单状态：">{{ orderObj.state }}</el-form-item>
          </el-col>
          <el-col :md="12" :sm="24">
            <el-form-item label="所属部门">{{ orderObj.belong_dept }}</el-form-item>
          </el-col>
          <el-col :md="12" :sm="24">
            <el-form-item label="交货日期：">{{ orderObj.delivery_date }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="id" stripe :params="query" hidePagination>
        <el-table-column type="index" width="50" />
        <el-table-column label="物料" prop="material" show-overflow-tooltip>
          <template #default="scope">
            <span v-if="scope.row.material_"> {{ scope.row.material_.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="所需数量" prop="count">
        </el-table-column>
        <el-table-column label="已到货数量" prop="delivered_count">
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="更新时间" prop="update_time" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="left" width="100px">
          <template #default="scope">
            <el-link type="primary" @click="table_edit(scope.row)" v-auth="'pu_orderitem.update'">
              编辑
            </el-link>
            <el-divider direction="vertical"></el-divider>
            <el-link type="danger" @click="table_del(scope.row)" v-auth="'pu_orderitem.delete'">
              删除
            </el-link>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" :puOrder="puOrder" @success="handleSaveSuccess"
    @closed="dialog.save = false"></save-dialog>
  <plan-dialog v-if="dialog.plan" ref="planDialog" :puOrder="puOrder" @success="handlePlanSuccess"
    @closed="dialog.plan = false"></plan-dialog>
</template>
<script>
import saveDialog from "./orderitem_form.vue";
import planDialog from "./orderitemplan.vue";
export default {
  name: "rparty",
  components: {
    saveDialog, planDialog
  },
  data() {
    return {
      dialog: {
        save: false,
        splan: false,
      },
      apiObj: this.$API.pum.orderitem.list,
      query: {
        page: 1,
        page_size: 20
      },
      puOrder: '',
      orderObj: {},
      selection: [],
      state_: {
        10: '完好',
        20: '限用',
        30: '在修',
        40: '禁用',
      },
    };
  },
  mounted() {
    this.puOrder = this.$route.query.pu_order;
    this.getOrder();
  },
  methods: {
    getOrder() {
      this.$API.pum.order.item.req(this.puOrder).then((res) => {
        this.orderObj = res;
      })
    },
    //添加
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    addLinkPlanItem(row) {
      this.dialog.plan = true;
      this.$nextTick(() => {
        this.$refs.planDialog.open();
      });
      // this.$router.push({
      //       name: "orderitemplan",
      //       query:{
      //         pu_order:this.puOrder
      //       }
      //     });
    },

    //编辑
    table_edit(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("edit").setData(row);
      });
    },
    //查看
    table_show(row) {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("show").setData(row);
      });
    },
    //删除
    table_del(row) {
      this.$confirm(`确定删除吗？`, "提示", {
        type: "warning",
      }).then(() => {
        this.$API.pum.orderitem.delete.req(row.id).then((res) => {
          this.$message.success("删除成功");
          this.$refs.table.refresh();
          return res;
        }).catch((err) => {
          return err;
        });
      }).catch(() => { });
    },
    //本地更新数据
    handleSaveSuccess(data, mode) {
      if (mode == "add") {
        this.$refs.table.refresh();
      } else if (mode == "edit") {
        this.$refs.table.refresh();
      }
    },
    handlePlanSuccess() {
      this.dialog.plan = false;
      this.$refs.table.refresh();
    },
    handleQuery() {
      this.$refs.table.queryData(this.query)
    },
    resetQuery() {
      this.query = {};
    },
  },
};
</script>