<template>
  <el-row>
    <el-col :span="5">
      <company-tree @node-click="nodeClick"></company-tree>
    </el-col>
    <el-col :span="19">
      <basic-container>
        <avue-crud
          :data="data"
          :table-loading="loading"
          :option="option"
          ref="crud"
          v-model="form"
          :permission="permissionList"
          :before-open="beforeOpen"
          :before-close="beforeClose"
          :page.sync="page"
          @row-update="rowUpdate"
          @row-del="rowDel"
          @search-change="searchChange"
          @search-reset="searchReset"
          @selection-change="selectionChange"
          @current-change="currentChange"
          @size-change="sizeChange"
          @refresh-change="refreshChange"
          @on-load="onLoad"
        >
          <template slot="menuLeft">
            <el-button
              type="success"
              size="small"
              v-if="
                vaildData(permission.manage_company_worktypes_upload, false)
              "
              icon="el-icon-upload2"
              :disabled="!companyId"
              @click="handleImport"
              >批量新增</el-button
            >
            <el-button
              type="danger"
              size="small"
              icon="el-icon-delete"
              v-if="
                vaildData(permission.manage_company_worktypes_delete, false)
              "
              :disabled="!companyId"
              @click="handleDelete"
              >删 除</el-button
            >
          </template>
        </avue-crud>
      </basic-container>
    </el-col>
    <el-dialog
      title="用户数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
        :upload-error="uploadError"
      >
        <template slot="excelTemplate">
          <el-button
            type="primary"
            @click="handleTemplate()"
            :loading="templateLoading"
          >
            点击下载
            <i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </el-row>
</template>

<script>
import { getList, update, excelImport, remove } from "@/api/manage/trade";
import { mapGetters } from "vuex";
import companyTree from "./companyTree";
import { getTemplate } from "@/api/resource/template";
import { excelAccept } from "@/common/accept";

const scoreDic = [
  { label: "-", value: -1 },
  { label: "客服", value: 1 },
  { label: "用户", value: 2 },
];

