<template>
  <div class="app-container">
    <el-card>
      <el-row style="margin-top: 10px; margin-bottom: 10px">
        <span>机床编号:</span>
        <el-select v-model="machineNumberValue" filterable placeholder="机床编号" size="small" clearable @change="searchTable1();machineChange()">
          <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>
        <el-button size="small" icon="el-icon-circle-plus" type="primary" style="margin-left: 25%" @click="addTable()">增加装配计划</el-button>
        <el-button type="danger" plain size="small" style="float: right;margin-right: 20px" @click="deleteData()">删除</el-button>
      </el-row>
    </el-card>
    <el-card>
      <div v-for="(file, key) in hadFile" :key="key">
        <div class="wrapper">
          <div class="content">
            <div class="action"/>
            <div v-if="file.suffix==='pdf'" class="icon"><svg-icon icon-class="pdf" /></div>
            <div v-else-if="file.suffix==='docx'||file.suffix === 'doc'" class="icon"><svg-icon icon-class="doc" /></div>
            <div v-else-if="file.suffix==='xlsx' || file.suffix === 'xls'" class="icon"><svg-icon icon-class="excel" /></div>
            <div v-else-if="file.suffix==='pptx' || file.suffix === 'ppt'" class="icon"><svg-icon icon-class="ppt" /></div>
            <div v-else-if="file.suffix==='jpg' || file.suffix === 'jpeg'|| file.suffix === 'png'" class="icon"><svg-icon icon-class="pic" /></div>
            <div v-else class="icon"><svg-icon icon-class="file" /></div>
            <a :href="file.url" target="view_window"><div class="info">{{ file.name }}</div></a>
          </div>
        </div>
      </div>
    </el-card>
    <el-card style="width: 40%; float: left">
      <el-table :data="tableData1" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="720px" @row-click="searchTable2">
        <el-table-column label="项目名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床编号" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床图号 }}
          </template>
        </el-table-column>
        <el-table-column label="机床名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床名称 }}
          </template>
        </el-table-column>
        <el-table-column label="机床数量" align="center">
          <template slot-scope="scope">
            {{ scope.row.机床数量 }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-card style="width: 59.5%; float: left">
      <el-table :data="tableData2" border size="mini" highlight-current-row style="width: 100%;margin-top: 15px" height="720px" @row-dblclick="changeState" @row-click="deleteData1">
        <el-table-column label="组号" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-select v-model="scope.row.组件流水号" filterable clearable size="small" style="width: 100px" @change="groupChange(scope.row, scope.$index)">
                <el-option
                  v-for="item in groupNum"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"/>
              </el-select>
            </template>
            <template v-else>
              {{ scope.row.组号 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="任务名称" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.任务名称" size="mini" style="width: 100%"/>
            </template>
            <template v-else>
              {{ scope.row.任务名称 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="计划工时" align="center">
          <template slot-scope="scope">
            <template v-if="scope.row.edit">
              <el-input v-model="scope.row.计划工时" size="mini" style="width: 100%"/>
            </template>
            <template v-else>
              {{ scope.row.计划工时 }}
            </template>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="250px" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row.edit" type="success" plain size="small" @click="confirmEdit(scope.row)">确定</el-button>
            <el-button v-else type="primary" size="small" plain @click="scope.row.edit=!scope.row.edit">编辑</el-button>
            <el-button v-if="scope.row.edit" type="warning" plain size="small" @click="cancel(scope.row)">取消</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>import { initProject, searchgroup, searchgroupName, getFileData, searchTable1, searchTable2, deletedata, editData, addData } from '@/api/Assembly/MakeAssemblyPlan'
import { ServerIP } from '@/utils/request'
// import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      machineNumberValue: '',
      machineNumber: [],
      groupNumberValue: '',
      groupNum: [],
      tableData1: [],
      tableData2: [],
      hadFile: [],
      plan: ''
    }
  },
  created() {
    this.searchTable1()
  },
  mounted() {
    this.initProject()
  },
  methods: {
    getFileData(machineValue) {
      this.hadFile = []
      getFileData(machineValue).then(res => {
        if (res.data.length > 0) {
          res.data.map(item => {
            this.hadFile.push({
              url: `${ServerIP}${item.文件标识}.${item.文件后缀}`,
              name: `${item.文件名称}.${item.文件后缀}`,
              suffix: item.文件后缀,
              id: item.文件标识
            })
          })
        }
      })
    },
    groupChange(row, index) {
      this.groupNumberValue = ''
      this.tableData2[index].任务名称 = ''
      if (row.组件流水号 !== '' && row.组件流水号 !== null) {
        searchgroupName(row.组件流水号).then(response => {
          console.log(row, 22222)
          this.groupNumberValue = response.data[0].组号
          this.tableData2[index].任务名称 = response.data[0].组件名称
        })
      }
    },
    changeState(row) {
      row.edit = !row.edit
    },
    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].机床流水号
          })
        }
      })
    },
    confirmEdit(row) {
      editData(row.计划流水号, row.组件流水号, this.groupNumberValue, row.任务名称, row.计划工时).then(response => {
        if (response.data[0].result === '1') {
          row.edit = false
          this.tableData2 = []
          searchTable2(this.machineNumberValue, 3).then(response => {
            for (let i = 0; i < response.data.length; i++) {
              this.tableData2.push({
                计划流水号: response.data[i].计划流水号,
                组件流水号: response.data[i].组件流水号,
                组号: response.data[i].组号,
                任务名称: response.data[i].任务名称,
                计划工时: response.data[i].计划工时,
                edit: false
              })
            }
          })
          this.$message.success('编辑成功')
        } else {
          this.$message.error('编辑失败')
        }
      })
    },
    cancel(row) {
      row.edit = !row.edit
    },
    deleteData1(row) {
      this.plan = row.计划流水号
    },
    deleteData() {
      this.deleteRow(deletedata, this.plan, function() {
        this.tableData2 = []
        searchTable2(this.machineNumberValue, 3).then(response => {
          for (let i = 0; i < response.data.length; i++) {
            this.tableData2.push({
              计划流水号: response.data[i].计划流水号,
              组件流水号: response.data[i].组件流水号,
              组号: response.data[i].组号,
              任务名称: response.data[i].任务名称,
              计划工时: response.data[i].计划工时,
              edit: false
            })
          }
        })
      })
    },
    addTable() {
      if (this.machineNumberValue !== '') {
        addData(this.machineNumberValue, 3).then(response => {
          console.log(response)
          if (response.data[0].result === '1') {
            this.tableData2 = []
            searchTable2(this.machineNumberValue, 3).then(response => {
              for (let i = 0; i < response.data.length; i++) {
                this.tableData2.push({
                  计划流水号: response.data[i].计划流水号,
                  组件流水号: response.data[i].组件流水号,
                  组号: response.data[i].组号,
                  任务名称: response.data[i].任务名称,
                  计划工时: response.data[i].计划工时,
                  edit: false
                })
              }
            })
            this.$message.success('增加成功')
          } else {
            this.$message.error('增加失败')
          }
        })
      } else {
        this.$message.warning('请选择机床')
      }
    },
    searchTable1() {
      this.tableData1 = []
      this.tableData2 = []
      if (this.machineNumberValue !== '' && this.machineNumberValue !== null) { this.check2 = 1 } else { this.check2 = 0 }
      searchTable1(this.machineNumberValue, this.check2).then(response => {
        this.tableData1 = response.data
      })
    },
    searchTable2(row) {
      this.tableData2 = []
      this.machineNumberValue = row.机床流水号
      searchTable2(this.machineNumberValue, 3).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.tableData2.push({
            计划流水号: response.data[i].计划流水号,
            组件流水号: response.data[i].组件流水号,
            组号: response.data[i].组号,
            任务名称: response.data[i].任务名称,
            计划工时: response.data[i].计划工时,
            edit: false
          })
        }
      })
      this.groupNumberValue = ''
      this.groupNum = []
      searchgroup(this.machineNumberValue).then(response => {
        for (let i = 0; i < response.data.length; i++) {
          this.groupNum.push({
            label: response.data[i].组号,
            value: response.data[i].组件流水号
          })
        }
      })
      this.getFileData(this.machineNumberValue)
    }
  }
}

</script>

<style scoped>

</style>
