<template>
  <div class="app-container">
    <el-card>
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
          >新增采购订单</el-button
        >

        <el-input
          v-model="listQuery.search"
          placeholder="采购订单编号、供应商名称"
          style="width: 300px"
          class="filter-item"
          @keyup.enter.native="handleFilter"
        />
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-search"
          @click="handleFilter"
          >搜索</el-button
        >
        <el-button
          class="filter-item"
          type="primary"
          icon="el-icon-refresh-left"
          @click="resetFilter"
          >重置</el-button
        >
      </div>
    </el-card>
    <el-card style="margin-top: 2px">
      <el-table
        v-loading="listLoading"
        :data="puorderList.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.number }}</template>
        </el-table-column>
        <el-table-column label="供应商">
          <template slot-scope="scope" v-if="scope.row.vendor_">{{
            scope.row.vendor_.name
          }}</template>
        </el-table-column>
        <el-table-column label="审核情况" width="150">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_audited == false">未审核</el-tag>
            <el-tag v-else-if="scope.row.is_audited == true">已审核</el-tag>
          </template>
        </el-table-column>
  <el-table-column label="创建时间">
          <template slot-scope="scope">{{ scope.row.create_time }}</template>
        </el-table-column>
        <el-table-column align="center" label="操作" width="220px">
          <template slot-scope="scope">
            <el-link
              v-if="checkPermission(['vendor_update'])"
                type="primary"
              @click="handlePuOrderItem(scope)"
              >订单项</el-link
            >
            <el-link
              v-if="
                checkPermission(['vendor_update']) &&
                scope.row.is_audited == false
              "
                type="primary"
              @click="handleAudit(scope)"
              >审核</el-link
            >
            <el-link
              v-if="checkPermission(['vendor_update'])"
                type="primary"
              @click="handleEdit(scope)"
              >编辑</el-link
            >
            <el-link
              v-if="checkPermission(['vendor_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="puorderList.count > 0"
        :total="puorderList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑采购订单' : '新增采购订单'"
    >
      <el-form
        ref="Form"
        :model="puorder"
        label-width="120px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="订单编号" prop="number">
          <el-input v-model="puorder.number" placeholder="订单编号" />
        </el-form-item>

        <el-form-item label="供应商" prop="vendor">
          <el-select
            style="width: 100%"
            v-model="puorder.vendor"
            placeholder="请选择"
          >
            <el-option
              v-for="item in vendorList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </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('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getpVendorList } from "@/api/vendor";
import { getUserList } from "@/api/user";
import {
  getPuorderList,
  createPuorder,
  updatePuorder,
  deletePuorder,
  createPuorderAudit,
  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 defaultpuorder = {
  number: "",
};
export default {
  components: { Pagination },
  data() {
    return {
      puorder: defaultpuorder,
      puorderList: {
        count: 0,
      },

      listQuery: {
        page: 1,
        page_size: 20,
      },
      vendorList: [],
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        number: [{ required: true, message: "请输入", trigger: "blur" }],
        vendor: [
          { required: true, message: "请选择供应商", trigger: "change" },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
    this.getVendorList();
  },
  methods: {
    checkPermission,
    //采购订单
    getList() {
      this.listLoading = true;
      getPuorderList(this.listQuery).then((response) => {
        if (response.data) {
          this.puorderList = response.data;
        }
        this.listLoading = false;
      });
    },
    //供应商列表
    getVendorList() {
      getpVendorList({ page: 0 }).then((response) => {
        if (response.data) {
          this.vendorList = response.data;
        }
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
      };
      this.getList();
    },
    handleCreate() {
      this.puorder = Object.assign({}, defaultpuorder);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },

    handleEdit(scope) {
      this.puorder = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deletePuorder(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },

    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updatePuorder(this.puorder.id, this.puorder).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          } else {
            createPuorder(this.puorder).then((res) => {
              if (res.code >= 200) {
                this.getList();
                this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    handlePuOrderItem(scope) {
      this.$router.push({ name: "puorderitem", params: { id: scope.row.id } });
    },
    handleAudit(scope) {

      createPuorderAudit(scope.row.id).then((res) => {
        if (res.code >= 200) {
          this.getList();
         
          this.$message.success("审核成功!");
        }
      });
    },
  },
};
</script>
