<template>
  <div class="page">
    <topBar
      v-if="topList.length > 0"
      :top-list="topList"
      :moren="moren"
    />
    <div class="thetopbox">
      <el-form label-width="auto">
        <el-row :gutter="10">
          <el-col :span="4">
            <el-form-item :label="'体系名称'" prop="vertexName">
              <el-input v-model="queryParams.vertexName" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="区域分组" prop="regionVertexPkId">
              <el-select v-model="queryParams.regionVertexPkId" clearable>
                <el-option v-for="item in groupList" :key="item.pkId" :label="item.name" :value="item.pkId" />
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="4">
            <el-form-item label="体系编号" prop="memberCode">
              <el-input clearable v-model="queryParams.memberCode"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="8">
            <el-form-item :label="'创建日期'" prop="">
              <el-date-picker
                v-model="cjsj"
                type="daterange"
                range-separator="——"
                :start-placeholder="'开始日期'"
                :end-placeholder="'结束日期'"
                value-format="yyyy-MM-dd"
                :unlink-panels="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <div class="searchbox">
              <el-button class="delBtn" @click="getvertexList()">
                {{ '搜索' }}</el-button>
              <el-button @click="reChongzhi"> {{ '重置' }}</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="main">
      <div class="maintop">
        <div class="mainbtn">
          <el-button
            v-hasButtons="['SystemconfigurationAdd']"
            size="small"
            class="thebtn1"
            @click="adddate()"
          >{{ '添加' }}</el-button>
          <el-button
            v-hasButtons="['SystemconfigurationExport']"
            size="small"
            class="thebtn2"
          >
            {{ '导出' }}</el-button>
          <el-button
            v-hasButtons="['SystemconfigurationUpAll']"
            size="small"
            class="tjBtn"
            @click="upData"
          >更新全部</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table
          v-loading="loading"
          height="740px"
          style="width: 100%"
          :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName"
          :data="tableData"
        >
          >
          <el-table-column type="selection" width="55" />

          <el-table-column
            align="center"
            prop="vertexName"
            :label="'体系名称'"
          />
          <el-table-column
            align="center"
            prop="memberCode"
            :label="'会员编号'"
          />
          <el-table-column
            align="center"
            prop="regionVertexName"
            label="区域分组"
          />
          <el-table-column
            align="center"
            prop="creationTime"
            :label="'创建日期'"
          />
          <el-table-column
            align="center"
            prop="sort"
            label="序号"
          />
          <el-table-column
            align="center"
            prop="time"
            :label="'操作'"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                v-hasButtons="['SystemconfigurationUpdate']"
                class="button-s"
                type="text"
                size="small"
                style="color: #6962f6"
                @click.native.prevent="update(scope.row)"
              >
                {{ '修改' }}
              </el-button>
              <el-button
                v-hasButtons="['SystemconfigurationDelete']"
                class="button-s"
                type="text"
                size="small"
                style="color: #c73030"
                @click.native.prevent="deleteRow(scope.row)"
              >
                {{ '删除' }}
              </el-button>
              <el-button
                v-hasButtons="['SystemconfigurationUpVertex']"
                class="button-s"
                type="text"
                size="small"
                style="color: #48b2fd"
                @click.native.prevent="updateTeam(scope.row)"
              >
                {{ '更新体系' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getvertexList"
    />
    <el-dialog
      :title="updateTitle"
      :visible.sync="updateShow"
      width="37%"
      center
      :before-close="handleClose"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="formd"
        label-width="100px"
        style="padding-bottom: 170px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'体系名称'" prop="vertexName">
              <el-input
                v-model="formd.vertexName"
                clearable
                :placeholder="'请输入体系名称'"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="'会员编号'" prop="memberCode">
              <el-input
                v-model="formd.memberCode"
                clearable
                :placeholder="'请输入会员编号'"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="'上级体系'">
              <el-select
                v-model="formd.pkParent"
                clearable
                :placeholder="'请选择'"
              >
                <el-option
                  v-for="item in vertexList"
                  :key="item.pkId"
                  :label="item.vertexName"
                  :value="item.pkId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区域分组">
              <el-select v-model="formd.regionVertexPkId" clearable>
                <el-option v-for="item in groupList" :key="item.pkId" :label="item.name" :value="item.pkId" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="序号">
              <el-input v-model="formd.sort" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button class="cancels" @click="handleClose">{{
          '取消'
        }}</el-button>
        <el-button class="submit" type="primary" @click="onSubmit('form')">{{
          '确认'
        }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from '@/components/topBar/checked.vue'
import {
  getvertexList,
  deleteVertex,
  getOnevertexList,
  updateVertex,
  saveVertex,
  findVertex,
  flushAll,
  getGroupConfigurationList
} from '@/api/systemconfiguration'
import { updateVertexs } from '@/api/settle'
import { getRouters } from '@/api/settle'
import { topList } from './mixins'
export default {
  name: 'Txpz',
  components: {
    topBar
  },
  data() {
    return {
      formd: {
        vertexName: '', // 体系名称
        // memberCode: "", //体系编号
        memberCode: '', // 体系编号
        regionVertexPkId: '' // 区域分组
      },
      moren: 'txpz',
      topList: topList,
      tableData: [],
      loading: false,
      multipleSelection: [],
      total: 0,
      cjsj: '',
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        vertexName: '', // 体系名称
        memberCode: '', // 体系编号
        createStartDate: '', // 创建时间起始
        createEndDate: '', // 创建时间结束
        regionVertexPkId: '' // 区域分组
      },
      updateShow: false,
      addOrEdit: '', // 添加还是修改 true添加 false修改
      updateTitle: '',

      rules: {
        vertexName: [
          { required: true, message: '请输入体系名称', trigger: 'blur' }
        ],
        memberCode: [
          { required: true, message: '请输入会员编号', trigger: 'blur' }
        ]
      },
      vertexList: [],
      groupList: []
    }
  },
  created() {
    this.getvertexList()
    this.getData()
    this.getGroupList()
    this.getUserRoute()
  },
  methods: {
    getUserRoute() {
      getRouters().then((res) => {
        console.log(res.data, 'res.data', this.topList)
        res.data.forEach((item) => {
          this.topList.forEach((items) => {
            if (item.routeName == items.url) {
              this.$set(items, 'changed', true)
            }
          })
        })
      })
    },
    getData() {
      findVertex().then((res) => {
        this.vertexList = res.data
      })
    },
    getGroupList() {
      getGroupConfigurationList({
        pageNum: 1,
        pageSize: 999
      }).then((res) => {
        this.groupList = res.rows
        console.log(this.vertexList, 'vertexList')
      })
    },
    upData() {
      this.$confirm('是否确认更新该体系?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
        flushAll().then((res) => {
          if (res.code == '200') {
            this.$message({
              message: res.msg,
              type: 'success'
            })
          }
          loading.close()
        })
      })
    },
    // 更新团队
    updateTeam(row) {
      this.$confirm('是否确认更新该体系?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateVertexs(row.pkId).then((res) => {
          if (res.code == '200') {
            this.$message({
              message: res.msg,
              type: 'success'
            })
          }
        })
      })
    },
    // 保存
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = ''
          if (this.addOrEdit == true) {
            url = saveVertex
          } else {
            url = updateVertex
          }
          url(this.formd).then((res) => {
            if (res.code == 200) {
              this.updateShow = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              this.getvertexList()
              this.formd = {
                vertexName: '', // 体系名称
                // memberCode: "", //体系编号
                memberCode: '' // 会员编号
              }
            }

            // else {
            //   this.$message({
            //     message: res.msg,
            //     type: "warning",
            //   });
            // }
          })
        }
      })
    },
    // 删除
    deleteRow(row) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteVertex(row.pkId).then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.getvertexList()
          } else {
            this.$message({
              message: res.msg,
              type: 'warning'
            })
          }
        })
      })
    },
    // 修改
    update(row) {
      console.log(
        '%c [ row ]-266',
        'font-size:13px; background:#a7ae35; color:#ebf279;',
        row
      )
      this.updateTitle = '修改'
      this.updateShow = true
      this.addOrEdit = false
      getOnevertexList({ pkId: row.pkId }).then((res) => {
        this.formd = res.data
      })
    },
    // 添加
    adddate() {
      this.updateShow = true
      this.updateTitle = '添加'
      this.addOrEdit = true
      this.formd = {
        vertexName: '', // 体系名称
        // memberCode: "", //体系编号
        memberCode: '', // 会员编号
        pkParent: ''
      }
    },
    // 获取体系配置列表
    getvertexList() {
      this.loading = true
      if (this.cjsj) {
        this.queryParams.createStartDate = this.cjsj[0]
        this.queryParams.createEndDate = this.cjsj[1]
      }
      getvertexList(this.queryParams).then((res) => {
        this.tableData = res.rows
        this.total = res.total
        this.loading = false
      })
    },
    // 重置
    reChongzhi() {
      this.cjsj = ''
      this.queryParams = {
        pageNum: 1,
        pageSize: 50,
        vertexName: '', // 体系名称
        memberCode: '', // 体系编号
        createStartDate: '', // 创建时间起始
        createEndDate: '' // 创建时间结束
      }
      this.getvertexList()
    },
    handleClose() {
      this.updateShow = false
      this.$refs['form'].clearValidate()
      this.$refs['form'].resetFields()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return 'warning-row'
      } else if (rowIndex % 2 == 0) {
        return 'success-row'
      }
      return ''
    }
  }
}
</script>

