<template>
  <div class="cost-team">
    <el-button type="primary" class="add" @click="append">增加收入部门</el-button>
    <el-card class="right-card">
      <el-table
      :data="costTeamData"
      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="收入部门编号"
          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 { getCostTeamList, addCostTeam, setCostTeam } from '@/api/costTeamApi'
export default {
  data () {
    return {
      costTeamData: [],
      dialogFormVisible: false,
      addFlag: false,
      id: '',
      form: {
        code: '',
        name: '',
        status: 'NORMAL',
        sort: ''
      },
      formLabelWidth: '120px',
      index: ''      
    }
  },
  created () {
    getCostTeamList().then(res => {
      this.costTeamData = 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);
      setCostTeam({
        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) {
            addCostTeam(this.form).then(res => {
              this.costTeamData.unshift(res.data)
              this.$refs[formName].resetFields();
            })
          } else {
            this.form.id = this.id;
            setCostTeam(this.form).then(res => {
              console.log(res);
              console.log(this.index)
              this.costTeamData.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>
  .cost-team {
    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>




