<template>
  <div class="app-container">
    <el-card>
      <div>
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleCreate"
        >
          新增采购订单项
        </el-button>
      </div>
    </el-card>
    <el-card style="margin-top: 2px">
      <el-table
        v-loading="listLoading"
        :data="puorderTtemList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="700"
        height="100"
        v-el-height-adaptive-table="{ bottomOffset: 43 }"
      >
        <el-table-column type="index" width="50"/>
        <el-table-column label="物料名称">
          <template slot-scope="scope">{{ scope.row.material_.name }}</template>
        </el-table-column>
        <el-table-column label="物料型号">
          <template slot-scope="scope">{{ scope.row.material_.specification }}</template>
        </el-table-column>
        <el-table-column label="采购数量">
          <template slot-scope="scope">{{ scope.row.count }}</template>
        </el-table-column>
        <el-table-column label="已到货数量">
          <template slot-scope="scope">{{ scope.row.delivered_count }}</template>
        </el-table-column>
        <el-table-column label="截止到货时间">
          <template slot-scope="scope">{{ scope.row.delivery_date }}</template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="220px">
          <template slot-scope="scope">
            <el-link
              type="danger"
              @click="handleDelete(scope)"
            >
              删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="puorderTtemList.count > 0"
        :total="puorderTtemList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :visible.sync="dialogVisible" title="新增采购订单项">
      <el-form
        ref="Form"
        :model="puorderTtem"
        label-width="120px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="所需数量" prop="number">
          <el-input v-model="puorderTtem.count" placeholder="所需数量"/>
        </el-form-item>
        <el-form-item label="截止到货时间" prop="delivery_date">
          <el-date-picker
            v-model="puorderTtem.delivery_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width: 100%"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item class="material" label="采购物料：" :prop="material">
          <el-select v-model="puorderTtem.material" filterable size="small">
            <el-option
              v-for="item in materialoptions"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {getpVendorList} from "@/api/vendor";
  import {getMaterialList} from "@/api/mtm";

  import {
    getPuorderItemList,
    createPuorderItem,
    deletePuorderItem,
  } from "@/api/pum";
  import checkPermission from "@/utils/permission";

  import {genTree} from "@/utils";
  import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
  const defaultpuorderItem = {
    pu_order: null,
  };
  export default {
    components: {Pagination},
    data() {
      return {
        puorderTtem: defaultpuorderItem,
        puorderTtemList: {
          count: 0,
        },
        materialoptions: [],
        listQuery: {
          page: 1,
          page_size: 20,
        },
        dialogVisible: false,
        rule1: {
          delivery_date: [{required: true, message: "请选择日期", trigger: "blur"}],
          material: [
            {required: true, message: "请选择物料", trigger: "change"},
          ],
        },
      };
    },
    computed: {},
    watch: {},
    created() {
      this.id = this.$route.params.id;
      this.getList();
      this.getmaterialList();
    },
    methods: {
      checkPermission,
      //采购订单列表
      getList() {
        this.listQuery.pu_order = this.id;
        getPuorderItemList(this.listQuery).then((response) => {
          if (response.data) {
            this.puorderTtemList = response.data;
          }
        });
      },
      //物料
      getmaterialList() {
        getMaterialList({page: 0}).then((response) => {
          if (response.data) {
            this.materialoptions = response.data;
          }
        });
      },
      handleCreate() {
        this.puorderTtem = Object.assign({}, defaultpuorderItem);
        this.dialogVisible = true;
        this.$nextTick(() => {
          this.$refs["Form"].clearValidate();
        });
      },
      async confirm() {
        this.puorderTtem.pu_order = this.id
        createPuorderItem(this.puorderTtem).then((res) => {
          if (res.code >= 200) {
            this.getList();
            this.dialogVisible = false;
            this.$message.success("成功");
          }
        });
      },
      handleDelete(scope) {
        this.$confirm("确认删除?", "警告", {
          confirmButtonText: "确认",
          cancelButtonText: "取消",
          type: "error",
        })
          .then(async () => {
            await deletePuorderItem(scope.row.id);
            this.getList();
            this.$message.success("成功");
          })
          .catch((err) => {
            console.error(err);
          });
      },
    },
  };
</script>