export default {
  components: { companyTree },
  name: "manage_company_worktypes",
  data() {
    return {
      companyId: null,
      tenanteId: null,
      loading: false,
      templateLoading: false,
      excelBox: false,
      selectionList: [],
      data: [],
      form: {},
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 10,
      },
      option: {
        height: "auto",
        calcHeight: 186,
        tip: false,
        page: true,
        addBtn: false,
        delBtn: true,
        viewBtn: true,
        editBtn: true,
        selection: true,
        searchShow: true,
        searchMenuSpan: 6,
        dialogType: "drawer",
        column: [
          {
            type: "input",
            label: "行业名称",
            prop: "tradeName",
            display: false,
            search: true,
            sortable: true,
          },
          {
            type: "select",
            label: "行业来源",
            span: 12,
            display: false,
            prop: "tradeSource",
            dicData: scoreDic,
          },
          {
            type: "input",
            label: "工种名称",
            span: 12,
            display: false,
            prop: "worktypesName",
            search: true,
            sortable: true,
          },
          {
            type: "select",
            label: "工种来源",
            span: 12,
            display: false,
            prop: "worktypesSource",
            dicData: scoreDic,
          },
          {
            type: "input",
            label: "参保名称",
            span: 12,
            display: false,
            prop: "insuredName",
            sortable: true,
          },
          {
            type: "input",
            label: "保费",
            span: 12,
            display: false,
            prop: "insuredCost",
            sortable: true,
          },
          {
            type: "input",
            label: "技能名称",
            display: false,
            prop: "skillsName",
            search: true,
            sortable: true,
          },
          {
            type: "select",
            label: "技能来源",
            span: 12,
            display: false,
            prop: "skillsSource",
            dicData: scoreDic,
          },
        ],
        group: [
          {
            label: "行业信息",
            column: [
              {
                type: "input",
                label: "行业名称",
                prop: "tradeName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "请输入行业名称",
                    trigger: "blur",
                  },
                ],
                disabled: true,
              },
              {
                type: "input",
                label: "行业别名",
                prop: "tradeAlias",
              },
              {
                type: "textarea",
                label: "行业备注",
                prop: "tradeRemarks",
                span: 24,
              },
              {
                type: "select",
                label: "行业来源",
                span: 12,
                editDisplay: false,
                prop: "tradeSource",
                dicData: scoreDic,
              },
            ],
          },
          {
            label: "工种信息",
            column: [
              {
                type: "input",
                label: "工种名称",
                span: 12,
                size: "small",
                prop: "worktypesName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "请输入工种名称",
                    trigger: "blur",
                  },
                ],
                placeholder: "请输入工种名称",
                disabled: true,
              },
              {
                type: "input",
                label: "工种别名",
                prop: "worktypesAlias",
              },
              {
                type: "input",
                label: "参保名称",
                span: 12,
                size: "small",
                prop: "insuredName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "请输入参保名称",
                    trigger: "blur",
                  },
                ],
                placeholder: "请输入参保名称",
              },
              {
                type: "input",
                label: "保费",
                span: 12,
                size: "small",
                prop: "insuredCost",
                rules: [],
                placeholder: "请输入保费",
              },
              {
                type: "textarea",
                label: "工种备注",
                prop: "worktypesRemarks",
                span: 24,
              },
              {
                type: "select",
                label: "工种来源",
                span: 12,
                editDisplay: false,
                prop: "worktypesSource",
                dicData: scoreDic,
              },
            ],
          },
          {
            label: "技能信息",
            column: [
              {
                type: "input",
                label: "技能名称",
                prop: "skillsName",
                rules: [
                  {
                    required: true,
                    whitespace: true,
                    message: "请输入参保名称",
                    trigger: "blur",
                  },
                ],
                disabled: true,
              },
              {
                type: "input",
                label: "技能别名",
                prop: "skillsAlias",
              },
              {
                type: "textarea",
                label: "技能备注",
                prop: "skillsRemarks",
                span: 24,
              },
              {
                type: "select",
                label: "技能来源",
                span: 12,
                editDisplay: false,
                prop: "skillsSource",
                dicData: scoreDic,
              },
            ],
          },
        ],
      },
      excelForm: {},
    };
  },
  created() {},
  mounted() {},
  methods: {
    nodeClick({ data }) {
      this.companyId = data.id;
      this.tenantId = data.tenantId;
      this.page.currentPage = 1;
      this.$refs.crud.searchReset();
      this.refreshChange();
    },
    rowUpdate(row, done, loading) {
      update(
        {
          id: row.tradeId,
          alias: row.tradeAlias,
          remarks: row.tradeRemarks,
        },
        {
          id: row.worktypesId,
          alias: row.worktypesAlias,
          insuredName: row.insuredName,
          insuredCost: row.insuredCost,
          remarks: row.worktypesRemarks,
        },
        {
          id: row.skillsId,
          alias: row.skillsAlias,
          remarks: row.skillsRemarks,
        }
      )
        .then(() => {
          this.onLoad(this.page);
          this.refreshChange();
          done();
        })
        .catch(() => {
          loading();
        });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.skillsId);
        })
        .then(() => {
          this.onLoad(this.page, this.query);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page, this.query);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    beforeOpen(done, type) {
      if (["add", "edit"].includes(type)) {
        done();
      }
      if (["edit", "view"].includes(type)) {
        done();
      }
    },
    beforeClose(done) {
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      if (this.companyId) {
        this.loading = true;
        getList(
          page.currentPage,
          page.pageSize,
          Object.assign(this.query, params),
          this.companyId
        ).then((res) => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        });
      }
    },
    handleImport() {
      this.excelBox = true;
    },
    handleDelete() {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids.join(","));
        })
        .then(() => {
          this.onLoad(this.page, this.query);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    uploadAfter(res, done) {
      if (!(res instanceof Error)) {
        this.excelBox = false;
      }
      this.refreshChange();
      this.onLoad(this.page, this.query);
      done();
    },
    uploadError(error) {
      if (error) {
        this.$message.error(error);
      }
    },
    handleTemplate() {
      this.templateLoading = true;
      getTemplate("hy")
        .then((rep) => {
          this.templateLoading = false;
          window.open(rep.data.data);
        })
        .catch(() => {
          this.templateLoading = false;
        });
    },
  },
  computed: {
    ...mapGetters(["userInfo", "permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permission.manage_company_talentsList_add,
          false
        ),
        viewBtn: this.vaildData(
          this.permission.manage_company_talentsList_view,
          false
        ),
        editBtn: this.vaildData(
          this.permission.manage_company_talentsList_edit,
          false
        ),
        delBtn: this.vaildData(
          this.permission.manage_company_talentsList_delete,
          false
        ),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.skillsId);
      });
      return ids;
    },
    excelOption() {
      return {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "文件上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "文件上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            accept: excelAccept,
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: excelImport(
              this.companyId,
              this.tenantId,
              this.excelForm.isCovered
            ),
          },
          {
            label: "数据覆盖",
            prop: "isCovered",
            type: "switch",
            align: "center",
            width: 80,
            dicData: [
              {
                label: "否",
                value: 0,
              },
              {
                label: "是",
                value: 1,
              },
            ],
            value: 0,
            slot: true,
            rules: [
              {
                required: true,
                message: "请选择是否覆盖",
                trigger: "blur",
              },
            ],
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      };
    },
  },
};
</script>

<style>
</style>