<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="add">新增</el-button>
      </div>
      <div class="right-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>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" row-key="id" stripe :params="query">
        <el-table-column type="index" width="50" />
        <el-table-column label="供应商名称" prop="name" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="联系人" prop="contact">
        </el-table-column>
        <el-table-column label="联系方式" prop="contact_phone">
        </el-table-column>
        <el-table-column label="地址" prop="address" show-overflow-tooltip>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="120px">
          <template #default="scope">
            <el-button link type="primary" @click="table_edit(scope.row)" v-auth="'supplier.update'">
              编辑
            </el-button>
            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row)" v-auth="'supplier.delete'">
              <template #reference>
                <el-button link type="danger">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
  </el-container>
  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSaveSuccess"
    @closed="dialog.save = false"></save-dialog>
</template>
<script>
import saveDialog from "./supplier_form.vue";
export default {
  name: "rparty",
  components: {
    saveDialog,
  },
  data() {
    return {
      dialog: {
        save: false,
      },
      apiObj: this.$API.pum.supplier.list,
      query: {
        page: 1,
        page_size: 20,
        type: 10
      },
      selection: [],
      state_: {
        10: '完好',
        20: '限用',
        30: '在修',
        40: '禁用',
      },
    };
  },
  methods: {
    //添加
    add() {
      this.dialog.save = true;
      this.$nextTick(() => {
        this.$refs.saveDialog.open("add");
      });
    },
    //编辑
    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);
      });
    },
    //删除
    async table_del(row) {
      this.$API.pum.supplier.delete
        .req(row.id)
        .then((res) => {
          this.$message.success("删除成功");
          this.$refs.table.refresh();
          return res;
        })
        .catch((err) => {
          return err;
        });
    },

    //本地更新数据
    handleSaveSuccess(data, mode) {
      if (mode == "add") {
        this.$refs.table.refresh();
      } else if (mode == "edit") {
        this.$refs.table.refresh();
      }
    },
    handleQuery() {
      this.$refs.table.queryData(this.query)
    },
    resetQuery() {
      this.query = {};
    },
  },
};
</script>