<template>
  <div class="app-container">
    <div class="filter-container">

      <el-input size="mini" v-model="search.keyWord" placeholder="关键字" style="width: 200px;" class="filter-item"/>
      <el-button size="mini" class="filter-item" type="primary" icon="el-icon-search" @click="searchData">
        搜索
      </el-button>

      <el-button
        v-if="webType && isChoose == false"
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-plus"
        size="mini"
        @click="handleCreateAdd">
        增加班组
      </el-button>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="getList"/>
        </el-tooltip>
      </div>
    </div>
    <el-table
      :data="tableData"
      v-loading="listLoading"
      style="width: 100%;margin-bottom: 20px;"
      size="mini"
      row-key="id"
      border
      default-expand-all>
      <!--      <el-table-column label="字典编码" align="center" prop="dictCode" />-->
      <el-table-column label="班组类型" align="center" prop="teamKind">
        <template slot-scope="{row}">
          <span v-if="row.teamKind == 0">业务</span>
          <span v-else-if="row.teamKind == 1">修图</span>
          <span v-else-if="row.teamKind == 2">机台班组</span>
          <span v-else-if="row.teamKind == 3">覆膜班组</span>
          <span v-else-if="row.teamKind == 4">模切班组</span>
          <span v-else-if="row.teamKind == 5">穿绳班组</span>
          <span v-else-if="row.teamKind == 6">拼版班组</span>
        </template>
      </el-table-column>
      <el-table-column label="班组名称" align="center" prop="teamName"/>
      <el-table-column label="负责人" align="center" prop="managerUserName">
      </el-table-column>
      <el-table-column align="center" label="状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag v-if="row.voidFlag==0" type="success">
            正常
          </el-tag>
          <el-tag v-else-if="row.voidFlag==1" type="danger">
            禁用
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="left" align="center" class-name="small-padding fixed-width" width="260">
        <template slot-scope="{row}">
          <template v-if="webType">
            <template v-if="!isChoose">
              <el-button @click="handleUpdate(row)" type="primary" size="mini">
                编辑
              </el-button>
              <el-button
                size="mini"
                @click="$router.push({path:'/baseInfo/workTeamPerson/'+row.id ,query:{idTeam:row.id,teamName:row.teamName}})">
                班组人员表
              </el-button>
              <el-button @click="editStatus(row,'1')" type="danger" v-if="row.voidFlag==0" size="mini">
                禁用
              </el-button>
              <el-button @click="editStatus(row,'0')" v-else-if="row.voidFlag==1" size="mini">
                启用
              </el-button>
            </template>
            <template v-else>
              <el-button @click="sourceType == 1? chooseThisTo(row):chooseThis(row)" type="primary" size="mini">
                选择
              </el-button>
            </template>
          </template>
          <template v-else>
            <el-button @click="ERPChoose(row)" type="primary" size="mini">
              选择
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="listData.total>0" :total="listData.total" :page.sync="listData.page"
                :limit.sync="listData.pageSize" @pagination="getList"/>

    <el-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogVisible" width="600px">
      <el-form label-width="130px" label-position="right">
        <el-form-item label="班组类型">
          <el-select v-model="form.teamKind">
            <el-option
              v-for="(item,index) in options"
              :key="index"
              :label="item.label"
              :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="班组名称" prop="dictLabel">
          <el-input v-model="form.teamName" placeholder="请输入班组名称"/>
        </el-form-item>

        <el-form-item label="负责人" prop="dictLabel">
          <el-button @click="openChoose" plain type="primary">
            {{ form.managerUserName ? form.managerUserName : '选择负责人' }}
          </el-button>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogStatus==='1'?addBtn():editBtn()">确 定</el-button>
      </div>
    </el-dialog>
    <cancledialog @tableRefresh="tableRefresh" ref="confirmDialog"></cancledialog>
    <el-dialog :close-on-click-modal="false" title="选择负责人" :visible.sync="userInfoVisible" width="70%">
      <user :is-choose="true" @chooseUser='chooseUser'></user>
    </el-dialog>
  </div>
</template>

<script>
import {getWorkTeamList, addWorkTeam, updateWorkTeam, getWorkTeamInfo} from '@/api/baseInfo/workTeam/workTeam'
import Cancledialog from '@/components/Dialog/Cancledialog.vue'
import Pagination from '@/components/Pagination'
import User from '@/views/system/user/user'

const form = {
  teamKind: 0,
  teamName: undefined,
  idManagerUser: undefined,
  managerUserName: undefined
}

