<template>
  <el-container>
    <el-header v-if="isSearch">
      <el-form style="margin: 10px 0" :inline="true">
        <el-form-item label="单位名称：">
          <el-input v-model="search.companyName" placeholder="单位名称" maxlength="30" clearable/>
        </el-form-item>
        <el-form-item label="工种名称：">
          <el-input v-model="search.professionName" placeholder="工种名称" maxlength="30" clearable/>
        </el-form-item>
        <el-form-item>
          <el-button
            style="margin-left: 10px"
            type="primary"
            icon="el-icon-search"
            @click="getList"
          >
            搜索
          </el-button>
          <el-button
            class="commonBtn"
            icon="el-icon-refresh"
            @click="reset"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-header>
    <el-main>
      <div class="headerTop">
        <div>
          <el-button
            type="primary"
            class="el-icon-plus"
            @click="handleItem('add')"
          >
            新增</el-button
          >
          <el-button
            icon="el-icon-delete"
            type="primary"
            :disabled="multipleSelection.length < 1"
            @click="dleSignTemolate"
            >删除</el-button
          >
          <!-- <el-button
            type="primary"
            class="el-icon-download"
            @click="handleExport"
          >
            导出文档</el-button
          > -->
        </div>
        <right-toolbar
          :showSearch.sync="isSearch"
          @queryTable="reset"
        ></right-toolbar>
      </div>
      <el-table
        height="calc(100% - 200px)"
        :data="list"
        :row-style="{ height: '0px' }"
        :header-cell-style="{ 'text-align': 'center', padding: '0px' }"
        :cell-style="{ 'text-align': 'center', padding: '2px' }"
        style="width: 100%"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="50px" />
        <el-table-column label="序号" align="center" width="80px">
          <template slot-scope="scope">
            <span> {{ (pageNum - 1) * pageSize + (scope.$index + 1) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="单位名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.companyName || "-"  }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工种名称" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.professionName || "-"  }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="300">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="handleItem('edit', scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              @click="dleSignTemolate(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :page.sync="pageNum"
        :limit.sync="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        :total="total"
        @pagination="getList"
      />
    </el-main>
    <el-dialog
      :visible.sync="addAlert"
      width="500px"
      :title="title"
      :show-close="false"
      @close="handleClose"
    >
      <el-form
        ref="formData"
        :model="formData"
        label-position="right"
        :rules="rules1"
        style="padding: 0 20px; height: 150px; overflow: auto"
        label-width="100px"
        :disabled="isCheck"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="单位名称：" prop="contractorId">
              <el-select
                style="width: 90%"
                v-model="formData.contractorId"
                clearable
                size="medium"
              >
                <el-option
                  v-for="item in contractorList"
                  :key="item.id"
                  :label="item.companyName"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="工种名称：" prop="professionName">
              <el-input
                style="width: 90%"
                v-model="formData.professionName"
                maxlength="30"
                placeholder="请输入工种名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-row style="margin-top: 10px">
        <el-col align="right">
          <el-button @click="handleClose"> 取消 </el-button>
          <el-button :loading="loading" type="primary" @click="handleSave">
            确定
          </el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </el-container>
</template>

<script>
import {
  queryContractorList,
  queryProfessionList,
  delProfession,
  editProfession,
  getProfessionDetails,
  addProfession,
} from "@/api/contractor.js";
export default {
  name: "RuoyiIndex",

  data() {
    return {
      loading: false,
      //模糊搜索
      search: {
        companyName: "",
        professionName: ""
      },
      contractorList: [],
      title: "",
      type: "",
      total: 0,
      isSearch: true,
      list: [],
      multipleSelection: [],
      pageNum: 1,
      pageSize: 10,
      addAlert: false,
      isCheck: false,
      rowId: "",
      formData: {
        professionName: "",
        contractorId: "",
      },
      rules1: {
        contractorId: [
          { required: true, message: "请选择单位名称", trigger: "blur" },
        ],
        professionName: [
          { required: true, message: "请输入工种名称", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getContractorList();
  },
  methods: {
    reset() {
      this.search = this.$options.data().search;
      this.getList();
    },
    getContractorList() {
      const params = {
        pageNum: 1,
        pageSize: 100,
      };
      queryContractorList(params).then((data) => {
        if (data.code === 200) {
          this.contractorList = data.rows;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    getList() {
      const params = {
        ...this.search,
        pageNum: this.pageNum,
        pageSize: this.pageSize,
      };
      queryProfessionList(params).then((data) => {
        if (data.code === 200) {
          this.list = data.rows;
          this.total = data.total;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    // 全选/反选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    dleSignTemolate(row) {
      let ids =
        row.id || this.multipleSelection.map((item) => item.id).join(",");
      this.$confirm("是否删除？", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delProfession(ids).then((data) => {
            if (data.code === 200) {
              this.$message({
                message: "删除成功",
                type: "success",
              });
              this.getList();
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {
          // 取消后退出
        });
    },
    // 取消添加
    handleClose() {
      this.addAlert = false;
      setTimeout(() => {
        this.isCheck = false;
        this.formData = this.$options.data().formData;
        this.$refs.formData.resetFields();
      }, 200);
    },
    handleSave() {
      console.log(this.formData, "this.imgList");
      this.$refs.formData.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.type === "add") {
            addProfession(this.formData)
              .then((data) => {
                if (data.code === 200) {
                  this.$message({
                    message: "新增成功",
                    type: "success",
                  });
                  this.handleClose();
                  this.getList();
                } else {
                  this.$message.error(data.msg);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          } else {
            editProfession({ ...this.formData, id: this.rowId })
              .then((data) => {
                if (data.code === 200) {
                  this.$message({
                    message: "编辑成功",
                    type: "success",
                  });
                  this.handleClose();
                  this.getList();
                } else {
                  this.$message.error(data.msg);
                }
                this.loading = false;
              })
              .catch(() => {
                this.loading = false;
              });
          }
        } else {
          return false;
        }
      });
    },
    getDetails(id) {
      getProfessionDetails(id).then((data) => {
        if (data.code === 200) {
          this.formData = data.data;
        } else {
          this.$message.error(data.msg);
        }
      });
    },
    handleItem(type, row = {}) {
      this.addAlert = true;
      this.type = type;
      this.rowId = row.id;
      if (type === "add") {
        this.title = "新增";
      } else if (type === "edit") {
        this.title = "编辑";
        this.getDetails(row.id);
      } else if (type === "check") {
        this.title = "查看";
        this.isCheck = true;
        this.getDetails(row.id);
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/doubledefense/profession/export",
        {},
        `job_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>

<style lang="scss" scoped></style>
