<template>
  <div class>
    <a-breadcrumb class="newBreadcrumb mt0 mb16">
      <a-breadcrumb-item href>
        <router-link :to="{name:'merchantBack'}">
          <a-icon type="home" />
        </router-link>
      </a-breadcrumb-item>
      <a-breadcrumb-item>
        <span @click="cancelAdd" v-if="secondBreadcrumb" style="cursor:pointer">角色管理</span>
        <span v-else>角色管理</span>
      </a-breadcrumb-item>
      <a-breadcrumb-item v-if="secondBreadcrumb">{{secondBreadcrumb}}</a-breadcrumb-item>
    </a-breadcrumb>
    <!-- <div v-if="secondBreadcrumb">
      /
      {{secondBreadcrumb}}
    </div>-->
    <div class="contentbody bodyContainer">
      <div class="fsa pb16" v-if="secondBreadcrumb == ''">
        <div class="f20 fw500">{{secondBreadcrumb || '角色管理'}}</div>
        <a-button class="addmemeber f14" @click="addRole('add')" >
          <i class="iconfont icon-fangda mr4"></i> 添加角色
        </a-button>
      </div>
      <div v-if="secondBreadcrumb == ''">
        <a-table :columns="columns" :data-source="roleList" :pagination="false" @change="onChange" :loading="{spinning: isspinning,tip: '加载中',wrapperClassName: 'spinstyle'}" :rowKey="(record) => record.id" :locale="tablenodata">
          <!-- <a slot="index" slot-scope="record, text, index">{{ index + 1 }}</a> -->
          <div slot="setting" class="setting" slot-scope="text, record" style="color: #1890FF">
            <a @click="secondBreadcrumb = '角色详情'">详情</a>
            <a class="delete" @click="addRole('edit')">编辑</a>
            <a @click="delRole('del')">删除</a>
          </div>
        </a-table>
        <div class="pagecontainer mt16 fss">
          <a-pagination :current="pagination.pageNum" :page-size-options="pagination.pageSizeOptions" :default-current="1" :defaultPageSize="20" :show-total="total => `共${total}条`" :total="pagination.total" @change="onChange" size="small" show-size-changer show-quick-jumper />
        </div>
      </div>
      <div v-else-if="secondBreadcrumb == '添加角色' || secondBreadcrumb == '编辑角色'">
        <div>
          <a-form :form="roleForm" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }" @submit.event="addSubmit">
            <!-- 角色信息 -->
            <div class="fw500 mb16 f16">角色信息</div>
            <a-form-item class="addItem" label="角色名称:">
              <a-input placeholder="请输入角色名称" v-decorator="[ 'aliasName', { rules: [{ required: true, message: '请输入角色名称' }] }, ]" />
            </a-form-item>
            <a-form-item class="addItem" label="角色描述:">
              <a-input placeholder="请输入角色描述" v-decorator="[ 'desp', { rules: [{ required: true, message: '请输入角色描述' }] }, ]" />
            </a-form-item>
            <a-divider />
            <!-- 功能权限 -->
            <div class="fw500 mb16 f16">功能权限</div>
            <a-form-item>
              <div class>
                在
                <a-select default-value="lucy" class="mr8 ml8" style="width:360px" placeholder=" 请在已有的角色选择" v-decorator="['agencyTeamId',{ rules: [{ required: false, message: ' 请在已有的角色选择' }] },]" dropdownClassName="groupSelectDrop">
                  <a-select-option :value="item.roleName" v-for="(item,index) in roleList">{{item.roleName}}</a-select-option>
                  <!-- <div slot="dropdownRender" slot-scope="menu">
                  <div v-for="(item,index) in roleList" @click="choiceGroup(item)" class="groupSelectSub fsa" :key="index">
                    <div>{{item.roleName}}</div>
                  </div>
                  </div>-->
                </a-select>的基础上编辑权限
              </div>
            </a-form-item>
            <div v-for="(item, key) in authorityList">
              <a-checkbox :checked="item.checked" @change="checkboxChange(item,key)">{{item.label}}</a-checkbox>
              <br />
              <div class="ml24 mt16">
                <a-checkbox v-for="(subItem, subIndex) in item.children" :checked="subItem.checked" :disabled="!item.checked" @change="checkboxChange(subItem,key,subIndex)">{{subItem.label}}</a-checkbox>
              </div>
              <a-divider />
            </div>
            <a-form-item>
              <a-button type class="mr16" @click="cancelAdd">取消</a-button>
              <a-button type="primary" html-type="submit">保存</a-button>
            </a-form-item>
          </a-form>
        </div>
      </div>
      <div v-if="secondBreadcrumb == '角色详情' ">角色详情</div>
    </div>
  </div>
</template>

