<template>

  <div class="app-container">
    <el-form
      ref="dataForm"
      v-enterToNext="true"
      :rules="rules"
      :model="dataform"
      label-position="left"
      label-width="100px"
      style="width: 600px; margin-left:50px;"
    >
      <el-form-item label="部门" prop="deptid">
        <tree-select
          :disabled="disabled"
          :height="280"
          :width="250"
          size="small"
          :data="parentDept"
          :default-props="defaultProps"
          :node-key="nodeKey"
          :checked-keys="defaultCheckedKeys"
          @popoverHide="popoverHide"
        />
      </el-form-item>
      <el-button type="primary" style="position:absolute;top:26px;left:455px;" size="mini" @click="handleClickSure">确认</el-button>
    </el-form>
  </div>
</template>

<script>
import TreeSelect from '@/components/TreeSelect'
import waves from '@/directive/waves' // waves directive
import { isIntegerZero } from '@/utils/validate'
import enterToNext from '@/directive/enterToNext' // enterToNext directive
import { GetDataByName, getRecuData, PostDataByName } from '@/api/common'
// import Cookies from 'js-cookie'
export default {
  name: 'Template',
  components: { TreeSelect },
  directives: { waves, enterToNext },
  props: {
    assetTypeid: {
      type: Number,
      default: 0
    },
    deptId: {
      type: Number,
      default: 0
    },
    typeNumber: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      disabled: false,
      tableKey: 0,
      list: [{ sqlname: '', id: 0, sqlstr: '' }],
      parentDept: [],
      total: 0,
      listLoading: true,
      requestParam: {
        name: 'createapisql',
        offset: 0,
        pagecount: 0,
        params: []
      },
      requestParamsDept: {
        name: 'updateAssetTypeDept',
        offset: 0,
        pagecount: 0,
        params: []
      },
      dataform: {
        deptid: ''
      },
      // eslint-disable-next-line no-undef
      getDeptParm: { name: 'getdeptrecu', idname: 'value', params: [3] },
      getdataListParm: {
        name: 'getEmpAll',
        offset: 1,
        pagecount: 10,
        returntype: 'Map',
        params: []
      },
      rules: {
        empname: [
          {
            type: 'string',
            required: true,
            message: '员工名必填',
            trigger: 'change'
          }
        ],
        sort: [{ validator: isIntegerZero, trigger: 'blur' }]
      },
      dialogFormVisible: false,
      selectedRole: '',
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' },
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      nodeKey: 'value',
      defaultCheckedKeys: []
    }
  },
  watch: {
    assetTypeid(val) {
      if (this.assetTypeid != null) {
        this.getDeptList()
      }
    },
    deptId(val) {
      if (this.deptId != null) {
        this.getDeptList()
      }
    },
    typeNumber(val) {
      if (this.typeNumber != null) {
        this.getDeptList()
      }
    }

  },
  created() {
    this.getList()
  },
  methods: {

    popoverHide(checkedIds, checkedData) {
      this.dataform.deptid = checkedIds
      this.requestParamsDept.params[0] = this.dataform.deptid

      // var str1 = this.typeNumber
      // var str2 = str1.substring(0, 5)
      // console.log(this.typeNumber)

      // this.requestParamsDept.params[1] = str2

      // PostDataByName(this.requestParamsDept).then(response => {
      //   if (response.msg === 'ok') {
      //     this.$notify({
      //       title: '成功',
      //       message: '更新成功',
      //       type: 'success',
      //       duration: 2000
      //     })
      //   } else {
      //     this.$notify({
      //       title: '失败',
      //       message: '更新失败',
      //       type: 'danger',
      //       duration: 2000
      //     })
      //   }
      // })
    },
    handleClickSure(typeNumber) {
      var str1 = this.typeNumber
      var str2 = str1.substring(0, 5)
      // console.log(this.typeNumber)
      this.requestParamsDept.params[1] = str2
      PostDataByName(this.requestParamsDept).then(response => {
        if (response.msg === 'ok') {
          this.$notify({
            title: '成功',
            message: '更新成功',
            type: 'success',
            duration: 2000
          })
        } else {
          this.$notify({
            title: '失败',
            message: '更新失败',
            type: 'danger',
            duration: 2000
          })
        }
      })
    },
    deptenter() {
      this.$nextTick(() => {
        this.$refs['remark'].focus()
      })
    },
    getList() {
      this.listLoading = true
      GetDataByName(this.getdataListParm).then(response => {
        this.list = response.data.list
        if (response.data.total) {
          this.total = response.data.total
        }
        this.getDeptList()
        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.5 * 1000)
      })
    },

    getDeptList() {
      getRecuData(this.getDeptParm).then(response => {
        this.parentDept = response.data
      })
    }
  }
}
</script>
