<template>
  <el-container style="position: relative">
    <el-header>
      <div class="left-panel">
        <el-button type="primary" icon="el-icon-plus" @click="page_add"></el-button>
      </div>
      <div class="right-panel-search">

        <el-input
            v-model="search.scheduleNumber"
            placeholder="排产单号"
            clearable>
        </el-input>
        <el-input
            v-model="search.extrusionMachine"
            placeholder="挤压机台"
            clearable>
        </el-input>
        <el-input
            v-model="search.extrusionTeam"
            placeholder="挤压班组"
            clearable>
        </el-input>
        <el-date-picker
            v-model="search.extrusionDate"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="挤压排产日期"
            style="width: 600px"
        ></el-date-picker>

        <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
      </div>
    </el-header>
    <el-main class="nopadding">
      <scTable ref="table" :apiObj="apiObj" :column="column" :remoteFilter="config.remoteFilter" row-key="" @selection-change="selectionChange">

        <el-table-column type="selection" width="50"></el-table-column>

        <el-table-column label="操作" fixed="right" align="center" width="120"  v-if="config.option">
          <template #default="scope">
            <el-button type="text" size="medium" @click="table_edit(scope.row, scope.$index)" v-if="config.option.edit">物品信息</el-button>
            <el-button type="text" size="medium" @click="table_edit(scope.row, scope.$index)" v-if="config.option.edit">编辑</el-button>
            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)" v-if="config.option.del">
              <template #reference>
                <el-button type="text" size="medium">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </scTable>
    </el-main>
        <save-page
      ref="savePage"
      v-if="dialog.savePage"
      @success="handleSuccess"
      @closed="dialog.savePage = false"
    ></save-page>
  </el-container>

  <save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>

<script>
import saveDialog from './save'
import module from './module'
import config from "@/config"
import savePage from "./savePage";

export default {
  components: {
    saveDialog,
    savePage
  },
  data() {
    return {
      dialog: {
        save: false,
        product: false,
        savePage: false,
      },

      loading:false,
      file:"",
      search: {
        scheduleNumber: "",
        extrusionMachine:"",
        extrusionTeam:"",
        extrusionDate:""
      },

      apiObj:this.$API[module.name][module.list],
      selection: [],
      config: module.config,
      options: module.options,
      column: module.column
    }
  },
  mounted(){

  },
  methods: {
    //打开导入弹窗
    importExcel(){
      this.dialog.importExcel = true
      this.$nextTick(() => {
        this.$refs.importExcelDialog.open()
      })
    },
    //重置列表
    handleReset(){
      console.log('handleReset method called');
      // 1. 重置查询条件为初始状态
      this.search = {};
      var search =  {
        email: ""
      }; // 根据实际情况设置默认值，如果有特定默认值，请用默认值替换 {}

      // 2. 刷新表格数据
      if (this.$refs.table) {
        // 使用 upsearch 方法或者直接调用 upData 来刷新数据
        this.$refs.table.upData(search);
      }
    },
    //添加
    add(){
      // this.dialog.save = true
      this.dialog.product = true
      this.$nextTick(() => {
        this.$refs.productMaterialsDialog.open('add', this.list)
        // this.$refs.saveDialog.open('add', this.list)
      })
    },
    page_add() {
      this.dialog.savePage = true;
      this.$nextTick(() => {
        this.$refs.savePage.open();
      });
    },
    page_edit(row) {
      this.dialog.savePage = true
      this.$nextTick(() => {
        this.$refs.savePage.open('edit').setData(row);
      })
    },
    //编辑
    table_edit(row){
      this.dialog.product = true
      this.$nextTick(() => {
        this.$refs.productMaterialsDialog.open('edit', this.list).setData(row)
        // this.$refs.saveDialog.open('edit', this.list).setData(row)
      })
    },
    //删除
    async table_del(row, index){
      var reqData = {id: row[this.config.preId?this.config.preId:"id"]}
      var res = await this.$API[module.name][module.del].http(reqData);
      if(res.code == 0){
        //这里选择刷新整个表格 OR 插入/编辑现有表格数据
        this.$refs.table.upData();
        this.$message.success("删除成功");
      }else{
        this.$alert(res.message, "提示", {type: 'error'})
      }
    },
    //批量删除
    async batch_del(){
      this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
        type: 'warning'
      }).then(async () => {
        const loading = this.$loading();
        console.log(this.selection)
        var idArr = [];
        this.selection.forEach(item => {
          idArr.push(item[this.config.preId?this.config.preId:"id"])
        })

        var reqData = {ids: idArr.join(",")}
        var res = await this.$API[module.name][module.del].http(reqData);
        if(res.code == 0){
          //这里选择刷新整个表格 OR 插入/编辑现有表格数据
          this.$refs.table.refresh();
          this.$message.success("删除成功");
        }else{
          this.$alert(res.message, "提示", {type: 'error'})
        }
        loading.close();
      }).catch(() => {
        this.$message.success("操作失败")
      })
    },
    //表格选择后回调事件
    selectionChange(selection){
      this.selection = selection;
    },
    filterChange(obj) {
      this.search = obj;
      this.upsearch();
    },
    //搜索
    upsearch(){
      this.$refs.table.upData(this.search);
    },
    //本地更新数据
    handleSuccess(data, mode){
      this.$refs.table.upData()
    },
  }
}
</script>

<style>
.upload {
  color: #409EFF;
  background: #ecf5ff;
  min-height: 32px;
  padding: 9px 15px;
  font-size: 12px;
  border-radius: 3px;
  margin-left: 10px;
  margin-left: 10px;
}
.sc-upload-uploader {
  border: none !important;
}

</style>
