<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属单位" prop="contractorId">
        <el-select v-model="queryParams.contractorId" placeholder="请选择所属单位" filterable clearable size="small">
          <el-option
            v-for="item in contractorInfoList"
            :key="item.id"
            :label="item.contractorName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="人员状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择人员状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </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"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractorPersonList">
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="所属单位" align="center" prop="contractorName" >
        <template slot-scope="scope">
          <span v-if="scope.row.contractorId != '' && scope.row.contractorId != null">{{scope.row.contractorName}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" >
        <template slot-scope="scope">
          <span v-if="scope.row.age != '' && scope.row.age != null">{{scope.row.age}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.sex == '0'">男</span>
          <span v-if="scope.row.sex == '1'">女</span>
          <span v-if="scope.row.sex == '2'">未知</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" >
        <template slot-scope="scope">
          <span v-if="scope.row.phone != '' && scope.row.phone != null">{{scope.row.phone}}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="资质证照" align="center" prop="certificateUrl" style="text-align:center;">
        <template slot-scope="scope">
          <span v-if="scope.row.certificateUrl != null && scope.row.certificateUrl != ''">
          <img :src="scope.row.certificateUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(scope.row)"/>
          <el-image :ref="'a'+scope.row.id" :src="scope.row.certificateUrl" v-show="false" :preview-src-list="[scope.row.certificateUrl]" v-if="scope.row.certificateUrl != '' && scope.row.certificateUrl != null"></el-image>
          </span>
          <span v-else>
            -
          </span>
        </template>
      </el-table-column>
      <el-table-column label="人员状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="操作" align="center" width="300">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleApproval(scope.row)"
          >审核</el-button>
          <el-button
            v-if="scope.row.status != '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBlacklist(scope.row)"
          >加入黑名单</el-button>
          <el-button
            v-if="scope.row.status == '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleBlacklist(scope.row)"
          >移出黑名单</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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-row>
          <el-col :span="11">
            <el-form-item label="所属单位" prop="contractorId">
              <el-select v-model="form.contractorId" placeholder="请选择所属单位" filterable clearable style="width: 100%">
                <el-option
                  v-for="item in contractorInfoList"
                  :key="item.id"
                  :label="item.contractorName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓名" :disabled="readOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="性别" prop="sex">
              <el-select v-model="form.sex" placeholder="请选择性别" style="width: 100%">
                <el-option
                  v-for="dict in sexOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="年龄" prop="age">
              <el-input v-model="form.age" placeholder="请输入年龄" :disabled="readOnly" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入手机号" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="资质名称" prop="certificateName">
              <el-input v-model="form.certificateName" placeholder="请输入资质名称" :disabled="readOnly" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item label="单位资质证照" v-if="!readOnly" prop="certificateUrl">
              <MyFileUpload
                listType="picture-card"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              />
              <el-input v-show="false" disabled v-model="form.certificateUrl"></el-input>
            </el-form-item>
            <el-form-item label="单位资质证照" v-if="readOnly" prop="certificateUrl">
              <img :src="form.certificateUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(form)"/>
              <el-image :zIndex="9999" :ref="'a'+form.id" :src="form.certificateUrl" v-show="false" :preview-src-list="[form.certificateUrl]" v-if="form.certificateUrl != '' && form.certificateUrl != null"></el-image>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="23">
            <el-form-item v-show="operate" label="审核" prop="status">
              <el-radio v-model="form.status" label="1">通过</el-radio>
              <el-radio v-model="form.status" label="0">驳回</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
      </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 MyFileUpload from '@/components/MyFileUpload';
import { listContractorPerson, getContractorPerson, delContractorPerson, addContractorPerson, updateContractorPerson, exportContractorPerson } from "@/api/contractor/contractorPerson";
import { listAll } from "@/api/contractor/contractorInfo";

export default {
  name: "ContractorPerson",
  components: {
    MyFileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 承包商人员信息表格数据
      contractorPersonList: [],
      contractorInfoList: [],
      statusOptions: [],
      // 性别状态字典
      sexOptions: [],
      // 文件列表
      fileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      operate: false,
      readOnly: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        contractorId: null,
        name: null,
        age: null,
        sex: null,
        phone: null,
        certificateName: null,
        certificateUrl: null,
        status: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入姓名", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getContractorList();
    this.getDicts("t_contractor_status").then(response => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.sexOptions = response.data;
    });
  },
  methods: {
    // 人员状态
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    getContractorList() {
      this.loading = true;
      listAll().then(response => {
        this.contractorInfoList = response.data;
      });
    },
    /** 查询承包商人员信息列表 */
    getList() {
      this.loading = true;
      listContractorPerson(this.queryParams).then(response => {
        this.contractorPersonList = response.rows;
        console.log("this.contractorPersonList", this.contractorPersonList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractorId: null,
        name: null,
        age: null,
        phone: null,
        certificateName: null,
        certificateUrl: null,
        status: null,
        isDel: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
      this.fileList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.readOnly=false;
      this.reset();
      this.open = true;
      this.title = "添加承包商人员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.readOnly=false;
      this.reset();
      const id = row.id || this.ids
      getContractorPerson(id).then(response => {
        this.form = response.data;
        if (this.form.certificateUrl) {
          this.fileList.push({
            url: this.form.certificateUrl,
          });
        }
        this.open = true;
        this.title = "修改承包商人员信息";
      });
    },
    /** 审核按钮操作 */
    handleApproval(row) {
      this.readOnly = true;
      this.reset();
      const systemId = row.systemId || this.ids
      getContractorPerson(row.id).then(response => {
        this.form = response.data;
        this.form.status = '1';
        this.operate = true;
        this.open = true;
        this.title = "审核";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            if(this.operate){
              updateContractorPerson(this.form).then(response => {
                this.msgSuccess("审核成功");
                this.open = false;
                this.operate = false;
                this.readOnly = false;
                this.getList();
              });
            } else {
              updateContractorPerson(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addContractorPerson(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 加入黑名单按钮操作 */
    handleBlacklist(row) {
      // const ids = row.id || this.ids;
      var confirm = '';
      if(row.status != '2'){
        confirm = '是否确认将"' + row.name + '"加入黑名单?';
      } else {
        confirm = '是否确认将"' + row.name + '"移出黑名单?';
      }
      this.$confirm(confirm, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        if(row.status != '2'){
          row.status = '2';
        } else {
          row.status = '0';
        }
        return updateContractorPerson(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("操作成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      // const ids = row.id || this.ids;
      this.$confirm('是否确认删除"' + row.name + '"的信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
        row.isDel = "1";
          return updateContractorPerson(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    getFileInfo(res){
      this.form.certificateUrl = res.url;
      //this.$set(this.feedBookForm,'iconUrl',res.url)
    },
    listRemove(e) {
      this.form.certificateUrl = "";
      this.fileList = [];
    },
    showPicture(row){
      this.$refs['a'+row.id].showViewer = true;
      console.log("===",row.id);
    },
  }
};
</script>
