<template>
  <div class="app-container">
    <div class="app-container">
      <div class="filter-container">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate"> 新增 </el-button>
      </div>
      <el-table
        v-loading="listLoading"
        :element-loading-text="$t('common.tableMsg')"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable"
        row-key="id"
        default-expand-all
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column
           :label="$t('role.jsmc')"
          header-align="center"
          min-width="150px"
          align="center"
        >
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column :label="$t('formulationEvaluation.sort')" min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.sort }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('dailyExecutionPlan.qy')"  min-width="80px" header-align="center" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
              @change="handleEnableChange(scope.$index, scope.row)"
            />
          </template>
        </el-table-column>
        <el-table-column  :label="$t('errorAnalysis.cz')" header-align="center" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button type="warning" size="mini" @click="handleMenu(row)">菜单</el-button>
            <el-button type="warning" size="mini" style="width:70px" @click="handleDataRole(row)">数据权限</el-button>
            <el-button type="primary" size="mini" @click="handleUpdate(row)">编辑</el-button>
            <el-button v-if="row.status!='已删'" size="mini" type="danger" @click="form_delete(row)">{{$t('formulationEvaluation.del')}}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>=0" :total="total" :page.sync="getdataListParm.offset" :limit.sync="getdataListParm.pagecount" @pagination="getList" />

      <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
        <el-form ref="dataForm" :rules="rules" :model="dataform" label-position="left" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item  :label="$t('role.jsmc')" prop="name">
            <el-input
              ref="name"
              v-model="dataform.name"
            />
          </el-form-item>
          <el-form-item :label="$t('formulationEvaluation.sort')" prop="sort">
            <el-input
              ref="sort"
              v-model="dataform.sort"
            />
          </el-form-item>
          <el-form-item :label="$t('dailyExecutionPlan.qy')"  prop="enable">
            <el-switch
              ref="enable"
              v-model="dataform.enable"
              active-color="#13ce66"
              inactive-color="#ff4949"
              :active-value="1"
              :inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="dialogStatus==='create'" ref="createb" class="save" @click="createData_again()"> 确认新增 </el-button>
          <el-button class="save" @click="dialogStatus==='create'?createData():updateData()"> 确认 </el-button>
          <el-button class="minCancel" @click="dialogFormVisible = false"> 关闭 </el-button>
        </div>
      </el-dialog>

      <el-dialog :title="textMap['dataRole']" :visible.sync="dialogDataRoleVisible" :close-on-click-modal="false">
        <el-form ref="dataRoleTemp" :rules="rules" label-position="left" :model="dataRoleTemp" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item label="数据权限" prop="datarole">
            <el-radio-group v-model="dataRoleTemp.datarole">
              <el-radio :label="3">全部数据</el-radio>
              <el-radio :label="2">牧场数据</el-radio>
              <el-radio :label="1">部门数据</el-radio>
              <el-radio :label="0">个人数据</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="save" @click="updateDataRole()"> 确认 </el-button>
          <el-button class="minCancel" @click="dialogDataRoleVisible = false"> 关闭 </el-button>
        </div>
      </el-dialog>
      <el-dialog :title="textMap['menu']" :visible.sync="dialogMenuVisible" :close-on-click-modal="false">
        <el-form :rules="rules" label-position="left" label-width="100px" style="width: 600px; margin-left:50px;">
          <el-form-item label="菜单" prop="menuname">
            <tree-select
              :disabled="disabled"
              :height="280"
              :width="200"
              size="small"
              multiple
              :data="parentMenu"
              :default-props="defaultProps"
              collapse-tags
              check-strictly
              :node-key="nodeKey"
              :checked-keys="defaultCheckedKeys"
              @popoverHide="popoverHide"
            />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button class="save" :disabled="isokDisable" @click="updateMenu()"> 确认 </el-button>
          <el-button class="minCancel" @click="dialogMenuVisible = false"> 关闭 </el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import TreeSelect from '@/components/TreeSelect'
