<template>
  <div>
    <form data-vv-scope="form2" v-form>
      <el-row :gutter="20">
        <el-col :span="20">
          <MdView>
            <template>
              # 选择器用法
              当前实现的选择器是演示使用的 ，具体到项目中实现时需要实现对应的组件
              例如 维度选择器是MyDemension.vue组件 具体到X7项目实现时可以参考MyDemension.vue实现一个X7Demension.vue的组件
            </template>
          </MdView>数据预览
          <pre>{{data.xxbb}}</pre>
          <CodeBox name="维度选择器" alias="demensionSelector" :code="demo.demensionCode" desc="维度选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission1" permission="w" :options="permissions" />
                <br />
                <my-demension
                  v-model="data.xxbb.szwd"
                  :permission="permission1"
                  :config="{id:'data.xxbb.szwdId',code: 'data.xxbb.szwdCode'}"
                  single
                  placeholder="选择你所在的维度"
                />
              </div>
            </template>
          </CodeBox>
          <CodeBox name="组织选择器" alias="orgSelector" :code="demo.orgCode" desc="组织选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission2" permission="w" :options="permissions" />
                <br />
                <my-org-selector
                  v-model="data.xxbb.orgs"
                  :permission="permission2"
                  :config="{id:'data.xxbb.orgIds',code: 'data.xxbb.orgCodes', name: 'data.xxbb.orgNames',pathName: 'data.xxbb.orgPathNames'}"
                  placeholder="选择你所在的组织"
                />
              </div>
            </template>
          </CodeBox>

          <CodeBox name="角色选择器" alias="roleSelector" :code="demo.roleCode" desc="角色选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission3" permission="w" :options="permissions" />
                <br />
                <my-role-selector
                  v-model="data.xxbb.roles"
                  :permission="permission3"
                  :config="{id:'data.xxbb.roleId',code: 'data.xxbb.roleCode'}"
                  placeholder="请选择"
                />
              </div>
            </template>
          </CodeBox>

          <CodeBox name="用户选择器" alias="userSelector" :code="demo.userCode" desc="用户选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission4" permission="w" :options="permissions" />
                <el-button @click="$refs.myUserSelector.$refs.htUserSelector.clear()">清空</el-button>
                <el-button @click="showDialog">打开对话框</el-button>
                <br />
                <my-user-selector
                  ref="myUserSelector"
                  v-model="data.xxbb.users"
                  :permission="permission4"
                  :config="{id:'data.xxbb.userIds'}"
                  placeholder="请选择"
                />
              </div>
            </template>
          </CodeBox>
          <el-dialog title="用户选择" :visible="dialogVisible" :before-close="beforeClose" >
              <pre>{{data.defaultUsers}}</pre>
              <div v-for="(item,$index) in data.defaultUsers" :key="$index" v-show="dialogVisible" >
                <my-user-selector
                  ref="myUserSelector"
                  v-model="item.name"
                  :permission="permission4"
                  :config="{id:'data.defaultUsers['+$index+'].id'}"
                  placeholder="请选择"
                  append-to-body 
                />
              </div>

          </el-dialog>

          <CodeBox name="职务选择器" alias="jobSelector" :code="demo.jobCode" desc="职务选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission5" permission="w" :options="permissions" />
                <br />
                <my-job-selector
                  v-model="data.xxbb.jobs"
                  :permission="permission5"
                  :config="{id:'data.xxbb.jobIds',code: 'data.xxbb.jobCodes'}"
                  placeholder="请选择"
                />
              </div>
            </template>
          </CodeBox>

          <CodeBox name="岗位选择器" alias="postSelector" :code="demo.postCode" desc="岗位选择器">
            <template slot="demo">
              <div>
                权限设置：
                <ht-radio v-model="permission6" permission="w" :options="permissions" />
                <br />
                <my-post-selector
                  v-model="data.xxbb.posts"
                  :permission="permission6"
                  :config="{id:'data.xxbb.postIds',code: 'data.xxbb.postCodes'}"
                  placeholder="请选择"
                />
              </div>
            </template>
          </CodeBox>
        </el-col>
        <el-col :span="4">
          <AnchorWrapper :anchors="demo.anchors" />
        </el-col>
      </el-row>
    </form>
    <el-row>
      <el-col :span="20">
        <form data-vv-scope="form1" v-form>
          <table class="form-table" cellspacing="0" cellpadding="0" border="0">
            <tbody>
              <tr>
                <th class="group-th" colspan="4">
                  选择器 &ensp;&ensp;&ensp;
                  权限设置：
                  <ht-radio v-model="permission" permission="w" :options="permissions" />
                </th>
              </tr>
              <tr v-permit="4">
                <th>所在维度：</th>
                <td>
                  <my-demension
                    v-model="data.xxb.szwd"
                    :permission="permission"
                    :config="{id:'data.xxb.szwdId',code: 'data.xxb.szwdCode'}"
                    placeholder="选择你所在的维度"
                  />
                </td>
                <th>组织选择：</th>
                <td>
                  <my-org-selector
                    v-model="data.xxb.orgs"
                    :permission="permission"
                    :config="{id:'data.xxb.orgIds',code: 'data.xxb.orgCodes', name: 'data.xxb.orgNames',pathName: 'data.xxbb.orgPathNames'}"
                    placeholder="选择你所在的组织"
                  />
                </td>
              </tr>
              <tr v-permit="4">
                <th>角色选择：</th>
                <td>
                  <my-role-selector
                    v-model="data.xxb.roles"
                    :permission="permission"
                    :config="{id:'data.xxb.roleId',code: 'data.xxb.roleCode'}"
                    placeholder="请选择"
                  />
                </td>
                <th width="10%">用户选择：</th>
                <td>
                  <my-user-selector
                    v-model="data.xxb.users"
                    permission="r"
                    :config="{id:'data.xxb.userIds',fullname: 'data.xxb.userFullnames',account: 'data.xxb.userAccounts'}"
                    placeholder="请选择"
                  />
                </td>
              </tr>
              <tr v-permit="4">
                <th>职务选择：</th>
                <td>
                  <my-job-selector
                    v-model="data.xxb.jobs"
                    :permission="permission"
                    :config="{id:'data.xxb.jobIds',code: 'data.xxb.jobCodes'}"
                    placeholder="请选择"
                  />
                </td>
                <th width="10%">岗位选择：</th>
                <td>
                  <my-post-selector
                    v-model="data.xxb.posts"
                    :permission="permission"
                    :config="{id:'data.xxb.postIds',code: 'data.xxb.postCodes'}"
                    placeholder="请选择"
                  />
                </td>
              </tr>
            </tbody>
          </table>
        </form>
        <pre>{{data}}</pre>
        <el-button
          style="margin-top: 20px"
          :loading="loading"
          size="small"
          type="primary"
          @click="submit"
        >提交</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import CodeBox from "@/components/CodeBox.vue";
