<template>
  <div class="app-container">
    <el-form label-width="90px">
      <el-row>
        <el-col :span="4">
          <el-form-item label="分公司名称">
            <el-input v-model="searchForm.CORP_NAME" placeholder="分公司名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="10px">
            <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
              搜索
            </el-button>
            <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
              重置
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <!--      <el-table-column :reserve-selection="true" type="selection" width="55" align="center"/>-->
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="CORP_NAME" label="分公司名称" />
      <el-table-column label="属地" >
        <template slot-scope="{row}">
          <span v-if="row.prvinceName">{{ row.prvinceName }}</span>
          <span v-if="row.cityName"> -- {{ row.cityName }}</span>
          <span v-if="row.countryName"> --{{ row.countryName }}</span>
          <span v-if="row.villageName"> -- {{ row.villageName }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="INDUSTRYALL" label="所属行业">
        <!--        <template slot-scope="{row}">-->
        <!--          {{ translate(industryList,row.INDUSTRY) }}-->
        <!--        </template>-->
      </el-table-column>
      <el-table-column prop="count" label="人数"/>
      <el-table-column prop="CORP_STATE" label="分公司状态">
        <template slot-scope="{row}">
          {{ translate(qyztList,row.CORP_STATE) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="110">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-view" size="mini" @click="handleEdit(row.CORPINFO_ID)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <!--
           <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
-->
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>

    <el-dialog :visible.sync="dialogFormEdit" title="查看公司组织架构" width="600px">
      <el-tree :data="treeData" :props="defaultProps"/>
    </el-dialog>
  </div>
</template>

<script>
import SelectTree from '@/components/SelectTree'
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination, SelectTree },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: true,
      del: true,
      edit: true,
      listQuery: {
        page: 1,
        limit: 20
      },
      searchForm: {
        CORP_NAME: '',
        PROVINCE: '',
        CITY: '',
        CORP_STATE: '',
        CREATTIME: ''
      },
      total: 0,
      varList: [],
      qyztList: [],
      options: [{
        value: '0',
        label: '启用'
      }, {
        value: '1',
        label: '禁用'
      }],
      STATE: [],
      industryList: [],
      countryList: [],
      villageList: [],
      treeData: [],
      CITY_CODE: '',
      COUNTRY: '',
      VILLAGE: '',
      config: config,
      dialogFormEdit: false,
      rules: {
        CORP_NAME: [{ required: true, message: '分公司名称不能为空', trigger: 'blur' }],
        NEWCORP_NAME: [{ required: true, message: '新分公司名称不能为空', trigger: 'blur' }],
        PASSWORD: [{ required: true, message: '分公司密码不能为空', trigger: 'blur' }]
      },
      updateBranchNameForm: {
        CORPINFO_ID: '',
        CORP_NAME: '',
        NEWCORP_NAME: '',
        PASSWORD: ''
      },
      defaultProps: {
        children: 'nodes',
        label: 'name'
      }
    }
  },
  created() {
    this.getList()
    this.getDicList('qyztList', '07c2674470c1498ba1ebd74906b3b518')
    this.getDicList('industryList', 'f2598ba72e864eadabf0ca4b664d26b9')
  },
  methods: {
    getRowKey(row) {
      return row.CORPINFO_ID
    },
    // 搜索
    getQuery() {
      this.getList()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/corpinfo/listRetrieval?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        { ...this.searchForm }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.CITY_CODE = data.CITY_CODE
        this.getCountryList()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getCountryList() {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: this.CITY_CODE
        }
      ).then((data) => {
        this.countryList = data.list
      }).catch((e) => {
      })
    },
    getVillageList(value, id) {
      this.COUNTRY = value
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: id
        }
      ).then((data) => {
        this.villageList = data.list
      }).catch((e) => {
      })
    },
    changeVillage(village) {
      this.VILLAGE = village
    },
    // 查看
    handleEdit(ID) {
      this.$parent.activeName = 'IndexView'
      this.$parent.CORPINFO_ID = ID
    },
    enableORDisable(ISUSE, CORPINFO_ID) {
      this.$confirm(ISUSE === '0' ? '确定要启用该账户吗?' : '确定要禁用该账户吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/corpinfo/editisuse',
          {
            CORPINFO_ID,
            ISUSE
          }
        ).then(() => {
          this.$message({
            message: ISUSE === '0' ? '启用成功' : '禁用成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {})
    },
    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'corpinfo:add,corpinfo:del,corpinfo:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.corpinfofhadminadd		// 新增权限
        this.del = data.corpinfofhadmindel		// 删除权限
        this.edit = data.corpinfofhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    goKeyReset() {
      this.searchForm = {
        CORP_NAME: '',
        PROVINCE: '',
        CITY: '',
        CORP_STATE: '',
        CREATTIME: ''
      }
      this.getList()
    },
    resetPwd(id) {
      this.$confirm('确定要重置选中的密码吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/corpinfo/resetPwd',
          {
            CORPINFO_ID: id
          }
        ).then(() => {
          this.$message({
            message: '重置成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 获取数据字典
    getDicList(list, DICTIONARIES_ID) {
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID
        }
      ).then((data) => {
        this[list] = data.list
      }).catch((e) => {
      })
    },
    translate(list, id) {
      for (let i = 0; i < list.length; i++) {
        if (list[i].BIANMA === id || list[i].DICTIONARIES_ID === id) {
          return list[i].NAME
        }
      }
    },
    showBranchName(CORPINFO_ID, CORP_NAME) {
      this.$refs.form && this.$refs.form.resetFields()
      this.dialogFormEdit = true
      this.updateBranchNameForm.CORPINFO_ID = CORPINFO_ID
      this.updateBranchNameForm.CORP_NAME = CORP_NAME
    },
    updateBranchName(ID) {
      requestFN(
        '/department/listTreeCorpInfo',
        {
          CORPINFO_ID: ID
        }
      ).then((data) => {
        this.treeData = JSON.parse(data.zTreeNodes)
      }).catch((e) => {
      })
    }
  }
}
</script>
