<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="班组" prop="teamCode">
        <el-select v-model="queryParams.teamCode" placeholder="请选择班组" clearable filterable>
          <el-option v-for="item in teamOptions" :key="item.teamCode" :label="item.teamName" :value="item.teamCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="责任产线" prop="lineCode">
        <el-select v-model="queryParams.lineCode" placeholder="请选择产线" clearable filterable>
          <el-option v-for="item in lineOptions" :key="item.productLineCode" :label="item.productLineName" :value="item.productLineCode" />
        </el-select>
      </el-form-item>
      <el-form-item label="班次" prop="shiftCode">
        <el-select v-model="queryParams.shiftCode" placeholder="请选择班次" clearable>
          <el-option v-for="item in shiftOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="排班日期">
        <el-date-picker v-model="daterangeShiftDate" type="daterange" value-format="yyyy-MM-dd" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 240px" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </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 v-hasPermi="['production:teamShift:add']" type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['production:teamShift:edit']" type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['production:teamShift:remove']" type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermi="['production:teamShift:export']" type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="teamShiftList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="班组" align="center" prop="teamName" />
      <el-table-column label="排班日期" align="center" prop="shiftDate" width="120" />
      <el-table-column label="班次" align="center" prop="shiftCode">
        <template slot-scope="scope">{{ getShiftLabel(scope.row.shiftCode) }}</template>
      </el-table-column>
      <el-table-column label="负责人" align="center" prop="leaderUserName" />
      <el-table-column label="责任产线" align="center" prop="lineName" />
      <el-table-column label="责任区域" align="center" prop="areaDesc" show-overflow-tooltip />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="scope.row.status === '0' ? 'success' : 'info'">{{ scope.row.status === '0' ? '启用' : '停用' }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" show-overflow-tooltip />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['production:teamShift:edit']" size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button v-hasPermi="['production:teamShift:remove']" 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="680px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="班组" prop="teamCode">
              <el-select v-model="form.teamCode" placeholder="请选择班组" filterable style="width: 100%">
                <el-option v-for="item in teamOptions" :key="item.teamCode" :label="item.teamName" :value="item.teamCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="班次" prop="shiftCode">
              <el-select v-model="form.shiftCode" placeholder="请选择班次" style="width: 100%">
                <el-option v-for="item in shiftOptions" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="排班日期" prop="shiftDate">
              <el-date-picker v-model="form.shiftDate" value-format="yyyy-MM-dd" type="date" placeholder="请选择日期" style="width: 100%" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="leaderUserId">
              <el-select v-model="form.leaderUserId" placeholder="请选择负责人" clearable filterable style="width: 100%">
                <el-option v-for="item in userOptions" :key="item.userId" :label="item.nickName || item.userName" :value="item.userId" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="16">
          <el-col :span="12">
            <el-form-item label="责任产线" prop="lineCode">
              <el-select v-model="form.lineCode" placeholder="请选择产线" clearable filterable style="width: 100%">
                <el-option v-for="item in lineOptions" :key="item.productLineCode" :label="item.productLineName" :value="item.productLineCode" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-radio-group v-model="form.status">
                <el-radio v-for="item in statusOptions" :key="item.value" :label="item.value">{{ item.label }}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="责任区域" prop="areaDesc">
          <el-input v-model="form.areaDesc" placeholder="请输入责任区域" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :rows="3" placeholder="请输入备注" />
        </el-form-item>
      </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 { listTeamShift, getTeamShift, addTeamShift, updateTeamShift, delTeamShift } from '@/api/production/teamShift'
import { getTeamMemberList } from '@/api/base/teamMembers'
import { findProductLineList } from '@/api/base/productLine'
import { selectUserList } from '@/api/system/user'

export default {
  name: 'TeamShift',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      teamShiftList: [],
      teamOptions: [],
      lineOptions: [],
      userOptions: [],
      daterangeShiftDate: [],
      ids: [],
      single: true,
      multiple: true,
      open: false,
      title: '',
      shiftOptions: [
        { label: '白班', value: 'DAY' },
        { label: '夜班', value: 'NIGHT' },
        { label: '中班', value: 'MIDDLE' }
      ],
      statusOptions: [
        { label: '启用', value: '0' },
        { label: '停用', value: '1' }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        teamCode: undefined,
        lineCode: undefined,
        shiftCode: undefined,
        status: undefined,
        params: {}
      },
      form: {},
      rules: {
        teamCode: [{ required: true, message: '班组不能为空', trigger: 'change' }],
        shiftDate: [{ required: true, message: '排班日期不能为空', trigger: 'change' }],
        shiftCode: [{ required: true, message: '班次不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.loadOptions()
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.queryParams.params = {}
      if (this.daterangeShiftDate && this.daterangeShiftDate.length === 2) {
        this.queryParams.params.beginShiftDate = this.daterangeShiftDate[0]
        this.queryParams.params.endShiftDate = this.daterangeShiftDate[1]
      }
      listTeamShift(this.queryParams).then(response => {
        this.teamShiftList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    loadOptions() {
      getTeamMemberList({}).then(response => {
        this.teamOptions = response.data || response.rows || []
      })
      findProductLineList({ productLineType: 1 }).then(response => {
        this.lineOptions = response.data || response.rows || []
      })
      selectUserList({}).then(response => {
        this.userOptions = response.rows || response.data || []
      })
    },
    getShiftLabel(value) {
      const match = this.shiftOptions.find(item => item.value === value)
      return match ? match.label : value
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        objId: undefined,
        teamCode: undefined,
        shiftDate: undefined,
        shiftCode: 'DAY',
        leaderUserId: undefined,
        lineCode: undefined,
        areaDesc: undefined,
        status: '0',
        remark: undefined
      }
      this.resetForm('form')
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.daterangeShiftDate = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.objId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增班组排班维护'
    },
    handleUpdate(row) {
      const objId = row.objId || this.ids[0]
      this.reset()
      getTeamShift(objId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改班组排班维护'
      })
    },
    handleDelete(row) {
      const objIds = row.objId || this.ids
      this.$modal.confirm('是否确认删除选中的班组排班数据？').then(() => {
        return delTeamShift(objIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      })
    },
    handleExport() {
      this.download('/production/teamShift/export', { ...this.queryParams }, `team_shift_${new Date().getTime()}.xlsx`)
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        const request = this.form.objId ? updateTeamShift(this.form) : addTeamShift(this.form)
        request.then(() => {
          this.$modal.msgSuccess(this.form.objId ? '修改成功' : '新增成功')
          this.open = false
          this.getList()
        })
      })
    }
  }
}
</script>