<script>
export default {
  // 组件名称
  name: 'RoleManagement',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {},
  // 组件状态值
  data() {
    return {
      tablenodata: {
        // 空状态文本内容
        emptyText: () => (
          <div>
            <div>
              <img src="https://user-images.githubusercontent.com/507615/54591670-ac0a0180-4a65-11e9-846c-e55ffce0fe7b.png" style="width:105px;height:90px;marginTop:20px" />
            </div>
            <div style="marginBottom:20px">暂无数据</div>
          </div>
        ),
      },
      secondBreadcrumb: '',
      roleForm: this.$form.createForm(this),
      roleList: [1, 2, 3, 4],
      pagination: {
        pageNum: 1,
        pageSize: 20,
        total: 3, //分页配置
        status: 0,
        keys: null,
        size: 'small',
        showTotal: function (total, range) {
          return `共${total}条`
        },
        pageSizeOptions: ['25', '50', '100'],
        showSizeChanger: true,
        // searchTag: 1,
      },
      isspinning: false,
      roleList: [
        {
          roleDesp: '',
          roleCreateTime: '2023-10-20 14:27:17',
          classify: 0,
          aliasName: '',
          userNum: 21,
          labelId: 672,
          pName: '稻田内测备注',
          roleId: 145,
          roleName: '稻田内测用户',
          sort: 1,
          roleType: 1,
          labelName: '稻田内测用户',
        },
        {
          roleDesp: '',
          roleCreateTime: '2023-10-12 09:34:33',
          classify: 0,
          aliasName: '',
          userNum: 2,
          labelId: 670,
          pName: '稻田内测备注',
          roleId: 144,
          roleName: '稻田内测用户1',
          sort: 2,
          roleType: 1,
          labelName: '稻田内测用户1',
        },
        {
          roleDesp: '',
          roleCreateTime: '2023-10-12 09:34:33',
          classify: 0,
          aliasName: '',
          userNum: 2,
          labelId: 670,
          pName: '稻田内测备注',
          roleId: 144,
          roleName: '稻田内测用户2',
          sort: 2,
          roleType: 1,
          labelName: '稻田内测用户2',
        },
      ],
      middleauthor: null,
      authorityList: {
        dtzw: {
          id: 1,
          label: '稻田职位',
          checked: false,
          children: [
            {
              id: 1,
              label: '查看职位列表',
              checked: false,
            },
            {
              id: 2,
              label: '职位详情页',
              checked: false,
            },
            {
              id: 3,
              label: '报名',
              checked: false,
            },
          ],
        },
        wdbm: {
          id: 2,
          label: '我的报名',
          checked: false,
          children: [
            {
              id: 1,
              label: '查看报名信息',
              checked: false,
            },
            {
              id: 2,
              label: '修改报名详情',
              checked: false,
            },
          ],
        },
        wdlx: {
          id: 3,
          label: '我的老乡',
          checked: false,
          children: [
            {
              id: 1,
              label: '查看老乡信息',
              checked: false,
            },
            {
              id: 2,
              label: '修改老乡详情',
              checked: false,
            },
          ],
        },
        tdsj: {
          id: 4,
          label: '团队数据',
          checked: false,
          children: [
            {
              id: 1,
              label: '查看数据',
              checked: false,
            },
          ],
        },
        xx: {
          id: 5,
          label: '消息',
          checked: false,
          children: [
            {
              id: 1,
              label: '系统消息',
              checked: false,
            },
            {
              id: 2,
              label: '团队消息',
              checked: false,
            },
          ],
        },
        sjht: {
          id: 6,
          label: '商家后台',
          checked: false,
          children: [
            {
              id: 1,
              label: '我的团队',
              checked: false,
            },
            {
              id: 2,
              label: '新增成员',
              checked: false,
            },
            {
              id: 3,
              label: '修改成员信息',
              checked: false,
            },
            {
              id: 4,
              label: '删除成员',
              checked: false,
            },
            {
              id: 5,
              label: '伯才TV',
              checked: false,
            },
            {
              id: 6,
              label: '数据大屏',
              checked: false,
            },
          ],
        },
        gzt: {
          id: 7,
          label: '工作台',
          checked: false,
          children: [],
        },
      },
    }
  },
  // 计算属性
  computed: {
    columns() {
      const columns = [
        {
          title: '排序',
          dataIndex: 'index',
          key: 'index',
          customRender: (text, row, index) => {
            if (row.children) {
              return ''
            } else {
              return index + 1
            }
          },
          // ellipsis: true,
          width: 60,
        },
        {
          title: '角色',
          dataIndex: 'roleName',
          key: 'roleName',
          scopedSlots: { customRender: 'roleName' },
          ellipsis: true,
          width: 150,
        },
        // {
        //   title: '角色',
        //   dataIndex: 'role',
        //   key: 'role',
        //   width: 100,
        //   ellipsis: true,
        // },
        {
          title: '角色备注',
          dataIndex: 'pName',
          key: 'pName',
          // width: 100,
          // filters: this.groupList,
          // customRender: (text, row, index) => {
          //   let text1
          //   this.groupList.forEach((item) => {
          //     if (item.id == text) {
          //       text1 = item.teamName
          //     }
          //   })
          //   return text1
          // },
          ellipsis: true,
        },
        // {
        //   title: '权限',
        //   dataIndex: 'permission',
        //   key: 'permission',
        //   ellipsis: true,
        // },
        {
          title: '操作',
          dataIndex: 'setting',
          key: 'setting',
          // sorter: (a, b) => a.userApplyOrder.localeCompare(b.userApplyOrder),
          scopedSlots: { customRender: 'setting' },
          ellipsis: true,

          // sortOrder: sortedInfo.columnKey == "userApplyOrder" && sortedInfo.order,
          // ellipsis: true,
          width: 140,
        },
      ]
      return columns
    },
  },
  // 侦听器
  watch: {},
  // 生命周期钩子   注：没用到的钩子请自行删除
  /**
   * 组件实例创建完成，属性已绑定，但DOM还未生成，$ el属性还不存在
   */
  beforeCreate() {},
  created() {
    this.$emit('setTitle', '角色管理')
    this.middleauthor = JSON.parse(JSON.stringify(this.authorityList))
    console.log(this.middleauthor)
  },
  /**
   * el 被新创建的 vm.el 替换，并挂载到实例上去之后调用该钩子。
   * 如果 root 实例挂载了一个文档内元素，当 mounted 被调用时 vm.el 也在文档内。
   */
  mounted() {},
  // 组件方法
  methods: {
    checkboxChange(e, key, index) {
      // console.log(e)
      // console.log(key)
      // console.log(index)
      // console.log(this.authorityList[key]);
      // console.log(this.authorityList[key][index]);
      if (e.children) {
        this.authorityList[key].checked = !this.authorityList[key].checked
      } else {
        this.authorityList[key].children[index].checked = !this.authorityList[key].children[index].checked
      }
      console.log(this.authorityList)
    },
    inputpageChange(e) {
      const pagenumcount = this.pagination.total / this.pagination.pageSize
      console.log(pagenumcount)
      if (e.target.value) {
        if (e.target.value > pagenumcount) {
          this.pagination.pageNum = Math.ceil(pagenumcount)
        } else {
          this.pagination.pageNum = e.target.value
        }
      } else {
        this.pagination.pageNum = 1
      }
      // this.getJobList();
      this.getMemberList()
      console.log(e)
    },
    choiceGroup() {},
    onChange(e, e1, e2) {
      console.log(e)
      console.log(e1)
      console.log(e2)
      if (typeof e == 'number') {
        this.pagination.pageNum = e
        this.getMemberList()
      }
    },
    addRole(type) {
      if (type == 'add') {
        this.secondBreadcrumb = '添加角色'
      } else {
        this.secondBreadcrumb = '编辑角色'
      }
      this.$forceUpdate()
    },
    delRole() {
      this.$confirm({
        title: '删除',
        content: '确定要删除该角色吗？',
        centered: true,
        onOk(e1, e2) {
          console.log(e1)
          console.log(e2)
          return new Promise((res) => {
            setTimeout(() => {
              res()
            }, 2000)
          })
        },
        onCancel() {},
      })
    },
    /**
     * 角色信息表单提交
     */
    addSubmit() {
      this.roleForm.validateFields(async (err) => {
        console.log(err)
      })
    },
    /**
     * 取消编辑或者新增角色
     */
    cancelAdd() {
      this.secondBreadcrumb = ''

      this.authorityList = JSON.parse(JSON.stringify(this.middleauthor))
    },
  },
}
</script>

