<template>
  <div class="app-container">
    <el-card>
      <span>材料名称:</span>
      <el-input v-model="Material" placeholder="请输材料名称" size="small" style="width:200px" clearable/>
      <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 15px" @click="pageCurrent = 1;pageSize = 10;featchMaterial()">查询</el-button>
      <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" @click="add()">增加</el-button>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="tableData"
        style="width: 100%"
        height="580"
        highlight-current-row
        border>
        <el-table-column align="center" label="材料名称">
          <template slot-scope="scope">
            {{ scope.row.材料 }}
          </template>
        </el-table-column>
        <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="Edit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="delMaterial(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]"
          :page-size="pageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" title="新增材料" center style="min-height: 200px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="110px">
        <el-form-item label="材料名称" prop="MaterialName" style="display: inline-block">
          <el-input v-model="form.MaterialName" clearable size="small" style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff()">取消</el-button>
        <el-button type="primary" size="small" @click="addMaterial('form')">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogFormVisible2" title="编辑材料" center style="min-height: 200px" width="400px">
      <el-form ref="form2" :model="form2" :rules="rules1" label-position="left" label-width="110px">
        <el-form-item label="材料名称" prop="MaterialName2" style="display: inline-block">
          <el-input v-model="form2.MaterialName2" size="small" clearable style="width:200px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="callOff()">取消</el-button>
        <el-button type="primary" size="small" @click="editMaterial('form2')">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { featchMaterial, addMaterial, delMaterial, editMaterial } from '@/api/BasicData/Materialmaintenance'
export default {
  data() {
    return {
      ID: '',
      dialogFormVisible: false,
      dialogFormVisible2: false,
      tableData: [],
      Material: '', // 材质名称
      listLoading: false,
      pageCurrent: 1,
      pageSize: 10,
      total: null,
      form: {
        MaterialName: ''
      },
      form2: {
        MaterialName2: ''
      },
      rules: {
        MaterialName: [{ required: true, message: '请输入材料名称', trigger: 'blur' }]
      },
      rules1: {
        MaterialName2: [{ required: true, message: '请输入材料名称', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.featchMaterial()
  },
  methods: {
    handleSizeChange(val) {
      this.pageSize = val
      this.pageCurrent = 1
      this.featchMaterial()
    },
    handleCurrentChange(val) {
      this.pageCurrent = val
      this.featchMaterial()
    },
    featchMaterial() {
      featchMaterial(this.Material, this.pageCurrent, this.pageSize).then(response => {
        this.tableData = response.data.rows
        this.total = response.data.total
      })
    },
    callOff() {
      this.dialogFormVisible = false
      this.dialogFormVisible2 = false
    },
    add() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.MaterialName = ''
      this.dialogFormVisible = true
    },
    addMaterial(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addMaterial(this.form.MaterialName).then(response => {
            if (response.data[0].result === '1') {
              this.$message.success('添加成功')
              this.dialogFormVisible = false
              this.form = {}
              this.featchMaterial()
            } else {
              this.$message.error('添加失败')
              this.form = {}
            }
          })
        }
      })
    },
    delMaterial(row) {
      this.$confirm('此操作将永久删除该行， 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delMaterial(row.材料流水号).then(response => {
          if (response.data[0].result === '1') {
            this.form.MaterialName = ''
            this.featchMaterial()
            this.$message.success('删除成功')
          } else {
            this.$message.error('删除失败')
          }
        })
      })
    },
    Edit(row) {
      if (this.$refs['form2'] !== undefined) {
        this.$refs['form2'].resetFields()
      }
      this.dialogFormVisible2 = true
      this.form2.MaterialName2 = row.材料
      this.ID = row.材料流水号
    },
    editMaterial(form2) {
      this.$refs[form2].validate((valid) => {
        if (valid) {
          this.dialogFormVisible2 = false
          editMaterial(this.ID, this.form2.MaterialName2).then(response => {
            if (response.data[0].result === '1') {
              this.featchMaterial()
              this.$message.success('编辑成功')
              this.form = {}
            } else {
              this.$message.error('编辑失败')
              this.form = {}
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
