<template>
  <div class="product">
    <el-button type="primary" class="add" @click="append">增加产品</el-button>
    <el-card class="right-card">
      <el-table
      :data="productData"
      :default-sort = "{prop: 'code', order: 'ascending'}"
      style="width: 100%">
      <!--
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="产品名称">
                <span>{{ props.row.name }}</span>
              </el-form-item>
              <el-form-item label="产品 ID">
                <span>{{ props.row.id }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        -->
        <el-table-column
          label="产品编号"
          sortable
          prop="code">
        </el-table-column>
        <el-table-column
          label="产品名称"
          prop="name">
        </el-table-column>
        <el-table-column label="操作" width="80">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
              <!--
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除</el-button>
              -->
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="新增产品" :visible.sync="dialogFormVisible">     
      <el-form :model="form" ref="form">
        <el-form-item label="产品编码" prop="code" :label-width="formLabelWidth" :rules="[{ required: true, message: '编码不能为空'}]">
          <el-input v-model="form.code" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="产品名称" prop="name" :label-width="formLabelWidth" :rules="[{ required: true, message: '名称不能为空'}]">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <!--
        <el-form-item label="产品状态" prop="status" :label-width="formLabelWidth" :rules="[{ required: true, message: '状态不能为空'}]">
          <el-select v-model="form.status" placeholder="请选择状态值">
            <el-option label="正常" value="NORMAL"></el-option>
            <el-option label="已删除" value="DEL"></el-option>
          </el-select>
        </el-form-item>
        -->
        <el-form-item label="产品排序" prop="sort" :label-width="formLabelWidth">
          <el-input v-model.number="form.sort" autocomplete="off"></el-input>
        </el-form-item>             
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </div>
    </el-dialog>    
  </div>
</template>
<script scoped>
import { getProductList, addProduct, setProduct } from '@/api/productApi'
export default {
  data () {
    return {
      productData: [],
      dialogFormVisible: false,
      addFlag: false,
      id: '',
      form: {
        code: '',
        name: '',
        status: 'NORMAL',
        sort: ''
      },
      formLabelWidth: '120px',
      index: ''      
    }
  },
  created () {
    getProductList().then(res => {
      this.productData = res.data
    });
  },
  methods: {
    // 新增产品
    append() {
      this.addFlag = true;
      this.dialogFormVisible = true;
    },
    // 编辑产品
    handleEdit(index, row) {
      this.form.code = row.code;
      this.form.name = row.name;
      // this.form.status = row.status;
      this.form.sort = row.sort;
      this.id = row.id;
      this.index = index;
      this.dialogFormVisible = true;
      this.addFlag = false;
    },
    //删除产品
    handleDelete(index, row) {
      console.log(index, row);
      setProduct({
        id: row.id,
        status: 'DEL'
      }).then(res => {
        console.log(res);
      })
    },
    // 提交新增
    submitForm(formName) {
      //验证表单
      this.$refs[formName].validate((valid, value) => {
        if (valid) {
          console.log(this.form);
          if(this.addFlag) {
            addProduct(this.form).then(res => {
              this.productData.unshift(res.data)
              this.$refs[formName].resetFields();
            })
          } else {
            this.form.id = this.id;
            setProduct(this.form).then(res => {
              console.log(res);
              console.log(this.index)
              this.productData.splice(this.index, 1, res.data)
              this.$refs[formName].resetFields();
            })
          }
          this.dialogFormVisible = false;
        } else {
          return false;
        }
      });
    },
    // 取消
    resetForm (formName) {
      this.$refs[formName].resetFields();
      this.dialogFormVisible = false;
    }  
  }, 
}
</script>
<style scope>
  .product {
    padding: 20px;
    box-sizing: border-box;
  }
  .add {
    float: right;
    margin-bottom: 20px;
  }
  .right-card {
    width: 100%;
    margin: 0 auto;
  }
  .demo-table-expand {
    font-size: 0;
  }
  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }
  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }
</style>



