<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
      <el-form-item label="专业">
        <el-select
          v-model="queryParams.dutyType"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <!-- <el-form-item label="状态">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="(item, k) in status"
            :key="item"
            :label="item"
            :value="k"
          >
          </el-option>
        </el-select>
        </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8" type="flex" justify="end">
      <el-col :span="1.5">
        <el-button
          type="primary"
          @click="handleCheck('add', '新增')"
          v-hasPermi="['postCheck:postAssessmentRules:add']"
          >新增</el-button
        >
      </el-col>
    </el-row>
    <el-table border v-loading="loading" :data="postList" style="height: 50vh">
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column label="专业" align="center" prop="dutyType">
        <template slot-scope="{ row }">
          <span>{{ dutyType(row.dutyType) || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="权重" align="center" prop="fullScore">
        <template slot-scope="{ row }">
          <span>{{ row.fullScore || '--' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="状态" align="center" prop="status">
          <template slot-scope="{ row }">
           <span :style="{color: statusColor[row.status]}">{{ status[row.status] }}</span>
          </template>
        </el-table-column> -->
      <el-table-column label="制定时间" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime || '--' }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleCheck('check', '查看', scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            @click="handleCheck('edit', '修改', scope.row)"
            v-hasPermi="['postCheck:postAssessmentRules:edit']"
            >修改</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="600px"
      @close="cancel"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="100px"
        :disabled="this.type === 'check'"
      >
        <el-form-item label="权重：" prop="postName">
          <el-input
            style="width: 100px"
            v-model="form.fullScore"
            placeholder="请输入"
          />
          %
        </el-form-item>
        <el-form-item label="专业：" filterable prop="status">
          <el-select
            v-model="form.dutyType"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="未通过理由：" prop="status" v-if="textarea2">
          <el-input
            type="textarea"
            :autosize="{ minRows: 6, maxRows: 8 }"
            placeholder="请输入"
            v-model="textarea2"
            disabled
          >
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="this.type !== 'check'">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </div>
    </el-dialog>
  </div>
</template>

  <script>
import {
  listAssessmentRules,
  getAssessmentRules,
  delAssessmentRules,
  addAssessmentRules,
  updateAssessmentRules,
  statusApprove
} from '@/api/postImplementation/functionalDepartment.js'

export default {
  components: {},
  data() {
    return {
      rowId: '',
      type: '',
      options: [
        {
          value: 1,
          label: 'HSE'
        },
        {
          value: 2,
          label: '生产'
        },
        {
          value: 3,
          label: '设备'
        }
      ],
      value1: '',
      value: new Date(),
      textarea2: '',
      active: 0,
      establishDialog: false,
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],

      status: {
        1: '待审核',
        2: '审核中',
        3: '通过',
        4: '驳回'
      },
      statusColor: {
        1: '#cdd152',
        2: '#61dd83',
        3: '#61dd83',
        4: 'red'
      },
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined,
        type: 3
      },
      form: {
        dutyType: '',
        fullScore: '',
        type: 3
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: '岗位名称不能为空', trigger: 'blur' }
        ],
        postCode: [
          { required: true, message: '岗位编码不能为空', trigger: 'blur' }
        ],
        postSort: [
          { required: true, message: '岗位顺序不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    dutyType(type) {
      const find = this.options.find((item) => item.value === type)
      if (find) {
        return find.label
      }
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listAssessmentRules(this.queryParams).then((response) => {
        this.postList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    cancelEstablish() {
      this.establishDialog = false
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form
      this.textarea2 = ''
      this.dateRange = []
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.establishDialog = true
    },
    /** 查看 */
    handleCheck(type, title, row) {
      this.title = title
      this.type = type
      if (type === 'add') {
        this.open = true
        return
      } else {
        this.rowId = row.id
        getAssessmentRules(row.id).then((response) => {
          const { dutyType, fullScore, type, reason } = response.data
          this.form = {
            dutyType,
            fullScore,
            type
          }
          this.textarea2 = reason
          this.open = true
        })
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const postId = row.postId || this.ids
      getPost(postId).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      // if ((this.type === "edit")) {
      //     updateAssessmentRules({...this.form, id: this.rowId}).then((response) => {
      //       this.$modal.msgSuccess("修改成功");
      //       this.open = false;
      //       this.getList();
      //     });
      //   } else {
      //     addAssessmentRules(this.form).then((response) => {
      //       this.$modal.msgSuccess("新增成功");
      //       this.open = false;
      //       this.getList();
      //     });
      //   }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.type === 'edit') {
            updateAssessmentRules({ ...this.form, id: this.rowId }).then(
              (response) => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            )
          } else {
            addAssessmentRules(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function () {
          return delPost(postIds)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },

    handleDownload() {
      // 生成地址
      var myUrl = URL.createObjectURL(this.previewUrl)

      // 下载文件
      this.downloadFile(myUrl, this.filename)
    },
    downloadFile(url, name) {
      const a = document.createElement('a')
      a.setAttribute('href', url)
      a.setAttribute('download', name)
      a.setAttribute('target', '_blank')
      const clickEvent = document.createEvent('MouseEvents')
      clickEvent.initEvent('click', true, true)
      a.dispatchEvent(clickEvent)
    }
  }
}
</script>
  <style lang="scss" scoped>
.resetStyle {
  width: 100%;
  ::v-deep .el-upload {
    width: 100%;
    .el-upload-dragger {
      width: 100%;
    }
  }
}
</style>