<style lang="scss" scoped>
.submit {
  width: 209px;
  height: 48px;
  background: #c8161d;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #c8161d;
  margin-left: 20px;
}
.cancels {
  width: 209px;
  height: 48px;
  background: #cccccc;
  border-radius: 5px;
  color: #fff;
  font-size: 18px;
  border-color: #ccc;
}
::v-deep .button-s {
  padding: 3px 0;
}
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}
::v-deep .el-table thead {
  color: #000000;
}
::v-deep .el-row {
  // margin-top: 20px;
}
.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;
  .thetopbox {
    padding: 0 20px 0px 20px !important;
    background: #ffffff;
    border-radius: 8px;

    .searchbox {
      display: flex;
      align-items: center;
      .searchtitle {
        margin-right: 10px;
      }
      .searchbtn {
        background: #08143f;
        color: #ffffff;
      }
    }
  }
  .main {
    //margin-top: 20px;
    background: #f9f9f9;
    //border-radius: 8px;
    //box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .maintop {
      display: flex;
      padding: 0px 0;
      justify-content: space-between;
      align-items: center;

      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
          width: 68px;
          height: 32px;
        }
      }
    }
    .maintable {
      // max-height: 550px;
      overflow: auto;
    }
  }
}
::v-deep .el-range-editor {
  width: auto;
}
::v-deep .el-select {
  width: 100%;
}
</style>