export default {
  name: 'workTeam',
  components: {
    User,
    Cancledialog, Pagination
  },
  props: {
    isChoose: {
      type: Boolean,
      default: false
    },
    /**
     * 调用来源
     * 0 普通调用
     * 1 查询统计调用
     */
    sourceType: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      webType: true,
      tempRoute: {},
      dialogStatus: 1, // 1增加，2修改
      dialogTitle: '',
      dialogVisible: false,

      innerVisible: false,

      search: {
        keyWord: ''
      },
      form: Object.assign({}, form),

      confirmdialog: false,
      listLoading: false,
      tableData: [],

      listData: {
        total: 0,
        page: 1,
        pageSize: 10
      },
      options: [
        {
          label: '业务',
          value: 0
        },
        {
          label: '修图',
          value: 1
        },
        {
          label: '机台班组',
          value: 2
        },
        {
          label: '覆膜班组',
          value: 3
        },
        {
          label: '模切班组',
          value: 4
        },
        {
          label: '穿绳班组',
          value: 5
        },
        {
          label: '拼版班组',
          value: 6
        }
      ],
      userInfoVisible: false,

    }
  },
  mounted() {
    this.getList()
    if (this.$route.query.type === undefined) {
      this.webType = true
    } else {
      this.webType = false
    }
  },
  methods: {
    // 获取列表
    getList() {
      const _that = this
      _that.listLoading = true
      const params = _that.search
      const data = {
        'params': JSON.stringify(params)
      }
      // console.log(data);
      getWorkTeamList(data).then(response => {
        console.log(response)
        const data = response.data
        // //  组合表单数据
        _that.tableData = data
        // console.log(_that.tableData)
        _that.listData.total = response.total
        _that.listLoading = false
      }).catch(err => {
        _that.$message.error(err)
        _that.listLoading = false
      })
    },

    openChoose() {
      var _that = this
      _that.userInfoVisible = true
    },
    chooseUser(row) {
      console.log(row)
      var _that = this
      _that.userInfoVisible = false
      _that.form.idManagerUser = row.id
      _that.form.managerUserName = row.userName
    },

    // 增加班组
    handleCreateAdd() {
      const _that = this
      _that.form = Object.assign({}, form)

      _that.form.idType = _that.typeId
      _that.dialogTitle = '增加班组'
      _that.dialogStatus = '1'
      _that.dialogVisible = true
    },
    // 增加档案
    addBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const data = {
        'workTeam': info
      }
      // console.log(data);
      // return

      addWorkTeam(data).then(res => {
        // console.log(res);
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '增加成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 唤起修改
    handleUpdate(row) {
      const _that = this

      _that.dialogTitle = '修改班组'
      _that.dialogVisible = true
      _that.dialogStatus = '2'
      const idPrint = row.id
      const data = {
        idTeam: row.id
      }
      // console.log(data);
      getWorkTeamInfo(data).then(res => {
        console.log(res)
        const data = res.data

        _that.form = {
          id: data.id,
          teamKind: data.teamKind,
          teamName: data.teamName,
          idManagerUser: data.idManagerUser,
          managerUserName: data.managerUserName
        }
      }).catch(err => {
        _that.$message.error(err)
      })
    },
    // 修改档案
    editBtn() {
      const _that = this
      const info = JSON.stringify(_that.form)
      const workTeam = {
        'workTeam': info
      }
      // console.log(data);
      // return;

      updateWorkTeam(workTeam).then(res => {
        console.log(res)
        if (res.httpCode == 200) {
          _that.dialogVisible = false
          _that.$message({
            message: '修改成功',
            type: 'success'
          })
          _that.tableRefresh()
        } else {
          _that.$message.error(res.msg)
        }
      }).catch(err => {
        _that.$message.error(err)
        _that.dialogVisible = true
      })
    },

    // 刷新变革
    tableRefresh() {
      // 刷新变革
      this.getList()
    },

    // 启用禁用
    editStatus(row, status) {
      const _that = this
      const printerTypeInfo = {
        id: row.id,
        voidFlag: status
      }
      const params = {
        'workTeam': JSON.stringify(printerTypeInfo)
      }

      const url = '/doc/voidWorkTeam'
      _that.$refs.confirmDialog.updataClick(params, url)
    },

    // 搜索
    searchData() {
      this.listData.page = 1
      this.getList()
    },

    // 弹窗内选择操作
    chooseThis(row) {
      // console.log('点击选择');
      this.$emit('choosePaper', row)
    },

    // 查询统计选择
    chooseThisTo(row) {
      var data = {
        id: row.id,
        name: row.teamName
      }
      this.$emit('chooseRow', data)
    },
    ERPChoose(row) {
      // this.$message.error(row)
      dyERP.sendJson(JSON.stringify(row))
    }

  }
}
</script>

<style lang="scss" scoped>
.filter-container {
  margin-bottom: 15px;
}

.tableItemInput {
  width: 30%;
}
</style>
