<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item>
          <el-select v-model="queryParams.customerType" placeholder="客户类型" clearable size="small">
            <el-option
              v-for="dict in companyTypeOptions"
              :key="dict.guid"
              :label="dict.typeName"
              :value="dict.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.companyName"
            placeholder="企业名称"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.orgCode"
            placeholder="组织机构代码"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="queryParams.pid"
            placeholder="证件号"
            clearable
            size="small"
            style="width: 240px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['customerManage:company:create']"
          >新增企业</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
      <el-table v-loading="loading" :data="list">
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column label="企业账号" align="center" prop="account" />
        <el-table-column label="企业类型" align="center" prop="companyType" :formatter="statusFormat" />
        <el-table-column label="企业昵称" align="center" prop="companyName" />
        <el-table-column label="组织机构代码" align="center" prop="orgCode" />
        <el-table-column label="联系人" align="center" prop="principals" />
        <el-table-column label="电话" align="center" prop="phone" />
        <el-table-column label="邮箱" align="center" prop="email" />
        <el-table-column
          label="操作"
          align="center"
          width="280"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-view"
              @click="handleView(scope.row)"
              v-hasPermi="['customerManage:company:view']"
            >查看</el-button>
            <el-button
              size="mini"
              type="success"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
              v-hasPermi="['customerManage:company:edit']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              icon="el-icon-delete"
              v-hasPermi="['customerManage:company:del']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNo"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <add-mobiel
      			v-if="modal.visibleModal"
      			:title="modal.title"
      			:action="modal.action"
      			:visible="modal.visibleModal"
      			:selectRow="modal.selectRow"
      			@closeModal="closeModal">
      		</add-mobiel>
  </div>
</template>

<script>
import { listCompany , delcompany , companyType } from "@/api/customerManage/company";
import addMobiel from './modal/addMobiel'
export default {
  name: "company",
  components: {
    addMobiel
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: null,
      // 状态数据字典
      companyTypeOptions: [],
      // 表单参数
      form: {},
      //弹出层
      modal:{
        action:'',
        title:'',
        visibleModal:false,
        selectRow:null
      },
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10
      }
    };
  },
  created() {
    this.getCompanyType();
  },
  mounted() {
    this.getList();
  },
  methods: {
    /** 企业类型列表 */
    getCompanyType(){
      companyType().then(res => {
          if(res.retHead.errCode == 0){
            this.companyTypeOptions = res.retBody
          }
        }
      );
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCompany(this.queryParams).then(res => {
          if(res.retHead.errCode == 0){
            this.list = res.retBody
            this.total = res.retHead.total;
          }
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      // this.resetForm("queryForm");
      this.queryParams = {
        pageNo:1,
        pageNo:10
      }
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
        this.modal.visibleModal = true
        this.modal.title = '企业添加'
        this.modal.action = 'add'
    },
    /** 查看按钮操作 */
    handleView(item){
        this.modal.visibleModal = true
        this.modal.title = '企业详情'
        this.modal.action = 'view'
        this.modal.selectRow = item
    },
    /** 查看按钮操作 */
    handleEdit(item){
        this.modal.visibleModal = true
        this.modal.title = '企业编辑'
        this.modal.action = 'edit'
        this.modal.selectRow = item
    },
    /** 删除按钮操作 */
    handleDelete(item) {
      let params = {
        "customerInfoForm":{
            "guid":item.fkCustomerId
        },
        "customerCompanyInfoForm":{
            "companyName":item.companyName,
            "guid":item.guid,
            "orgCode":item.orgCode,
            "principals":item.principals,
            "phone":item.phone,
            "email":item.email,
            "companyAddress":item.companyAddress,
            "companyImages":item.companyImages
        }
      }
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delcompany(params);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    // 字典状态字典翻译
    statusFormat(row, column) {
      return this.selectCompanyLabel(this.companyTypeOptions, row.companyType);
    },
    selectCompanyLabel(datas, value) {
    	var actions = [];
    	Object.keys(datas).some((key) => {
    		if (datas[key].guid == ('' + value)) {
    			actions.push(datas[key].typeName);
    			return true;
    		}
    	})
    	return actions.join('');
    },
    //窗口关闭
    closeModal(flag = false){
      this.modal.visibleModal = false
      flag && this.getList()
    }
  }
};
</script>
