<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="单位名称称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入单位名称称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位类别" prop="companyClassId">
        <el-input v-model="queryParams.companyClassId" placeholder="请输入单位类别" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位地址" prop="address">
        <el-input v-model="queryParams.address" placeholder="请输入单位地址" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位负责人" prop="leaderName">
        <el-input v-model="queryParams.leaderName" placeholder="请输入单位负责人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="单位成果数量" prop="achievementNum">
        <el-input v-model="queryParams.achievementNum" placeholder="请输入单位成果数量" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="录入时间" prop="createAt">
        <el-date-picker clearable v-model="queryParams.createAt" type="daterange" value-format="yyyy-MM-dd"
          range-separator="-" start-placeholder="开始时间" end-placeholder="结束时间">
        </el-date-picker>
      </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="['cs:csCompany: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="['cs:csCompany: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="['cs:csCompany: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="['cs:csCompany:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="csCompanyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="序号" align="center" prop="sort">
        <template slot-scope="scope">
          {{ ((queryParams.pageNum - 1) * queryParams.pageSize) + (scope.$index + 1) }}
        </template>
      </el-table-column>
      <el-table-column label="单位名称" align="center" prop="companyName" />
      <el-table-column label="单位类别" align="center" prop="companyClass" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="单位负责人" align="center" prop="leaderName" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="单位成果数量" align="center" prop="achievementNum" />
      <el-table-column label="成员数量" align="center" prop="userNum" />
      <el-table-column label="录入时间" align="center" prop="createAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <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" @click="handleUpdate(scope.row)"
            v-hasPermi="['cs:csCompany:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['cs:csCompany: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="100px">
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="单位类别" prop="companyClass">
          <el-input v-model="form.companyClass" placeholder="请输入单位类别" />
        </el-form-item>
        <!--<el-form-item label="省份ID" prop="provinceId">
          <el-input v-model="form.provinceId" placeholder="请输入省份ID" />
        </el-form-item>-->
        <el-form-item label="省份" prop="provinceName">
          <el-input v-model="form.provinceName" placeholder="请输入省份" />
        </el-form-item>
        <!--<el-form-item label="城市ID" prop="cityId">
          <el-input v-model="form.cityId" placeholder="请输入城市ID" />
        </el-form-item>-->
        <el-form-item label="城市" prop="cityName">
          <el-input v-model="form.cityName" placeholder="请输入城市" />
        </el-form-item>
        <!--<el-form-item label="区县ID" prop="areaId">
          <el-input v-model="form.areaId" placeholder="请输入区县ID" />
        </el-form-item>-->
        <el-form-item label="区县" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区县" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <!--<el-form-item label="单位负责人ID" prop="leaderId">
          <el-input v-model="form.leaderId" placeholder="请输入单位负责人ID" />
        </el-form-item>-->
        <el-form-item label="单位负责人" prop="leaderName">
          <el-input v-model="form.leaderName" placeholder="请输入单位负责人" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="单位成果数量" prop="achievementNum">
          <el-input v-model="form.achievementNum" placeholder="请输入单位成果数量" />
        </el-form-item>
        <el-form-item label="成员数量" prop="userNum">
          <el-input v-model="form.userNum" placeholder="请输入成员数量" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCsCompany, getCsCompany, delCsCompany, addCsCompany, updateCsCompany } from "@/api/cs/csCompany";

export default {
  name: "CsCompany",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单位信息表格数据
      csCompanyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        parentId: null,
        companyClassId: null,
        companyClass: null,
        companyStatus: null,
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        leaderId: null,
        leaderName: null,
        phone: null,
        achievementNum: null,
        userNum: null,
        content: null,
        sort: null,
        remarks: null,
        status: null,
        createAt: null,
        updateAt: null,
        delFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询单位信息列表 */
    getList() {
      this.loading = true;
      listCsCompany(this.queryParams).then(response => {
        this.csCompanyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        companyId: null,
        companyName: null,
        parentId: null,
        companyClassId: null,
        companyClass: null,
        companyStatus: "0",
        provinceId: null,
        provinceName: null,
        cityId: null,
        cityName: null,
        areaId: null,
        areaName: null,
        address: null,
        leaderId: null,
        leaderName: null,
        phone: null,
        achievementNum: null,
        userNum: null,
        content: null,
        sort: null,
        remarks: null,
        status: "0",
        createAt: null,
        updateAt: null,
        delFlag: 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.companyId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加单位信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      console.log(row)
      this.$router.push({ name: 'firstUnitDetail', params: { id: row.companyId } })
      return
      this.reset();
      const companyId = row.companyId || this.ids
      getCsCompany(companyId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改单位信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.companyId != null) {
            updateCsCompany(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCsCompany(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const companyIds = row.companyId || this.ids;
      this.$modal.confirm('是否确认删除？').then(function () {
        return delCsCompany(companyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cs/csCompany/export', {
        ...this.queryParams
      }, `单位信息列表_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