import AnchorWrapper from "@/components/AnchorWrapper.vue";
import MdView from "@/views/MdView.vue";
import utils from "@/utils.js";
import service from "@/service.js";
import { Message } from "element-ui";
import { setTimeout } from "timers";
import { Promise } from "q";
import MyDemension from "@/demo/MyDemension.vue";
import MyRoleSelector from "@/demo/MyRoleSelector.vue";
import MyUserSelector from "@/demo/MyUserSelector.vue";
import MyOrgSelector from "@/demo/MyOrgSelector.vue";
import MyJobSelector from "@/demo/MyJobSelector.vue";
import MyPostSelector from "@/demo/MyPostSelector.vue";

export default {
  name: "basic-form",
  components: {
    CodeBox,
    AnchorWrapper,
    MdView,
    MyDemension,
    MyRoleSelector,
    MyUserSelector,
    MyOrgSelector,
    MyJobSelector,
    MyPostSelector
  },
  data() {
    return {
      dialogVisible:false,
      loading: false,
      permission: "w",
      permission1: "w",
      permission2: "w",
      permission3: "w",
      permission4: "w",
      permission5: "w",
      permission6: "w",
      permissions: [
        { key: "w", value: "编辑" },
        { key: "b", value: "必填" },
        { key: "r", value: "只读" }
      ],
      demo: {
        anchors: [
          {
            content: "维度选择器",
            alias: "demensionSelector"
          },
          {
            content: "组织选择器",
            alias: "orgSelector"
          },
          {
            content: "角色选择器",
            alias: "roleSelector"
          },
          {
            content: "用户选择器",
            alias: "userSelector"
          },
          {
            content: "职务选择器",
            alias: "jobSelector"
          },
          {
            content: "岗位选择器",
            alias: "postSelector"
          }
        ],
        demensionCode: `
                    <my-demension
                            v-model="data.xxb.szwd"
                            :permission="permission"
                            :config="{id:'data.xxb.szwdId',code: 'data.xxb.szwdCode'}"
                            placeholder="选择你所在的维度"
                          />
        `,
        orgCode: `<my-org-selector
                v-model="data.xxb.orgs"
                :permission="permission"
                :config="{id:'data.xxb.orgIds',code: 'data.xxb.orgCodes', name: 'data.xxb.orgNames'}"
                placeholder="选择你所在的组织"/>`,
        roleCode: `<my-role-selector
                v-model="data.xxb.roles"
                :permission="permission"
                :config="{id:'data.xxb.roleId',code: 'data.xxb.roleCode'}"
                placeholder="请选择"/>`,
        userCode: `<my-user-selector
                v-model="data.xxb.users"
                 :permission="permission"
                :config="{id:'data.xxb.userIds',fullname: 'data.xxb.userFullnames',account: 'data.xxb.userAccounts'}"
                placeholder="请选择"/>`,
        jobCode: `<my-job-selector
                v-model="data.xxb.jobs"
                 :permission="permission"
                :config="{id:'data.xxb.jobIds',code: 'data.xxb.jobCodes'}"
                placeholder="请选择"/>`,
        postCode: `<my-post-selector
                v-model="data.xxb.posts"
                 :permission="permission"
                :config="{id:'data.xxb.postIds',code: 'data.xxb.postCodes'}"
                placeholder="请选择"/>`
      },
      data: {
        xxb: {
          xm: "张三",
          xb: "1",
          ah: "1,3",
          csrq: "1986-6-29",
          bysj: "2008-6-30",
          szwd: "行政维度,项目维度",
          szwdId: "1,2",
          szwdCode: "xzwd,xzwd",
          roles: "行政角色",
          roleId: "1",
          roleCode: "aa",
          fjzl:
            '[{"name":"更新日志.txt","size":12764,"percentage":100,"status":"success","state":"success"}]',
          sub_bxxx: [],
          users:
            "黎扬贵1,黎扬贵2,黎扬贵3,黎扬贵4,黎扬贵5,黎扬贵6,黎扬贵7,黎扬贵8,黎扬贵9,黎扬贵10,黎扬贵11,黎扬贵12,黎扬贵13,黎扬贵14,黎扬贵15",
          userIds: "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15",
         
          userFullnames:
            "黎扬贵1,黎扬贵2,黎扬贵3,黎扬贵4,黎扬贵5,黎扬贵6,黎扬贵7,黎扬贵8,黎扬贵9,黎扬贵10,黎扬贵11,黎扬贵12,黎扬贵13,黎扬贵14,黎扬贵15",
          userAccounts:
            "liyanggui1,liyanggui2,liyanggui3,liyanggui4,liyanggui5,liyanggui6,liyanggui7,liyanggui8,liyanggui9,liyanggui10,liyanggui11,liyanggui12,liyanggui13,liyanggui14,liyanggui15",
          orgs: "测试部门,财务部",
          orgNames: "测试部门,财务部",
          orgCodes: "csbm,cwb",
          orgIds: "876613,31784"
        },
        defaultUsers: [],
        xxbb: {
          szwd: "行政维度,项目维度",
          szwdId: "1,2",
          szwdCode: "xzwd,xzwd",

          roles: "行政角色",
          roleId: "1",
          roleCode: "aa",

          users: "黎扬贵1,黎扬贵2",
          userIds: "1,2",
          userFullnames: "黎扬贵1,黎扬贵2",
          userAccounts: "liyanggui1,liyanggui2",
          usersA: "黎扬贵1,黎扬贵2",
          userIdsArr:[{id:"1,2"}],

          orgs: "测试部门,财务部",
          orgNames: "测试部门,财务部",
          orgCodes: "csbm,cwb",
          orgIds: "876613,31784",
          orgPathNames:'/测试部门,/财务部'
        }
      },
      validate: {
        xm: null
      }
    };
  },
  methods: {
    showDialog(){
      this.dialogVisible=true;
      let user = {
          id:"1,2",
          name:"黎扬贵1,黎扬贵2",
          title:"用户选择",
          type: "user"
        }
      this.data.defaultUsers = [];
      this.data.defaultUsers.push(user);
    },
    beforeClose: function(){
      this.dialogVisible=false;
    },
    handleSelectOptions: function(query) {
      let _me = this;
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          _me.options.zy = _me.options.zy2;
          resolve();
        }, 1000);
      });
    },
    move: function(ary, item, direct) {
      this.data.xxb.sub_bxxx = utils.arrayMove(ary, item, direct);
    },
    submit: function() {
      this.loading = true;
      setTimeout(() => {
        utils
          .validateForm(this, "form1")
          .then(r => {
            Message.success(JSON.stringify(this.data));
          })
          .catch(reason => {
            Message.error(reason);
          })
          .finally(() => {
            this.loading = false;
          });
      }, 500);
    }
  }
};
</script>