<style scoped lang="less">
.addmemeber {
  border: 0;
  padding: 0 12px;
  height: 32px;
  line-height: 32px;
  color: #fff;
  background: #1890ff;
  border-radius: 4px;
  i {
    font-size: 12px;
  }
  &:hover {
    background: #40a9ff;
  }
}
.addItem {
  width: 468px;
}
.ant-divider {
  margin: 16px 0 22px 0;
}
.delete {
  border-left: 1px solid #979797;
  border-right: 1px solid #979797;
  padding: 0 9px;
  margin: 0 9px;
}
/deep/.ant-table {
  .ant-table-content {
    .ant-table-body {
      .ant-table-thead {
        th {
          padding: 12px 0;
          div {
            padding: 0px 8px;
          }
          &:not(:first-child) {
            > span {
              //  line-height: 22px;
              > div {
                //  line-height: 1;
                border-left: 1px solid #ddd;
                padding-left: 12px;
              }
            }
          }
        }
        .ant-table-row-cell-break-word {
          font-size: 14px !important;
          font-weight: bold;
        }
        .ant-table-row-cell-ellipsis {
          font-size: 14px !important;
          font-weight: bold;
        }
      }
      .ant-table-tbody {
        .ant-table-row-cell-ellipsis {
          font-size: 14px !important;
          padding: 12px 14px;
        }
        .ant-table-row-expand-icon {
          // color: #000;
          padding: 0;
        }
        .ant-table-row-indent {
          display: none !important;
        }
      }
    }
  }
}
</style>
