<template>
  <div class="app-container">
    <el-card>
      <el-row>
        <el-col style="width: 330px">
          <span style="margin-left: 10px">原材料名称:</span>
          <el-input v-model="RawMaterialName" placeholder="原材料名称" size="small" clearable/>
        </el-col>
        <el-col style="width: 200px">
          <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 10px" @click="pageCurrent=1;searchTableData()">查询</el-button>
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="handleAdd()">增加</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card style="margin-top: 15px">
      <el-table :data="tableData" style="width: 100%;min-height: 500px" height="500" size="mini" highlight-current-row border>
        <el-table-column align="center" label="原材料名称">
          <template slot-scope="scope">
            {{ scope.row.原材料名称 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center" width="200">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="pageCurrent"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>

    <el-dialog :title="title" :visible.sync="dialogFormVisible" center style="min-height: 300px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px" class="demo-ruleForm">
        <el-row>
          <el-form-item label="原材料名称" prop="RawMaterialName">
            <el-input v-model="form.RawMaterialName" placeholder="原材料名称" size="small" clearable/>
          </el-form-item>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel('form')">取消</el-button>
        <el-button v-show="title==='增加'" type="primary" @click="submitAdd('form')">确定</el-button>
        <el-button v-show="title==='编辑'" type="primary" @click="submitEdit('form')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { searchTableData, addData, editData, deleteCategory } from '@/api/ManufacturingCenter/RawMaterialsMaintenance'

export default {
  data() {
    return {
      RawMaterialNumber: '',
      RawMaterialName: '',
      param: 0,
      tableData: [],
      pageCurrent: 1,
      pageSize: 10,
      total: null,
      dialogFormVisible: false,
      title: '',
      form: {
        RawMaterialName: ''
      },
      rules: {
        RawMaterialName: [{ required: true, message: '请输入物料名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.searchTableData()
  },
  methods: {
    // 获取原材料
    searchTableData() {
      searchTableData(this.RawMaterialName, this.pageCurrent, this.pageSize).then(response => {
        this.tableData = response.data.rows
        this.total = parseInt(response.data.total)
      })
    },
    // 增加
    handleAdd() {
      this.addForm('form', [this.dialogFormVisible = true, this.title = '增加'])
    },
    // 提交增加
    submitAdd() {
      this.addTable(addData, [this.form.RawMaterialName], 'form', function() {
        this.dialogFormVisible = false
        this.pageCurrent = 1
        this.searchTableData()
      })
    },
    // 编辑
    handleEdit(row) {
      this.editForm(row, 'form', [this.title = '编辑', this.dialogFormVisible = true])
      this.form.RawMaterialName = row.原材料名称
      this.RawMaterialNumber = row.原材料流水号
    },
    // 提交编辑
    submitEdit() {
      this.editTable(editData, [this.RawMaterialNumber, this.form.RawMaterialName], 'form', function() {
        this.searchTableData()
        this.dialogFormVisible = false
      })
    },
    // 取消
    cancel() {
      this.dialogFormVisible = false
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该行, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteCategory(row.原材料流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            if (this.tableData && this.tableData.length === 1 && this.pageCurrent > 1) { // 判断删除的是否是最后一页最后一行，如果是且不是第一页，页数减一
              this.pageCurrent--
            }
            this.searchTableData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleSizeChange(val) {
      this.pageCurrent = 1
      this.pageSize = val
      this.searchTableData()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.searchTableData()
    }
  }
}
</script>

<style scoped>
  span {
    margin: 10px 0px 10px 10px;
  }
  .el-select{
    width:190px
  }
  .el-input{
    width:190px
  }
</style>
