<template>
  <div class="app-container">
    <el-card style="width: 91%">
      <el-row style="margin-top: 10px; margin-bottom: 10px">
        <span>机床编号:</span>
        <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="searchTable()">
          <el-option
            v-for="item in machineNumber"
            :key="item.value"
            :label="item.label"
            :value="item.value">
            <div style="float: left">{{ item.label }}</div>
            <div style="float: right; color: #8492a6; font-size: 13px">{{ item.name }}</div>
          </el-option>
        </el-select>
        <span>机床数量：{{ machineNum }}</span>
        <el-button size="small" icon="el-icon-circle-plus-outline" type="distribution" style="margin-left: 300px" @click="addTaskName()">增加装配任务</el-button>
      </el-row>
    </el-card>
    <el-card style="width: 52%; float: left">
      <el-table v-loading="loading" :data="tableData" :row-class-name="tableData1ClassName" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="720px">
        <el-table-column label="机床组号" align="center" width="120px">
          <template slot-scope="scope">
            {{ scope.row.机床组号 }}
          </template>
        </el-table-column>
        <el-table-column label="部件名称" align="center" width="90px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.部件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="部件数" align="center" width="65px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.部件数 }}
          </template>
        </el-table-column>
        <el-table-column label="总数量" align="center" width="65px">
          <template slot-scope="scope">
            {{ scope.row.总数量 }}
          </template>
        </el-table-column>
        <el-table-column label="自制件" align="center" width="65px">
          <template slot-scope="scope">
            {{ scope.row.自制件入库率 }}
          </template>
        </el-table-column>
        <el-table-column label="外购件" align="center" width="65px">
          <template slot-scope="scope">
            {{ scope.row.外购件入库率 }}
          </template>
        </el-table-column>
        <el-table-column label="成组配套" align="center" width="80px">
          <template slot-scope="scope">
            <el-button :disabled="!scope.row.组件流水号" type="text" size="mini" @click="openDialog(scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
        <el-table-column label="装配工" align="center" width="125px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.装配工 }}
          </template>
        </el-table-column>
        <el-table-column label="分配时间" align="center" width="85px">
          <template slot-scope="scope">
            {{ scope.row.分配时间 ? scope.row.分配时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="80px">
          <template slot-scope="scope">
            <el-button :disabled="Number(scope.row.是否工作) !== 0" type="text" size="mini" @click="openPeople(scope.row)">分配</el-button>
            <el-button :disabled="Number(scope.row.是否工作) !== 0" type="text" icon="el-icon-delete" size="mini" @click="deleteData(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card style="width: 39%; float: left">
      <el-table :data="tableData2" :row-class-name="tableData1ClassName" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="720px">
        <el-table-column label="状态" align="center" width="60px">
          <template slot-scope="scope">
            {{ scope.row.状态 }}
          </template>
        </el-table-column>
        <el-table-column label="部件名称" align="center" width="110px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.组件名称 }}
          </template>
        </el-table-column>
        <el-table-column label="装配工" align="center" width="120px" show-overflow-tooltip>
          <template slot-scope="scope">
            {{ scope.row.装配工 }}
          </template>
        </el-table-column>
        <el-table-column label="装配开始" align="center" width="85px">
          <template slot-scope="scope">
            {{ scope.row.开始时间 ? scope.row.开始时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="装配暂停" align="center" width="85px">
          <template slot-scope="scope">
            {{ scope.row.暂停开始时间 ? scope.row.暂停开始时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="装配结束" align="center" width="85px">
          <template slot-scope="scope">
            {{ scope.row.结束时间 ? scope.row.结束时间.split(' ')[0] : '' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="80px">
          <template slot-scope="scope">
            <el-button :disabled="Number(scope.row.是否工作) !== 0" type="text" size="mini" @click="outPeople(scope.row)">移出</el-button>
            <el-button :disabled="Number(scope.row.是否工作) !== 0" type="text" icon="el-icon-delete" size="mini" @click="deleteTask(scope.row)"/>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogFormVisible"
      title="零件完成情况"
      width="40%">
      <div style="margin: 10px 0 0 20px;height: 650px;overflow: auto">
        <el-tree
          :data="departmentData"
          :indent="50"
          :expand-on-click-node="false"
          :render-content="renderContent"
          node-key="机床流水号"
          class="selectTree"/>
      </div>
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogFormVisible1"
      title="装配任务"
      center
      width="300px">
      <el-form ref="form" :model="form" :rules="rules" label-position="right" label-width="80px">
        <el-form-item label="任务名称" prop="任务名称">
          <el-input v-model="form.任务名称" size="small" style="width: 150px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          :disabled="addTaskName_disable"
          type="primary"
          size="small"
          icon="el-icon-check"
          @click="submitTaskName">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogFormVisible2"
      title="分配装配任务"
      center
      width="340px">
      <el-table
        ref="multipleTable"
        :data="List"
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        height="500"
        highlight-current-row
        border
        size="mini"
        @selection-change="handleSelectionChange">
        <el-table-column
          :selectable="selectable"
          align="center"
          type="selection"
          width="50"/>
        <el-table-column align="center" label="装配工" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button
          :disabled="addPeople_disable"
          type="primary"
          size="small"
          icon="el-icon-check"
          @click="submitPeople">确定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-el-drag-dialog
      :visible.sync="dialogFormVisible3"
      title="移出装配工"
      center
      width="340px">
      <el-table
        ref="multipleTable"
        :data="List1"
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        highlight-current-row
        border
        size="mini"
        @selection-change="handleSelectionChange1">
        <el-table-column
          align="center"
          type="selection"
          width="50"/>
        <el-table-column align="center" label="装配工" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.姓名 }}
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button
          :disabled="outPeople_disable"
          type="primary"
          size="small"
          icon="el-icon-check"
          @click="submitOutPeople">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>import { initProject, searchTable, getMachineNames, selectParts, addData, deletedata, searchPeople, submitPeople, searchTable2, searchName, submitOutPeople, deleteTask } from '@/api/Assembly/AssemblyTaskAssignment'
// import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      machineNum: '',
      dialogFormVisible: false,
      dialogFormVisible1: false,
      dialogFormVisible2: false,
      dialogFormVisible3: false,
      groupNumberValue: '',
      loading: false,
      tableData: [],
      tableData2: [],
      hadFile: [],
      option: [],
      plan: '',
      List: [],
      List1: [],
      Name: [],
      peopleGroup: [],
      outPeopleGroup: [],
      multipleSelection: [],
      multipleSelection1: [],
      addTaskName_disable: false,
      addPeople_disable: false,
      outPeople_disable: false,
      form: {
        任务名称: ''
      },
      rules: {
        任务名称: [{ required: true, message: '请填写任务名称' }]
      },
      departmentData: [] // 树状数据
    }
  },
  created() {
    this.searchPeople()
  },
  mounted() {
    this.initProject()
  },
  methods: {
    initProject() {
      initProject().then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.machineNumber.push({
            label: response.data[i].机床图号,
            name: response.data[i].项目名称,
            value: response.data[i].机床流水号
          })
        }
      })
    },
    searchPeople() {
      searchPeople().then(response => {
        this.List = response.data
      })
    },
    searchTable() {
      this.loading = true
      getMachineNames(this.machineNumberValue).then(response => {
        this.machineNum = response.data[0].机床数量
      })
      searchTable2(this.machineNumberValue).then(response => {
        this.tableData2 = response.data
      })
      searchTable(this.machineNumberValue).then(response => {
        this.tableData = response.data
      }).then(() => {
        this.loading = false
      })
    },
    renderContent(h, { node, data, store }) {
      console.log(node, 11111)
      if (node.data.零件类型 === 1) { // 自制
        if (parseInt(node.data.考核状态) > 6) {
          return (<span class='treeNotesComplate'>{node.label}</span>)
        } else {
          return (<span class='treeNotesUnComplate'>{node.label}</span>)
        }
      } else { // 外购
        if (node.data.采购状态 === '已完成') {
          return (<span class='treeNotesComplate'>{node.label}</span>)
        } else {
          return (<span class='treeNotesUnComplate'>{node.label}</span>)
        }
      }
    },
    openDialog(row) {
      this.dialogFormVisible = true
      this.departmentData = []
      selectParts(1, row.组件流水号, 0, 0).then(response => {
        for (let j = 0; j < response.data.length; j++) {
          this.departmentData.push({
            label: (response.data[j].零件图号 ? response.data[j].零件图号 : '无零件图号') + '\xa0\xa0\xa0\xa0\xa0\xa0' + (response.data[j].规格 ? response.data[j].规格 : '') + '\xa0\xa0\xa0\xa0\xa0\xa0' + (response.data[j].投产总数量 ? response.data[j].投产总数量 : '') + '..........' + (response.data[j].零件名称 ? response.data[j].零件名称 : '无零件名称') + (response.data[j].零件类型 === 1 ? '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0自制：' + response.data[j].考核状态名称 : (response.data[j].零件类型 === 2 ? '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0制造采购' : '\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0\xa0外购：' + response.data[j].采购状态)),
            value: response.data[j].工艺计划流水号,
            考核状态: response.data[j].考核状态,
            采购状态: response.data[j].采购状态,
            零件类型: response.data[j].零件类型
          })
        }
      })
    },
    openPeople(row) {
      this.Name = []
      searchName(row.计划流水号).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.Name.push(Number(response.data[i].考勤编号))
        }
      })
      this.searchPeople()
      this.addPeople_disable = false
      this.dialogFormVisible2 = true
      this.groupNumberValue = row.计划流水号
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    selectable(row, index) { // 控制某行是否可选
      console.log(this.Name.indexOf(Number(row.考勤编号)))
      return this.Name.indexOf(Number(row.考勤编号)) === -1 // 未采购&&确认物料修改
    },
    submitPeople() {
      if (this.multipleSelection.length === 0) {
        this.$message.warning('请选择装配工')
      } else {
        this.peopleGroup = []
        this.addPeople_disable = true
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.peopleGroup.push(this.multipleSelection[i].人员编号)
        }
        submitPeople(this.groupNumberValue, this.peopleGroup).then(response => {
          if (response.data[0].result !== '0') {
            this.$message.success('分配成功')
            this.dialogFormVisible2 = false
            this.multipleSelection = []
            this.searchTable()
          } else {
            this.$message.error('分配失败')
            this.addPeople_disable = false
          }
        })
      }
    },
    outPeople(row) {
      this.outPeople_disable = false
      this.dialogFormVisible3 = true
      this.groupNumberValue = row.计划流水号
      searchName(this.groupNumberValue).then(response => {
        this.List1 = response.data
      })
    },
    handleSelectionChange1(val) {
      this.multipleSelection1 = val
    },
    submitOutPeople() {
      if (this.multipleSelection1.length === 0) {
        this.$message.warning('请选择装配工')
      } else {
        this.outPeopleGroup = []
        this.outPeople_disable = true
        for (let i = 0; i < this.multipleSelection1.length; i++) {
          this.outPeopleGroup.push(this.multipleSelection1[i].任务分配流水号)
        }
        submitOutPeople(this.groupNumberValue, this.outPeopleGroup).then(response => {
          if (response.data[0].result !== '0') {
            this.$message.success('移出成功')
            this.dialogFormVisible3 = false
            this.searchTable()
            this.multipleSelection1 = []
          } else {
            this.$message.error('移出失败')
            this.outPeople_disable = false
          }
        })
      }
    },
    addTaskName() {
      if (this.$refs['form'] !== undefined) {
        this.$refs['form'].resetFields()
      }
      this.form.任务名称 = ''
      this.addTaskName_disable = false
      this.dialogFormVisible1 = true
    },
    submitTaskName() {
      if (this.machineNumberValue) {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.addTaskName_disable = true
            addData(this.machineNumberValue, this.form.任务名称, 1).then(response => {
              if (response.data[0].result === '1') {
                this.$message.success('新建装配任务成功')
                this.searchTable()
                this.dialogFormVisible1 = false
              } else {
                this.$message.error('新建装配任务失败')
                this.addTaskName_disable = false
              }
            })
          } else {
            return false
          }
        })
      } else {
        this.$message.warning('请先选择机床')
      }
    },
    deleteData(row) {
      this.$confirm('确认删除该装配任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletedata(row.计划流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    deleteTask(row) {
      this.$confirm('确认删除该分配任务', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteTask(row.计划流水号).then(response => {
          if (response.data[0].result === '1') {
            this.$message.success('删除成功')
            this.searchTable()
          } else { this.$message.error('删除失败') }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    tableData1ClassName({ row, rowIndex }) {
      if (Number(row.是否工作) === 0 && row.分配时间) {
        return 'weizhuangpei'
      } else if (Number(row.是否工作) === 1 && Number(row.是否暂停) === 0) {
        return 'zhuangpeizhong'
      } else if (Number(row.是否工作) === 1 && Number(row.是否暂停) === 1) {
        return 'zanting'
      } else if (Number(row.是否工作) === 2) {
        return 'wancheng'
      }
    }
  }
}

</script>

<style>
  .el-table .cjzuwcqk_complet {
    background: #9bd7fc !important;
  }
</style>
<style lang="scss">
  .selectTree{
    .el-tree-node__content>.el-tree-node__expand-icon{
      display: inline !important;
    }
    .el-tree-node:focus > .el-tree-node__content {
      background-color: #ccc !important;
    }
  }
  .treeNotesComplate {
    color: black;
    background-color: #9bd7fc !important;
  }
  .treeNotesUnComplate {
    color: black;
  }
</style>
<style scoped>
  .el-form--label-left >>> .el-form-item__label {
    text-align: right;
  }
  >>>.el-dialog--center .el-dialog__body {
    text-align: initial;
    padding: 20px 30px 20px
  }
  >>>.el-dialog__header {
    padding: 20px 20px 0px;
  }
  >>>.el-dialog__footer {
    padding: 0px 20px 20px;
    /*text-align: right;*/
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
  }
  >>>.el-form-item {
    margin-bottom: 13px;
  }
</style>
<style>
  .weizhuangpei {
    background: #e9c5d1 !important;
  }
  .zhuangpeizhong {
    background: #E6EAEE !important;
  }
  .zanting {
    background: #D8E5F6 !important;
  }
  .wancheng {
    background: #c6d7b3 !important;
  }
</style>
