<template>
  <div class="app-container">
    <el-dialog :visible.sync="showLongitude" append-to-body>
      经度
      <el-input v-model="form.longitude"></el-input>
      纬度
      <el-input v-model="form.latitude"></el-input>
      <supermap ref="enterprisemap" :visible.sync="showLongitude"
                style="width: 100%;height: 500px;" :setLatLng="setLatLng"></supermap>
      <el-button type="primary" @click="showLongitude=false">确定</el-button>
    </el-dialog>

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="企业名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属部门" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入所属部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入录入人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="组织机构代码" prop="orgCode">
        <el-input
          v-model="queryParams.orgCode"
          placeholder="请输入组织机构代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主要负责人" prop="mainPerson">
        <el-input
          v-model="queryParams.mainPerson"
          placeholder="请输入主要负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人电话" prop="mainPersonPhone">
        <el-input
          v-model="queryParams.mainPersonPhone"
          placeholder="请输入主要负责人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="企业编码" prop="enterpriseCode">
        <el-input
          v-model="queryParams.enterpriseCode"
          placeholder="企业编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:enterprise:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:enterprise:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:enterprise:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:enterprise:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="企业名称" align="center" prop="businessName"/>
      <el-table-column label="经营地址" align="center" prop="businessAddress"/>
      <el-table-column label="办公地址" align="center" prop="officeAddress"/>
      <el-table-column label="主要负责人" align="center" prop="mainPerson"/>
      <el-table-column label="主要负责人电话" align="center" prop="mainPersonPhone"/>
      <el-table-column label="所属部门" align="center" prop="deptName" />
      <el-table-column label="录入人" align="center" prop="userName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.userId == loginUserId"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:enterprise:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.userId == loginUserId"
            v-hasPermi="['system:enterprise:remove']"
          >废弃
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:enterprise:remove']"
          >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="list-icon-sj iconfont icon-richangxuncha"
            @click="toXuncha(scope.row)"
            v-hasPermi="['system:enterprise:remove']"
          >巡查
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="list-icon-sj iconfont icon-xungengdian"
            v-if="scope.row.userId == loginUserId"
            @click="toPatrolstation(scope.row)"
            v-hasPermi="['system:enterprise:remove']"
          >巡更点
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改企业对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属部门" prop="deptId">
              <!--<el-input v-model="form.deptId" placeholder="请选择企业所属部门"/>-->
              <treeselect :disabled="isDetail" v-model="form.deptId" :options="deptOptions" :show-count="true"
                          placeholder="请选择归属部门"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="企业登录人账号" prop="accountId">
              <el-input v-model="form.accountId" placeholder="请选择企业登录人账号" disabled/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业名称" prop="businessName">
              <el-input :disabled="isDetail" v-model="form.businessName" placeholder="请输入企业名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="经营地址" prop="businessAddress">
              <el-input :disabled="isDetail" v-model="form.businessAddress" placeholder="请输入经营地址"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="办公地址" prop="officeAddress">
              <el-input :disabled="isDetail" v-model="form.officeAddress" placeholder="请输入办公地址"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="组织机构代码" prop="orgCode">
              <el-input :disabled="isDetail" v-model="form.orgCode" placeholder="请输入组织机构代码"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="主要负责人" prop="mainPerson">
              <el-input :disabled="isDetail" v-model="form.mainPerson" placeholder="请输入主要负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="主要负责人电话" prop="mainPersonPhone">
              <el-input :disabled="isDetail" v-model="form.mainPersonPhone" placeholder="请输入主要负责人电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="安全负责人" prop="headSecurity">
              <el-input :disabled="isDetail" v-model="form.headSecurity" placeholder="请输入安全负责人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="安全负责人电话" prop="headSecurityPhone">
              <el-input :disabled="isDetail" v-model="form.headSecurityPhone" placeholder="请输入安全负责人电话"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="属地监管人员" prop="supervisor">
              <el-input :disabled="isDetail" v-model="form.supervisor" placeholder="请输入属地监管人员"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="区县行业主管部门" prop="tradeDept">
              <el-input :disabled="isDetail" v-model="form.tradeDept" placeholder="请输入区县行业主管部门"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="经度" prop="longitude" @dblclick.native="showMap">
              <el-input :disabled="isDetail" v-model="form.longitude" placeholder="请输入经度"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="纬度" prop="latitude" @dblclick.native="showMap">
              <el-input :disabled="isDetail" v-model="form.latitude" placeholder="请输入纬度"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="企业编码" prop="enterpriseCode">
              <el-input :disabled="isDetail" v-model="form.enterpriseCode" placeholder="企业编码"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-show="!isDetail">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import {listEnterprise, getEnterprise, delEnterprise, addEnterprise, updateEnterprise} from "@/api/centerdata_emergency1/enterprise";
  import {listUser, deptTreeSelect,} from "@/api/system/user";
  import {getLoginUser} from "@/api/centerdata_emergency1/main";

  import supermap from './supermap'

  export default {
    components: {
      Treeselect,
      supermap
    },
    name: "Enterprise",
    data() {
      return {

        showLongitude: false,
        isDetail: false,

        //当前登录人ID
        loginUserId: 0,
        // 遮罩层
        loading: true,
        // 部门树选项
        deptOptions: undefined,
        // 选中数组
        ids: [],
        enterpriseNames: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 企业表格数据
        enterpriseList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          userId: null,
          accountId: null,
          businessName: null,
          businessAddress: null,
          officeAddress: null,
          orgCode: null,
          mainPerson: null,
          mainPersonPhone: null,
          headSecurity: null,
          headSecurityPhone: null,
          supervisor: null,
          tradeDept: null,
          longitude: null,
          latitude: null,
          dataStatus: null,
          deptName: null,
          userName: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          deptId: [
            {required: true, message: "所属部门不能为空", trigger: "blur"}
          ],
          enterpriseCode: [
            {required: true, message: "企业码不能为空", trigger: "blur"}
          ],
          businessName: [
            {required: true, message: "企业名称不能为空", trigger: "blur"}
          ],
          longitude: [
            {required: true, message: "经纬度坐标不能为空", trigger: "blur"},
            {
              type: "number", message: "经纬度只能为数字", trigger: "blur", transform(value) {
                return Number(value)
              }
            }
          ],
          mainPersonPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          headSecurityPhone: [
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ],
          latitude: [
            {required: true, message: "经纬度坐标不能为空", trigger: "blur"},
            {
              type: "number", message: "经纬度只能为数字", trigger: "blur", transform(value) {
                return Number(value)
              }
            }
          ],
        }
      };
    },
    created() {
      this.getList();
      this.getDeptTree();
      this.getLoginUser();
    },
    methods: {
      getLoginUser() {
        getLoginUser().then(response => {
          this.loginUserId = response.data.userid;
        });
      },
      toXuncha(row) {
        this.$router.push({path: '/edyj/log', query: {busEnterpriseId: row.busEnterpriseId}})
      },

      toPatrolstation(row) {
        this.$router.push({
          path: '/edyj/patrolstation',
          query: {busEnterpriseId: row.busEnterpriseId, busEnterpriseName: row.busEnterpriseName}
        })
      },

      showMap() {
        if (!this.isDetail) {
          this.showLongitude = true
          this.$nextTick(() => {
            if (this.form.latitude != null && this.form.latitude != undefined && this.form.longitude != null && this.form.longitude != undefined) {
              this.$refs.enterprisemap.setMarker(this.form.latitude, this.form.longitude)
            }
          })
        }
      },
      setLatLng: function (lat, lng) {
        this.form.latitude = lat
        this.form.longitude = lng
      },
      getDeptTree() {
        deptTreeSelect().then(response => {
          this.deptOptions = response.data;
        });
      },
      /** 查询企业列表 */
      getList() {
        this.loading = true;
        listEnterprise(this.queryParams).then(response => {
          this.enterpriseList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          busEnterpriseId: null,
          deptId: null,
          userId: null,
          accountId: null,
          businessName: null,
          businessAddress: null,
          officeAddress: null,
          orgCode: null,
          mainPerson: null,
          mainPersonPhone: null,
          headSecurity: null,
          headSecurityPhone: null,
          supervisor: null,
          tradeDept: null,
          createTime: null,
          longitude: null,
          latitude: null,
          dataStatus: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.busEnterpriseId)
        this.enterpriseNames = selection.map(item => item.businessName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.isDetail = false
        this.reset();
        this.open = true;
        this.title = "添加企业";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.isDetail = false
        this.reset();
        const busEnterpriseId = row.busEnterpriseId || this.ids
        getEnterprise(busEnterpriseId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改企业";
        });
      },
      handleDetail(row) {
        this.isDetail = true
        this.reset();
        const busEnterpriseId = row.busEnterpriseId || this.ids
        getEnterprise(busEnterpriseId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "企业信息详情";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.busEnterpriseId != null) {
              updateEnterprise(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addEnterprise(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const busEnterpriseIds = row.busEnterpriseId || this.ids;
        const enterpriseNames = row.businessName || this.enterpriseNames;
        this.$modal.confirm('是否确认废弃企业为["' + enterpriseNames + '"]的数据项？').then(function () {
          return delEnterprise(busEnterpriseIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("废弃成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/enterprise/export', {
          ...this.queryParams
        }, `enterprise_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