import waves from '@/directive/waves' // waves directive
import { isIntegerZero } from '@/utils/validate'
import { PostDataByName, GetDataByName, UpdateDataRelation, getRecuData, failproccess } from '@/api/common'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import { MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
export default {
  name: 'Role',
  components: { Pagination, TreeSelect },
  directives: { waves },
  data() {
    return {
      disabled: false,
      tableKey: 0,
      list: [{ 'sqlname': '', 'id': 0, 'sqlstr': '' }],
      parentMenu: [],
      total: 0,
      listLoading: true,
      requestParam: {
        name: 'createRole',
        offset: 0,
        pagecount: 0,
        params: []
      },
      UpdateDataRelationParam: {
        name: '',
        dataname: '',
        datavalue: '',
        valuename: '',
        values: ''
      },
      dataform: {
        id: '',
        name: '',
        selectMenus: [],
        sort: '',
        enable: ''
      },
      getdataListParm: { name: 'getRoleall',
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: { pastureid: Cookies.get('pastureid') }},
      getMenuParm: { name: 'getMenuandButtonRecuRule',
        idname: 'id',
        params: [-1] },
      rules: {
        name: [{ type: 'string', required: true, message: '名称必填', trigger: 'change' }],
        sort: [{ validator: isIntegerZero, trigger: 'blur' }],
        datarole: [{ required: true, message: this.$t('hardwareTest.bt'), trigger: 'blur' }]
      },
      dialogFormVisible: false,
      dialogMenuVisible: false,
      dialogDataRoleVisible: false,
      selectedMenu: '',
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增',
        menu: '授权菜单',
        dataRole: '数据权限'
      },
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      defaultProps: {
        children: 'children',
        label: 'title'
      },
      nodeKey: 'id',
      defaultCheckedKeys: [],
      getDataRoleListParm: {
        name: 'getDataRole',
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        parammaps: {}
      },
      isokDisable: false,
      dataRoleTemp: {},
      dataRolelist: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    popoverHide(checkedIds, checkedData) {
      this.dataform.selectMenus = checkedIds
      this.selectedMenu = checkedIds
      this.UpdateDataRelationParam.values = checkedIds
    },
    getMenuList() {
      getRecuData(this.getMenuParm).then(response => {
        this.parentMenu = response.data
      })
    },
    getList() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        if (response.data.list !== null) {
          for (var i = 0; i < response.data.list.length; i++) {
            response.data.list[i].enable = parseInt(response.data.list[i].enable)
          }
          this.list = response.data.list
        }
        this.total = response.data.total
        this.getMenuList()
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },
    resetRequestParam() {
      this.dataform.id = ''
      this.dataform.name = ''
      this.dataform.sort = '0'
      this.dataform.enable = '1'
    },
    handleCreate() {
      this.resetRequestParam()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.$refs.name.focus()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.requestParam.name = 'createRole'
          this.requestParam.params = []
          this.requestParam.params[0] = this.dataform.name
          this.requestParam.params[1] = this.dataform.sort
          this.requestParam.params[2] = this.dataform.enable
          PostDataByName(this.requestParam).then(() => {
            this.getList()
            this.dialogFormVisible = false
            this.$notify({
              title: this.$t('common.succes'),
              message: '新增成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    createData_again() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.requestParam.name = 'createRole'
          this.requestParam.params = []
          this.requestParam.params[0] = this.dataform.name
          this.requestParam.params[1] = this.dataform.sort
          this.requestParam.params[2] = this.dataform.enable
          PostDataByName(this.requestParam).then(() => {
            this.$nextTick(() => {
              this.$refs['name'].focus()
            })
            this.getList()
            this.resetRequestParam()
            this.$notify({
              title: this.$t('common.succes'),
              message: '新增成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleMenu(row) {
      this.requestParam.name = 'getMenuByRole'
      this.requestParam.params = []
      this.requestParam.params[0] = row.id
      this.requestParam.pagecount = 0
      this.requestParam.returntype = 'list'
      this.dataform.id = row.id
      this.dialogMenuVisible = true
      GetDataByName(this.requestParam).then(response => {
        this.selectedMenu = response.data.lists.menu_id
        this.defaultCheckedKeys = this.selectedMenu
        this.UpdateDataRelationParam.values = this.defaultCheckedKeys
      })
    },
    updateMenu() {
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.UpdateDataRelationParam.name = 'role_menu'
      this.UpdateDataRelationParam.dataname = 'role_id'
      this.UpdateDataRelationParam.datavalue = this.dataform.id
      this.UpdateDataRelationParam.valuename = 'menu_id'
      this.UpdateDataRelationParam.values = this.selectedMenu

      UpdateDataRelation(this.UpdateDataRelationParam).then(() => {
        this.dialogMenuVisible = false
        this.$notify({
          title: this.$t('common.succes'),
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    handleUpdate(row) {
      this.dataform.id = row.id
      this.dataform.name = row.name
      this.dataform.sort = row.sort
      this.dataform.enable = row.enable
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
        this.$refs.name.focus()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.requestParam.name = 'updateRole'
          this.requestParam.params = []
          this.requestParam.params[0] = this.dataform.name
          this.requestParam.params[1] = this.dataform.sort
          this.requestParam.params[2] = this.dataform.enable
          this.requestParam.params[3] = this.dataform.id
          PostDataByName(this.requestParam).then(() => {
            this.getList()
            this.resetRequestParam()
            this.dialogFormVisible = false
            this.$notify({
              title: this.$t('common.succes'),
              message: '修改成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleEnableChange(index, row) {
      this.requestParam.name = 'updateRole'
      this.requestParam.params = []
      this.requestParam.params[0] = row.name
      this.requestParam.params[1] = row.sort
      this.requestParam.params[2] = row.enable
      this.requestParam.params[3] = row.id
      PostDataByName(this.requestParam).then(() => {
        this.$notify({
          title: this.$t('common.succes'),
          message: '修改成功',
          type: 'success',
          duration: 2000
        })
      })
    },
    form_delete(row) {
      MessageBox.confirm('名称：' + row.name, '确认删除？', {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning'
      }).then(() => {
        this.requestParam.name = 'deleteRole'
        this.requestParam.params = []
        this.requestParam.params[0] = row.id
        PostDataByName(this.requestParam).then(() => {
          this.getList()
          this.resetRequestParam()
          this.dialogFormVisible = false
          this.$notify({
            title: this.$t('common.succes'),
           message: this.$t('common.delSuccess'),
            type: 'success',
            duration: 2000
          })
        })
      })
    },
    handleDataRole(row) {
      this.dataRoleTemp = Object.assign({}, row)
      this.$set(this.dataRoleTemp, 'datarole', 0)
      this.dialogDataRoleVisible = true
      this.getDataRoleListParm.parammaps.id = row.id
      GetDataByName(this.getDataRoleListParm).then(response => {
        this.dataRolelist = response.data.list
        this.dataRoleTemp.datarole = response.data.list[0].datarole
        this.$forceUpdate()
      })
    },
    updateDataRole() {
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.$refs['dataRoleTemp'].validate((valid) => {
        if (valid) {
          this.requestParam.name = 'setDataRole'
          this.requestParam.parammaps = {}
          this.requestParam.parammaps.id = this.dataRoleTemp.id
          this.requestParam.parammaps.datarole = this.dataRoleTemp.datarole
          PostDataByName(this.requestParam).then(response => {
            console.log('设置数据权限保存发送参数', this.requestParam)
            if (response.msg !== 'fail') {
              this.getList()
              this.dialogDataRoleVisible = false
              this.$notify({
                title: this.$t('common.succes'),
                message: '设置成功',
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify({ title: this.$t('driver.saveFail'), message: response.data, type: 'warning', duration: 2000 })
            }
          })
        }
      })
    }
  }
}
</script>
