<template>
  <el-card class="setting-card" shadow="never">
    <div slot="header" class="card-header">
      <span>额外假期设置</span>
      <el-button type="primary" size="mini" icon="el-icon-plus" @click="addRow">新增额外假期</el-button>
    </div>
    <NCC-table :data="value" border size="mini" height="320">
      <el-table-column label="员工" min-width="170" align="left">
        <template slot-scope="scope">
          <user-select
            v-model="scope.row.userId"
            placeholder="请选择员工"
            @change="(id, users) => handleUserChange(scope.row, id, users)"
          />
        </template>
      </el-table-column>
      <el-table-column label="假期名称" min-width="160" align="left">
        <template slot-scope="scope">
          <el-input v-model="scope.row.leaveName" placeholder="如：旅游奖励假" />
        </template>
      </el-table-column>
      <el-table-column label="归属年份" width="120" align="left">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.grantYear"
            :min="2000"
            :max="2100"
            controls-position="right"
            style="width: 100%"
          />
        </template>
      </el-table-column>
      <el-table-column label="额外天数" width="120" align="left">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.extraDays"
            :min="0.01"
            :step="0.5"
            :precision="2"
            controls-position="right"
            style="width: 100%"
          />
        </template>
      </el-table-column>
      <el-table-column label="是否启用" width="100" align="left">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.isEnabled" :active-value="1" :inactive-value="0" />
        </template>
      </el-table-column>
      <el-table-column label="备注" min-width="220" align="left">
        <template slot-scope="scope">
          <el-input v-model="scope.row.remark" placeholder="无" />
        </template>
      </el-table-column>
      <el-table-column label="操作" width="110" align="left">
        <template slot-scope="scope">
          <el-popconfirm
            title="确认删除当前额外假期配置吗？"
            confirm-button-text="确认删除"
            cancel-button-text="取消"
            @confirm="removeRow(scope.$index)"
          >
            <el-button slot="reference" type="text" class="danger-text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </NCC-table>
    <el-empty v-if="!value.length" :image-size="56" description="暂无额外假期配置" />
  </el-card>
</template>

<script>
import userSelect from '@/components/NCC-userSelect'

export default {
  name: 'ExtraLeaveTable',
  components: { userSelect },
  props: {
    value: {
      type: Array,
      default: () => []
    }
  },
  methods: {
    addRow() {
      this.$emit('input', [
        ...this.value,
        {
          userId: '',
          userName: '',
          leaveName: '',
          grantYear: new Date().getFullYear(),
          extraDays: 1,
          isEnabled: 1,
          remark: ''
        }
      ])
    },
    removeRow(index) {
      const list = this.value.slice()
      list.splice(index, 1)
      this.$emit('input', list)
    },
    handleUserChange(row, id, users) {
      row.userId = id
      row.userName = users && users[0] ? users[0].fullName : ''
    }
  }
}
</script>

<style lang="scss" scoped>
.setting-card {
  border-radius: 12px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
}

.danger-text {
  color: #f56c6c;
}

::v-deep .el-card__header {
  padding: 12px 16px;
}

::v-deep .el-card__body {
  padding: 12px 14px;
}
</style>
