<template>
  <div class='m-container'>
    <!-- <h2>收费类型设置</h2> -->
    <div class="m-core">
      <div @click="onChargeOperation('create')"
           class='newBand'>添加</div>
      <div class="tableWrap">
        <el-table :data="dataList"
                  :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column prop="organName"
                           label="所属分部">
          </el-table-column>
          <el-table-column prop="chargeTypeName"
                           label="收费类型">
          </el-table-column>
          <el-table-column prop="courseFee"
                           label="课程费用">
          </el-table-column>
          <el-table-column align='center'
                           label="操作">
            <template slot-scope="scope">
              <el-button @click="onChargeOperation('update', scope.row)"
                         type="text">修改</el-button>
              <el-button @click="onChargeDelete(scope.row)"
                         type="text">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <pagination :total="pageInfo.total"
                    :page.sync="pageInfo.page"
                    :limit.sync="pageInfo.limit"
                    :page-sizes="pageInfo.page_size"
                    @pagination="getList" />
      </div>

      <el-dialog :title="formTitle[formActionTitle]"
                 :visible.sync="chargeStatus"
                 @close="onFormClose('ruleForm')"
                 width="500px">
        <el-form :model="form"
                 :rules="rules"
                 ref="ruleForm">
          <el-form-item label="所属分部"
                        prop="organId"
                        :label-width="formLabelWidth">
            <el-select v-model.trim="form.organId"
                       clearable
                       filterable>
              <el-option v-for="(item, index) in branchList"
                         :key="index"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="收费类型"
                        prop="chargeTypeId"
                        :label-width="formLabelWidth">
            <el-select v-model.trim="form.chargeTypeId"
                       clearable
                       filterable>
              <el-option v-for="(item, index) in typesList"
                         :key="index"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="课程费用"
                        prop="courseFee"
                        :label-width="formLabelWidth">
            <el-input type="number"
                      @mousewheel.native.prevent
                      v-model.trim="form.courseFee"
                      autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="chargeStatus = false">取 消</el-button>
          <el-button @click="onTypesSubmit('ruleForm')"
                     type="primary">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import pagination from '@/components/Pagination/index'
import { chargeTypeList, chargeTypeOrganizationFeeAdd, chargeTypeOrganizationFeeDelete, chargeTypeOrganizationFeeUpdate, chargeTypeOrganizationFee, branchQueryPage } from '@/api/specialSetting'
export default {
  components: { pagination },
  name: 'chargesList',
  data () {
    return {
      id: null,
      dataList: [],
      typesList: [], // 分部列表
      branchList: [], // 分部列表
      formActionTitle: 'create',
      formTitle: {
        create: '添加乐团收费标准',
        update: '修改乐团收费标准'
      },
      chargeStatus: false,
      formLabelWidth: '100px',
      form: {
        chargeTypeId: null,
        courseFee: null
      },
      rules: {
        courseFee: [{ required: true, message: '请输入课程费用', trigger: 'blur' }],
        chargeTypeId: [{ required: true, message: '请选择收费类型', trigger: 'change' }],
        organId: [{ required: true, message: '请选择所属分部', trigger: 'change' }]
      },
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      }
    }
  },
  mounted () {
    this.getList()

    // 收费类型
    chargeTypeList({
      rows: 9999,
      page: 1
    }).then(res => {
      if (res.code == 200) {
        res.data.rows.forEach(item => {
          this.typesList.push({
            label: item.name,
            value: item.id
          })
        })
      }
    })

    // 分部列表
    branchQueryPage({
      rows: 9999,
      page: 1
    }).then(res => {
      if (res.code == 200) {
        res.data.rows.forEach(item => {
          this.branchList.push({
            label: item.name,
            value: item.id
          })
        })
      }
    })
  },
  methods: {
    onTypesSubmit (formName) { // 添加数据
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.formActionTitle == 'create') {
            if (this.form.id) { // 判断有没有Id，如果有则删除
              delete this.form.id
            }
            chargeTypeOrganizationFeeAdd(this.form).then(res => {
              this.messageTips('添加', res)
            })
          } else if (this.formActionTitle == 'update') {
            chargeTypeOrganizationFeeUpdate(this.form).then(res => {
              this.messageTips('修改', res)
            })
          }
        } else {
          return false;
        }
      })
    },
    messageTips (title, res) {
      if (res.code == 200) {
        this.$message.success(title + '成功')
        this.chargeStatus = false
        this.getList()
      } else {
        this.$message.error(res.msg)
      }
    },
    onChargeOperation (type, row) {
      this.chargeStatus = true
      this.formActionTitle = type

      if (type == 'update') {
        this.form = {
          chargeTypeId: row.chargeTypeId,
          courseFee: row.courseFee,
          organId: row.organId,
          id: row.id
        }
      }
    },
    onChargeDelete (row) { // 删除
      this.$confirm('您确定删除该收费标准?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        chargeTypeOrganizationFeeDelete({ id: row.id }).then(res => {
          this.messageTips('删除', res)
        })
      }).catch(() => { })

    },
    getList () {
      chargeTypeOrganizationFee({
        rows: this.pageInfo.limit,
        page: this.pageInfo.page
      }).then(res => {
        if (res.code == 200) {
          this.dataList = res.data.rows
          this.pageInfo.total = res.data.total
        }
      })
    },
    onFormClose (formName) { // 关闭弹窗重置验证
      this.form = {
        courseFee: null,
        chargeTypeId: null
      }
      this.$refs[formName].resetFields()
    },
  }
}
</script>
<style lang="scss" scoped>
/deep/.el-button--primary {
  background: #14928a;
  border-color: #14928a;
  color: #fff;
  &:hover,
  &:active,
  &:focus {
    background: #14928a;
    border-color: #14928a;
    color: #fff;
  }
}
/deep/.el-date-editor.el-input {
  width: 100% !important;
}
/deep/.el-select {
  width: 98% !important;
}

/deep/.el-table {
  display: inline-block;
}
</style>