<template>
  <div class="m-container">
    <div class="m-core">
      <div @click="chargeOperation('create')" v-permission="'paymentConfig/addTypeRoute'" class="newBand">添加</div>
      <!-- 列表 -->
      <div class="tableWrap">
        <el-table :data="tableList" :header-cell-style="{background:'#EDEEF0',color:'#444'}">
          <el-table-column align="center" prop="organName" label="分部"></el-table-column>
          <el-table-column align="center" label="收费方式">
            <template slot-scope="scope">
              <div v-if="scope.row.typeRouteScale && scope.row.typeRouteScale.length > 0">
                <p v-for="(item, index) in scope.row.typeRouteScale" :key="index">
                  {{ getOrganToStr(item.organId) }}--{{ getTypes(item.feeType) }} {{ item.scale }}%
                </p>
              </div>
            </template>
          </el-table-column>

          <el-table-column align="center" width="250px" label="操作">
            <template slot-scope="scope">
              <el-button
                @click="chargeOperation('update', scope.row)"
                v-permission="'paymentConfig/updateTypeRoute'"
                type="text"
              >修改</el-button>
              <el-button
                @click="chargeDel(scope.row)"
                v-permission="'paymentConfig/delTypeRoute'"
                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>
    </div>

    <el-dialog
      :title="formTitle[formActionTitle]"
      :visible.sync="chargeStatus"
      v-if="chargeStatus"
      width="700px"
    >
      <share-model :detail.sync="rowDetail"
              :branchList="branchList"
              :formActionTitle="formActionTitle"
              :close="onFormClose"
              :getList="getList" />
    </el-dialog>
  </div>
</template>
<script>
import pagination from "@/components/Pagination/index";
import {
  paymentConfigQueryPage
} from "@/api/systemManage";
import { delTypeRoute } from "./api";
import { branchQueryPage } from "@/api/specialSetting";
import ShareModel from './share-model'
export default {
  name: "chargeProfitManager",
  components: { pagination, ShareModel },
  data() {
    return {
      tableList: [],
      pageInfo: {
        // 分页规则
        limit: 10, // 限制显示条数
        page: 1, // 当前页
        total: 0, // 总条数
        page_size: [10, 20, 40, 50] // 选择限制显示条数
      },
      chargeStatus: false,
      branchList: [], // 分部列表
      formActionTitle: "create",
      formTitle: {
        create: "添加分润",
        update: "修改分润"
      },
      rowDetail: {}
    };
  },
  mounted() {
    this.getList();
    this.getRoleList()
  },
  methods: {
    getOrganToStr(id) {
      let name = null
      this.branchList.forEach(item => {
        if(item.value == id) {
          name = item.label
        }
      })
      return name
    },
    getTypes(type) {
      let template = {
        'SERVICE': '服务收入',
        'SELL': '销售收入'
      }
      return template[type]
    },
    getList() {
      let params = {
        hasTypeRoute: 1
      };
      params.rows = this.pageInfo.limit;
      params.page = this.pageInfo.page;
      paymentConfigQueryPage(params).then(res => {
        if (res.code == 200 && res.data) {
          res.data.rows.forEach(item => {
            if(item.typeRouteScale) {
              item.typeRouteScale = JSON.parse(item.typeRouteScale)
            }
          })
          this.tableList = res.data.rows;
          this.pageInfo.total = res.data.total;
        }
      });
    },
    async getRoleList(val) {
      await branchQueryPage({
        // 获取分部
        delFlag: 0,
        rows: 9999
      }).then(res => {
        if (res.code == 200 && res.data && res.data.rows) {
          res.data.rows.forEach(item => {
            this.branchList.push({
              label: item.name,
              value: item.id
            });
          });
        }
      });
    },
    chargeOperation(type, data) {
      this.formActionTitle = type;
      this.chargeStatus = true;
      // 修改的时候
      if (type == "update") {
        this.rowDetail = data
      }
    },
    chargeDel(row) {
      // 删除数据
      // paymentConfigDel
      this.$confirm("你确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delTypeRoute({ id: row.id }).then(res => {
            this.messageTips("删除", res);
          });
        })
        .catch();
    },
    messageTips(title, res) {
      if (res.code == 200) {
          this.$message.success(title + "成功");
          this.chargeStatus = false;
          this.getList();
      } else {
          this.$message.error(res.msg);
      }
    },
    onFormClose(formName) {
      this.chargeStatus = false
      // 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-select,
/deep/.el-date-editor.el-input {
  width: 100% !important;
}

.moreRule {
  background: #f0f0f0;
  position: relative;

  .el-form-item__error {
    color: #f56c6c;
    font-size: 12px;
    line-height: 1;
    position: absolute;
    left: 100px;
    top: 100%;
    margin-top: -21px;
  }
}
</style>
