<template>
  <!-- 派费管理 -->
  <div>
    <actionBar menuTitle="派费管理" :operateList="operateList" :formList="formList" :ruleForm="searchRuleForm"
      @openModel="openModel" @searchProtocol="searchProtocol"></actionBar>
    <tables :key="Math.random()" :suspension="false" :tableList="tableList" :tableData="tableData"
      @buttonData="buttonData"></tables>
    <!-- 分页 -->
    <div class="paging_bottom" v-if="Total">
      <pagination :total="Total" :currentPage="Pagination.PageIndex" @changeSize="changeSize"
        @changeCurrent="changeCurrent">
      </pagination>
    </div>
    <el-dialog :title="staffTitle" :visible.sync="staffDialogVisible" width="600px" :close-on-click-modal="false"
      @close="closeDialog">
      <forms ref="childRules" :formNewList="formRuleList" :ruleForm="ruleForm" labelWidth="100px"></forms>
      <span slot="footer" class="dialog-footer">
        <el-button plain @click="staffDialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="confirmLoading" @click="handleAdd">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getGoods,
    getGoodsDetails,
    addGoods,
    putGoods,
    delGoods,
  } from '@/api/levy'
  import actionBar from '@/components/actionBar'
  import tables from '@/components/tables'
  import pagination from '@/components/pagination'
  import forms from '@/components/forms'
  import {
    formRules,
    employee
  } from "./levy.js";
  export default {
    components: {
      actionBar,
      tables,
      pagination,
      forms
    },
    data() {
      return {
        staffTitle: '添加',
        staffDialogVisible: false,
        Pagination: {
          PageIndex: 1,
          PageSize: 10,
        },
        operateList: [{
          type: 'add',
          title: '添加',
          icon: 'el-icon-plus',
        }],
        formList: [{
          type: 'input',
          label: '名称',
          field: 'name',
          placeholder: '名称',
        }],
        searchRuleForm: {
          name: '',
        },
        tableList: employee(),
        tableData: [],
        Total: 0,
        formRuleList: [],
        operationType: '',
        searchValue: {},
        confirmLoading: false,
        enterpriseId: null,
        ruleForm: {
          name: '',
          price: '',
          remark: '',
        },
      }
    },
    mounted() {
      const dataList = formRules();
      this.formRuleList = dataList;
      this.getList()
    },
    methods: {
      // 搜索
      searchProtocol(value) {
        this.searchValue = value
        this.getList()
      },
      openModel(type) {
        if (type == 'add') {
          this.staffTitle = '添加'
        }
        this.staffDialogVisible = true
      },
      // 获取商品列表
      getList() {
        var params = {
          page: this.Pagination.PageIndex,
          pageSize: this.Pagination.PageSize,
          ...this.searchValue
        }
        getGoods(params).then(res => {
          if (res.code == 200) {
            this.tableData = res.data.list
            this.Total = res.data.count
          }
        })
      },
      openModel(type) {
        this.operationType = type
        if (type == 'add') {
          this.staffTitle = '添加'
        }
        this.staffDialogVisible = true
      },
      handleAdd() {
        let flag = this.$refs['childRules'].validateForm();
        if (flag) {
          this.confirmLoading = true
          if (this.operationType == 'add') {
            var params = {
              ...this.ruleForm
            }
            params.price = Number(params.price)
            addGoods(params).then(res => {
              if (res.code == 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
                this.getList()
              }
              this.staffDialogVisible = false
              this.confirmLoading = false
            }).catch(() => {
              this.confirmLoading = false
            })
          } else if (this.operationType == 'edit') {
            this.editItem()
          }
        } else {
          this.$message.error('表单信息不完整，请继续填写完整');
        }
      },
      editItem() {
        var params = {
          id: this.enterpriseId,
          ...this.ruleForm
        }
        params.price = Number(params.price)
        putGoods(params).then(res => {
          if (res.code == 200) {
            this.$message({
              message: '操作成功',
              type: 'success'
            });
            this.getList()
          }
          this.staffDialogVisible = false
          this.confirmLoading = false
        }).catch(() => {
          this.confirmLoading = false
        })
      },
      // 删除
      deleteItem(id) {
        this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delGoods({
            id: id,
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success'
              });
              this.getList()
            }
          })
        }).catch(() => {});
      },
      buttonData(row, type) {
        this.enterpriseId = row.id
        this.operationType = type
        if (type == 'edit') {
          this.staffDialogVisible = true
          this.staffTitle = '编辑'
          this.$nextTick(() => {
            this.ruleForm.name = row.name
            this.ruleForm.price = row.price
            this.ruleForm.remark = row.remark
          })
        } else if (type == 'del') {
          this.deleteItem(row.id)
        }
      },
      changeSize(val) {
        this.Pagination.PageSize = val
        this.getList()
      },
      changeCurrent(val) {
        this.Pagination.PageIndex = val
        this.getList()
      },
      // 清空表单
      closeDialog() {
        this.$refs.childRules.resetCheck();
      }
    }
  }
</script>

<style lang="scss" scoped>
  .card_podia {
    display: flex;
  }

  .seating_form_card {
    width: 70%;
  }

  .seating_order {
    width: 30%;
  }

  .card_seating_order {
    margin: 0px 20px;
  }
</style>
