<template>
  <div class="app-container">
    <el-form label-width="110px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="相关方单位名称">
            <el-input v-model="searchForm.RELEVANT_UNIT_NAME" placeholder="搜索相关方单位名称" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="属地">
            <el-cascader
              ref="POSSESSION_SEARCH"
              v-model="searchForm.POSSESSION"
              :options="areaList"
              :props="areaProps"
              placeholder="请选择属地"
              style="width: 100%"
              @change="cascaderChange"/>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="审核状态">
            <el-select v-model="searchForm.STATE" style="width: 100%;">
              <el-option v-for="item in stateList" :key="item.ID" :label="item.NAME" :value="item.ID" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用工形式">
            <el-select v-model="searchForm.EMPLOYMENT_FORM" style="width: 100%;">
              <el-option v-for="item in employmentTypeList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="选取形式">
            <el-select v-model="searchForm.SELECT_FORM" style="width: 100%;">
              <el-option v-for="item in selectFormList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="集团单位">
            <el-select v-model="searchForm.CORP_TYPE" style="width: 100%;">
              <el-option v-for="item in corpTypeList" :key="item.BIANMA" :label="item.NAME" :value="item.BIANMA" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <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="corpTypeName" label="集团单位" />
      <el-table-column prop="RELEVANT_UNIT_NAME" label="相关方单位名称"/>
      <el-table-column prop="COMPANY_AREA" label="属地">
        <template slot-scope="{row}">
          <span v-if="row.COMPANY_AREA">
            {{ row.COMPANY_AREA.replaceAll(',',' ') }}
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="MAIN_DEPARTMENT_NAME" label="股份主管部门"/>
      <el-table-column prop="SUPERVISE_CORPINFO_NAME" label="基层单位主管公司"/>
      <el-table-column prop="SUPERVISE_DEPARTMENT_NAME" label="基层单位主管部门" />
      <el-table-column prop="EMPLOYMENT_FORM_NAME" label="用工形式" />
      <el-table-column :formatter="(row) => formatDictionaryLabel(row, '1')" prop="SELECT_FORM" label="选取形式" />
      <el-table-column prop="STATE" label="审核状态">
        <template slot-scope="{row}">
          <span v-if="row.STATE =='0'">未填报</span>
          <span v-if="row.STATE =='1'">待审核</span>
          <span v-if="row.STATE =='2'">已审核</span>
          <span v-if="row.STATE =='3'">已打回</span>
          <span v-if="row.STATE =='4'">已填报</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="200px">
        <template slot-scope="{row}">
          <el-button type="primary" icon="el-icon-view" size="mini" @click="goView(row.CORPINFO_ID)">查看</el-button>
          <el-button v-if="row.STATE == '1'" type="primary" icon="el-icon-s-check" size="mini" @click="handleAudit(row.CORPINFO_ID)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div/>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="failForm.dialogVisible" width="700px" title="审核">
      <el-form label-width="150px">
        <el-form-item label="是否通过审核">
          <el-radio-group v-model="failForm.STATE">
            <el-radio :label="2">是</el-radio>
            <el-radio :label="3">否</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="success" @click="isExamine">提交</el-button>
        <el-button @click="failForm.dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      listLoading: true,
      add: false,
      del: false,
      edit: false,
      listQuery: {
        page: 1,
        limit: 20
      },
      config: config,
      bus_images: [],
      total: 0,
      STATE: '',
      MAIN_DEPARTMENT: '',
      KEYWORDS: '',
      UNITS_TYPE: '',
      EMPLOYMENT_FORM: '', // 用工形式
      varList: [],
      pd: [],
      areaList: [], // 属地
      areaProps: {
        lazy: true,
        lazyLoad: async(node, resolve) => {
          const resData = await this.getAreaDict(
            (node.data && node.data.DICTIONARIES_ID) || 'e725d2a91b8248f4b8f49889038df7de'
          )
          const { level } = node
          resolve(
            resData.map((item) => {
              return {
                DICTIONARIES_ID: item.DICTIONARIES_ID,
                BIANMA: item.BIANMA,
                NAME: item.NAME,
                leaf: level >= 3
              }
            })
          )
        },
        value: 'BIANMA',
        id: 'DICTIONARIES_ID',
        label: 'NAME',
        children: 'children',
        checkStrictly: true
      },
      CORPINFODETAILS_ID: '',
      CORPINFO_ID: '',
      SELECT_FORM: '', // 选取形式
      RELEVANT_UNIT_NAME: '', // 相关方单位名称
      LOCAL: '',
      countryList: [],
      form: {
        UNITS_NAME: '', // 相关方单位名称
        CODE: '', // 统一社会信用代码
        COMPANY_AREA: '', // 属地
        ADDRESS: '', // 企事业单位经营地址
        CONTACTS: '', // 主要负责人
        CONTACTS_PHONE: '', // 主要负责人电话
        LR_MOBILE: '', // 法人手机号
        EMPLOYEES: '', // 职工人数(人)
        CREATE_DATE: '', // 成立时间
        REGCAPITAL: '', // 注册资金(万元)
        TOTALASSETS: '', // 资产总额(万元)
        UNITS_TYPE: ''// 类型
      },
      failForm: {
        STATE: '',
        failForm: '',
        dialogVisible: false,
        CORPINFO_ID: ''
      },
      multipleSelectionAll: [], // 所有选中的数据包含跨页数据
      multipleSelection: [], // 当前页选中的数据
      dialogFormEdit: false,
      dialogType: 'add',
      employmentList: [],
      selectList: [],
      /* employmentList: [
        { ID: '0', NAME: '劳务承包' },
        { ID: '1', NAME: '劳务派遣' }
      ],
      selectList: [
        { ID: '0', NAME: '招标' },
        { ID: '1', NAME: '竞争性谈判' }
      ], */
      stateList: [
        { ID: '0', NAME: '未填报' },
        { ID: '1', NAME: '待审核' },
        { ID: '2', NAME: '已审核' },
        { ID: '3', NAME: '已打回' },
        { ID: '4', NAME: '已填报' }
      ],
      searchForm: {
        SELECT_FORM: '', // 选取形式
        RELEVANT_UNIT_NAME: '', // 相关方单位名称
        EMPLOYMENT_FORM: '', // 用工形式
        STATE: '', // 审核状态
        POSSESSION: [], // 属地
        KEYWORDS: '', // 相关方单位名称
        PROVINCE: '',
        CITY: '',
        COUNTY: '',
        VILLAGE: '',
        CORP_TYPE: ''
      },
      employmentTypeList: [],
      selectFormList: [],
      ecnomicTypeList: [],
      enterpriseSizeList: [],
      corpTypeList: []
    }
  },
  created() {
    this.getList()
    this.getDict()
    // this.getData()
  },
  methods: {
    async getAreaDict(DICTIONARIES_ID) {
      const { list } = await requestFN(
        '/xgf/dictionaries/getAreaLevels',
        {
          DICTIONARIES_ID
        }
      )
      return list
    },
    cascaderChange(e) {
      this.$nextTick(() => {
        const dom = document.getElementsByClassName('el-radio is-checked')
        const radioDom = dom[dom.length - 1]
        const brother = radioDom.nextElementSibling
        if (radioDom.getElementsByClassName('el-radio__original')[0].value.split(',').length < 4) {
          brother.click()
        }
      })
    },
    handleAudit(id) {
      this.failForm.dialogVisible = true
      this.failForm.CORPINFO_ID = id
      this.failForm.STATE = ''
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 处理字典数据
    formatDictionaryLabel(row, type) {
      if (type === '0') {
        // 用工形式
        if (this.employmentTypeList) {
          for (let i = 0; i < this.employmentTypeList.length; i++) {
            const obj = this.employmentTypeList[i]
            if (row.EMPLOYMENT_FORM === obj.BIANMA) {
              return obj.NAME
            }
          }
        }
      } else if (type === '1') {
        // 选取形式
        if (this.selectFormList) {
          const SELECT_FORM = row.SELECT_FORM.split(',')
          const name = []
          for (let i = 0; i < SELECT_FORM.length; i++) {
            for (let j = 0; j < this.selectFormList.length; j++) {
              const obj = this.selectFormList[j]
              if (SELECT_FORM[i] === obj.BIANMA) {
                name.push(obj.NAME)
              }
            }
          }
          return name.join('、')
        }
      }
    },
    getVillageList(value, id) {
      this.LOCAL = value
      requestFN(
        '/dictionaries/getLevels',
        {
          DICTIONARIES_ID: id
        }
      ).then((data) => {
        this.villageList = data.list
      }).catch((e) => {
      })
    },
    goKeyReset() {
      this.searchForm.POSSESSION = ''
      this.searchForm.SELECT_FORM = ''
      this.searchForm.PROVINCE = ''
      this.searchForm.CITY = ''
      this.searchForm.COUNTY = ''
      this.searchForm.VILLAGE = ''
      this.searchForm.STATE = ''
      this.searchForm.RELEVANT_UNIT_NAME = ''
      this.searchForm.EMPLOYMENT_FORM = ''
      this.searchForm.KEYWORDS = ''
      this.searchForm.CORP_TYPE = ''
      this.getQuery()
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/xgf/corp/getCorpInfoList?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.searchForm.RELEVANT_UNIT_NAME,
          PROVINCE: this.searchForm.POSSESSION[0] || '',
          CITY: this.searchForm.POSSESSION[1] || '',
          COUNTY: this.searchForm.POSSESSION[2] || '',
          VILLAGE: this.searchForm.POSSESSION[3] || '',
          STREET: this.searchForm.POSSESSION[4] || '',
          STATE: this.searchForm.STATE,
          EMPLOYMENT_FORM: this.searchForm.EMPLOYMENT_FORM,
          SELECT_FORM: this.searchForm.SELECT_FORM,
          CORP_TYPE: this.searchForm.CORP_TYPE
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
        this.pd = data.pd
      }).catch((e) => {
        this.listLoading = false
      })
    },
    getRowKey(row) {
      return row.CORPINFO_ID
    },
    goView(ID) {
      this.$parent.activeName = 'corpInfoEdit'
      this.$parent.CORPINFO_ID = ID
    },
    isExamine() {
      requestFN(
        '/xgf/corp/examine',
        {
          CORPINFO_ID: this.failForm.CORPINFO_ID,
          STATE: this.failForm.STATE
        }
      ).then((data) => {
        this.failForm.dialogVisible = false
        this.$message.success('审核成功')
        this.getList()
      }).catch((e) => {
      })
    },

    // 判断按钮权限，用于是否显示按钮
    hasButton: function() {
      var keys = 'units:add,units:del,units:edit,toExcel'
      requestFN(
        '/head/hasButton',
        {
          keys: keys
        }
      ).then((data) => {
        this.add = data.unitsfhadminadd		// 新增权限
        this.del = data.unitsfhadmindel		// 删除权限
        this.edit = data.unitsfhadminedit	// 修改权限
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 获取数据字典数据
    getDict() {
      requestFN(
        '/xgf/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '720992d898bf4fd7b44bf0ba1f1bbb88'
        }
      ).then((data) => {
        this.employmentTypeList = data.list
      })
      requestFN(
        '/xgf/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '9ce2e9a4636b4d41b460ce16c346d497'
        }
      ).then((data) => {
        this.selectFormList = data.list
      })
      requestFN(
        '/xgf/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '37d8385931f14bb0b962082cf6bb3395'
        }
      ).then((data) => {
        this.corpTypeList = data.list
      })
      /* requestFN(
        '/xgf/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '688d2cf1c6cd4dab999a0106e09aec83'
        }
      ).then((data) => {
        this.ecnomicTypeList = data.list
      }) */
      /* requestFN(
        '/xgf/dictionaries/getLevels',
        {
          DICTIONARIES_ID: '37b045e160c04ddba851073b4e510cc9'
        }
      ).then((data) => {
        this.enterpriseSizeList = data.list
      }) */
    },
    resetForm() {
      this.form = {
        UNITS_NAME: '', // 相关方单位名称
        CODE: '', // 统一社会信用代码
        POSSESSION: '', // 属地
        ADDRESS: '', // 企事业单位经营地址
        CONTACTS: '', // 主要负责人
        CONTACTS_PHONE: '', // 主要负责人电话
        LR_MOBILE: '', // 法人手机号
        EMPLOYEES: '', // 职工人数(人)
        CREATE_DATE: '', // 成立时间
        REGCAPITAL: '', // 注册资金(万元)
        TOTALASSETS: '', // 资产总额(万元)
        UNITS_TYPE: ''// 类型
      }
    }
  }
}

</script>
