<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      v-model="form"
      v-model:page="page"
      ref="crud"
      @row-del="rowDel"
      @row-save="rowSave"
      @row-update="rowUpdate"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template #menu-left>
        <el-button type="danger" icon="el-icon-delete" @click="handleDelete"
          >删除
        </el-button>
      </template>
      <template #menu-right>
        <el-button type="primary" @click="handleImport">导入 </el-button>
      </template>
    </avue-crud>
    <!-- 导入 -->
    <basic-import v-if="isShowImport" title="导入" :isShow="isShowImport"
      templateUrl="/blade-desk/BA/LocallyPlatedPart/downloadExcelTemplate"
      templateName="局部镀零件模板.xls"
      importUrl="/blade-desk/BA/LocallyPlatedPart/importExcel"
     @closeDialog="closeDialog"></basic-import>
  </basic-container>
</template>
<script>
import basicImport from '@/components/basic-import/main.vue'
import {getList,addPlatedPart,updatePlatedPart,deletePlatedPart} from "@/api/basicData/localPlateParts"
export default {
  components: {
    basicImport,
  },
  data() {
    return {
      isShowImport:false,
      selectionList: [],
      loading:false,
      query:{},
      option: {
        height: "auto",
        calcHeight: 32,
        tip: false,
        // size: "medium",
        simplePage: true,
        searchShow: true,
        searchMenuSpan: 6,
        searchIcon: true,
        searchIndex: 3,
        tree: false,
        border: true,
        index: false,
        selection: true,
        viewBtn: false,
        delBtn: true,
        addBtn: true,
        editBtnText: "修改",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        viewBtnText: "详情",
        labelWidth: 120,
        menuWidth: 120,
        dialogWidth: 640,
        dialogClickModal: false,
        searchEnter: true,
        excelBtn: false,
        filterBtn: true,
        searchShowBtn: false,
        columnSort: true,
        excelBtn: true,
        columnSort: true,
        showOverflowTooltip: true,
        searchLabelPosition: "left",
        searchLabelPosition: "left",
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: "center",
        gridBtn: false,
        searchMenuPosition: "right",
        addBtnIcon: " ",
        viewBtnIcon: " ",
        delBtnIcon: " ",
        editBtnIcon: " ",
        align: "center",

        column: [
          {
            label: '零件号',
            prop: 'partCode',
            sortable: true,
            filter: true,
            span: 24,
            search: true,
            rules:[
              {
                required: true,
                message: "请输入零件编号",
                trigger: "blur",
              }
            ]
          },
        ],
      },
      form: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
    };
  },
  methods: {
    // 点击导入按钮
    handleImport() {
      this.isShowImport = true
    },
    refreshChange(){
      this.onLoad()
    },
    // 搜索
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    // 重置
    searchReset() {
      this.query = {};
      this.page.currentPage = 1;
      this.onLoad(this.page);
    },
    rowSave(row, done, loading){
      addPlatedPart(row).then(res =>{
        if(res.data.code == 200){
          this.$message.success('新增成功')
          this.onLoad()
          done()
        }
      })
    },
    rowUpdate(row, index, done, loading){
      updatePlatedPart(row).then(res =>{
        if(res.data.code == 200){
          this.$message.success('修改成功')
          this.onLoad()
          done()
        }
      })
    },
    rowDel(row){
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deletePlatedPart({ids:row.id}).then(res => {
          if(res.data.code == 200){
            this.$message.success('删除成功')
            this.onLoad()
          } 
        })
      });
    },
    closeDialog(val) {
      this.isShowImport = false
      if(val){
        this.onLoad()
      }
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.error("请至少选择一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        deletePlatedPart({
          ids:this.selectionList.map(item => item.id).join(','),
        }).then(res =>{
          if(res.data.code == 200){
            this.$message.success("删除成功")
            this.onLoad()
          }
        })
      });
    },
    // 多选
    selectionChange(list) {
      this.selectionList = list;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList({
        current:this.page.currentPage,
        size:this.page.pageSize,
        ...this.query
      }).then(res =>{
        this.data = res.data.data.records
        this.page.total = res.data.data.total
        this.loading = false
      })
    },
  },
};
</script>
<style lang="scss" scoped></style>
