<template>
  <basic-container>
    <el-drawer title="评价" append-to-body :visible.sync="drawer" size="60%">
      <avue-crud :option="option" :table-loading="loading" :data="data" :page.sync="page" ref="crud" v-model="obj"
        :permission="permissionList" @search-change="searchChange" @search-reset="searchReset"
        @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
        @refresh-change="refreshChange" class="customPage" @on-load="getList">
        <!--自定义列-->
        <template slot="resume" slot-scope="{ row }">
          <div>
            <el-button type="text" size="mini" @click="$refs.resume.openDialog(row)"
              v-if="row.userId && row.userId !== '0'">查看简历</el-button>
            <div v-else>暂无简历</div>
          </div>
        </template>
        <template slot="scoreAll" slot-scope="{ row }">
          <div @click="rowAppraise(row)">
            <el-rate v-model="row.scoreAll" disabled></el-rate>
          </div>
        </template>
        <!--/自定义列-->
        <!--自定义按钮-->
        <template slot="menuLeft">
          <el-button type="primary" icon="el-icon-plus" @click="handleImport" size="small"
            :disabled="!selectionList.length">导入人才库</el-button>
        </template>
        <!--/自定义按钮-->
      </avue-crud>
      <!--评价dialog-->
      <el-dialog title :close-on-click-modal="false" :close-on-press-escape="false" :append-to-body="true" width="500px"
        :visible.sync="box" @closed="closed">
        <el-form :model="form" :rules="rules" ref="appraiseForm">
          <el-form-item prop="scoreAll">
            评价：
            <el-rate :disabled="!isCanRate && !rateLoading" v-model="form.scoreAll"
              style="display: inline-block; vertical-align: text-bottom"></el-rate>
          </el-form-item>
          <el-form-item prop="remarks">
            <el-input type="textarea" placeholder="请输入您的评价" v-model="form.remarks" minlength="0" maxlength="50"
              show-word-limit :disabled="!isCanRate && !rateLoading" :autosize="{ minRows: 5, maxRows: 5 }"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button icon="el-icon-check" type="primary" size="small" :loading="rateLoading" @click="submit"
            v-show="isCanRate === true">提 交</el-button>
          <el-button icon="el-icon-circle-close" :loading="rateLoading" size="small" @click="box = false"
            v-show="isCanRate === true">取 消</el-button>
        </span>
      </el-dialog>
      <!--/评价dialog-->
      <!--导入人才库dialog-->
      <el-dialog title="导入人才库" append-to-body :visible.sync="importBox" :close-on-click-modal="false"
        :close-on-press-escape="false" @closed="closedGorups" width="555px">
        <avue-form ref="groups" :option="importOption" v-model="importForm" @submit="submitImport">
          <template slot-scope="{ disabled }" slot="menuForm">
            <!-- <el-button type="primary" icon="el-icon-check" @click="copy" :disabled="formLoading">提交</el-button> -->
            <el-button icon="el-icon-circle-close" @click="importBox = false" :loading="disabled">取消</el-button>
          </template>
        </avue-form>
      </el-dialog>
      <!--/导入dialog-->
    </el-drawer>
    <Resume ref="resume"></Resume>
    <!--导入人才库提示错误信息-->
    <el-dialog title="提示" :visible.sync="errorDialogVisible" width="30%" :before-close="handleClose" append-to-body>
      <span style="margin-top: -10px;display: block;"><p v-for="(item,index) in errorTipList" :key="index">{{item}}</p></span>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {
    getRateList,
    getRateDetail,
    submitRate,
    importTalents,
  } from "@/api/tenant/postzp";
  import Resume from "@/components/resume/index";
  // import { isExcel } from "@/util/validate";
  //   import {mapGetters} from "vuex";

  export default {
    comments: {},
    components: {
      Resume,
    },
    data() {
      const validateRate = (rule, value, callback) => {
        if (value > 0) {
          callback();
        } else {
          callback(new Error("请选择评分"));
        }
      };
      return {
        errorDialogVisible:false,
        errorTipList:[],
        missionId: "",
        worktypeNames: "",
        userId: "",
        loading: false,
        rateLoading: false,
        isIndeterminate: false,
        checkAll: false,
        checked: false,
        isCanRate: false,
        form: {},
        rules: {
          scoreAll: [
            { required: true, validator: validateRate, trigger: "change" },
          ],
          remarks: [
            {
              min: 0,
              max: 50,
              message: "长度在 0 到 50 个字符",
              trigger: "blur",
            },
          ],
        },
        selectionList: [],
        page: {
          pageSize: 20,
          currentPage: 1,
          total: 0,
        },
        query: {},
        drawer: false,
        box: false,
        data: [],
        obj: {},
        option: {
          height: "auto",
          calcHeight: 30,
          align: "center",
          menuAlign: "center",
          menu: false,
          tip: false,
          addBtn: false,
          viewBtn: false,
          delBtn: false,
          editBtn: false,
          excelBtn: false,
          columnBtn: false,
          searchBtn: true,
          searchShow: true,
          searchMenuSpan: 7,
          border: true,
          index: true,
          indexLabel: "序号",
          selection: true,
          dialogType: "drawer",
          dialogWidth: "60%",
          dialogClickModal: false,
          column: [
            {
              label: "姓名",
              prop: "realName",
              search: true,
              searchLabelWidth: 45,
              searchSpan: 7
            },
            {
              label: "性别",
              prop: "sex",
              type: "select",
              dicData: [
                {
                  label: "男",
                  value: 1,
                },
                {
                  label: "女",
                  value: 2,
                },
              ],
            },
            {
              label: "年龄",
              prop: "age",
            },
            // {
            //   label: "简历",
            //   prop: "resume",
            //   slot: true
            // },
            {
              label: "评价",
              prop: "scoreAll",
              slot: true,
              width: 180,
            },
          ],
        },
        importForm: {},
        importOption: {
          menuPosition: "right",
          menuBtn: true,
          submitBtn: true,
          submitText: "导入",
          emptyBtn: false,
          column: [
            {
              label: "所属分组",
              prop: "groupId",
              type: "tree",
              span: 24,
              display: true,
              dicUrl: `/api/jobslink-api/tenant/talents/group/listAll`,
              dicMethod: "get",
              dicFormatter: (res) => {
                return res.data.list; //返回字典的层级结构
              },
              props: {
                label: "groupName",
                value: "id",
              },
              rules: [
                {
                  required: true,
                  message: "请选择分组",
                  trigger: "change",
                },
              ],
              placeholder: "请选择分组",
            },
          ],
        },
        importBox: false,
      };
    },
    computed: {
      //   ...mapGetters(["permission"]),
      //   permissionList() {
      //     return {
      //       addBtn: this.vaildData(this.permission.topmenu_add, false),
      //       viewBtn: this.vaildData(this.permission.topmenu_view, false),
      //       delBtn: this.vaildData(this.permission.topmenu_delete, false),
      //       editBtn: this.vaildData(this.permission.topmenu_edit, false)
      //     };
      //   },
      ids() {
        let ids = [];
        this.selectionList.forEach((ele) => {
          ids.push(ele.id);
        });
        return ids.join(",");
      },
    },
    watch: {},
    methods: {
      /*打开drawer*/
      openDialog(row) {
        this.missionId = row.missionNo;
        this.worktypeNames = row.worktypeNames;
        if (this.$refs.crud) {
          this.$refs.crud.searchReset();
        } else {
          this.query = {};
          this.getList(this.page, this.query);
        }
        this.drawer = true;
      },
      /*关闭评分dialog*/
      closed() {
        if (this.$refs.appraiseForm) {
          this.$refs.appraiseForm.resetFields();
        }
      },
      /*关闭导入人才库dialog*/
      closedGorups() {
        this.importForm = {};
        this.$refs.groups.init();
        this.$refs.groups.clearValidate();
        this.$refs.groups.resetForm();
      },
      /*评价人员*/
      rowAppraise(row) {
        this.userId = row.userId;
        if (row.id === "0") {
          this.isCanRate = true;
          this.box = true;
          this.form = {};
        } else {
          //打开评价弹框
          this.isCanRate = false;
          this.rateLoading = true;
          getRateDetail(row.id).then((res) => {
            this.form = res.data.data;
            this.rateLoading = false;
            this.box = true;
          });
        }
      },
      /*提交评价*/
      submit() {
        this.rateLoading = true;
        this.$refs.appraiseForm.validate((valid) => {
          if (valid) {
            submitRate(
              {
                scoreAll: this.form.scoreAll,
                remarks: this.form.remarks,
              },
              this.missionId,
              this.userId
            ).then(
              () => {
                this.box = false;
                this.rateLoading = true;
                this.$message({
                  type: "success",
                  message: "操作成功!",
                });
                this.rateLoading = false;
                this.getList(this.page, this.query);
              },
              (error) => {
                window.console.log(error);
                this.rateLoading = false;
              }
            );
          } else {
            return false;
          }
        });
      },
      /*导入人才库 */
      handleImport() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.importBox = true;
      },
      /*提交导入人才库 */
      submitImport(form, done) {
        var arr = [];
        this.selectionList.forEach((ele) => {
          arr.push({
            userId: ele.userId,
            name: ele.realName,
            idNumber: ele.cardNumber,
            telphone: ele.telphone,
          });
        });
        //导入分组接口
        importTalents(form.groupId, this.worktypeNames, arr).then(
          (res) => {
            this.importBox = false;
            const data=res.data.data;
            this.errorTipList=[];
            if(Object.getOwnPropertyNames(data).length!=0 && data.length!=0){
              for(var i=0;i<data.length;i++){
                this.errorTipList[i]=data[i];
              }
              this.errorDialogVisible=true;
            }
            else{
              this.$message({
              type: "success",
              message: "操作成功!",
            });
            }
            this.getList(this.page, this.query);
            done();
          },
          (error) => {
            window.console.log(error);
            done();
          }
        );
      },
      searchReset() {
        this.query = {};
        this.getList(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.getList(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      /*刷新本页 带搜索参数*/
      refreshChange() {
        this.getList(this.page);
      },
      /*加载数据*/
      getList(page, params = {}) {
        this.loading = true;
        getRateList(
          page.currentPage,
          page.pageSize,
          Object.assign(params, this.query),
          this.missionId
        ).then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      },
    },
  };
</script>
