<template>
  <div>
    <el-card>
      <div>
        <span>项目名称：</span>
        <el-select v-model="entryNameValue" clearable filterable placeholder="项目名称" size="small" style="width:200px;margin-top: 3px">
          <el-option
            v-for="item in entryName"
            :key="item.value"
            :label="item.label"
            :value="item.value"/>
        </el-select>
        <span style="margin-left: 10px">机床型号：</span>
        <el-input v-model="machine" clearable size="small" style="width:200px" placeholder="请输入机床型号"/>
      </div>
      <div style="margin-top: 10px">
        <span style="margin-left: 0px">开始日期：</span>
        <el-date-picker
          v-model="startTime"
          style="width: 200px;"
          value-format="yyyy-MM-dd"
          size="small"
          type="date"
          placeholder="请选择开始日期"/>
        <span style="margin-left: 10px">结束日期：</span>
        <el-date-picker
          v-model="endTime"
          style="width: 200px;"
          value-format="yyyy-MM-dd"
          size="small"
          type="date"
          placeholder="请选择结束日期"/>
        <el-button type="success" icon="el-icon-search" size="small" style="margin-left: 10px" @click="PageSize = 10;PageCurrent = 1;fetchTableData1()">查询</el-button>
        <el-tooltip :open-delay="1200" effect="dark" content="新增项目" placement="top">
          <el-button type="primary" icon="el-icon-circle-plus-outline" size="small" style="margin-left: 10px" @click="ADD()">新增</el-button>
        </el-tooltip>
      </div>
    </el-card>
    <el-card>
      <el-table
        v-loading="listLoading"
        :data="tableData1"
        :row-key="getRowKeys"
        :expand-row-keys="expands"
        style="width: 100%"
        height="500"
        border
        stripe
        size="mini"
        @expand-change="selectMachine">
        <el-table-column type="expand" label="详情">
          <template slot-scope="scope">
            <el-table :data="tableData2" class="subTableHeader" border stripe style="width: 612px; text-align: center" size="mini">
              <el-table-column label="设备型号" align="center" width="150">
                <template slot-scope="scope">
                  {{ scope.row.机床图号 }}
                </template>
              </el-table-column>
              <el-table-column label="设备名称" align="center" width="160">
                <template slot-scope="scope">
                  {{ scope.row.机床名称 }}
                </template>
              </el-table-column>
              <el-table-column label="机床数量" align="center" width="100">
                <template slot-scope="scope">
                  {{ scope.row.机床数量 }}
                </template>
              </el-table-column>
              <el-table-column label="操作" width="200" align="center">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="editMachine(scope.row);flag = 2">编辑</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="deleteMachine(scope.row)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同编号" sortable prop="合同编号" width="150">
          <template slot-scope="scope">
            <template>
              <el-tag slot="reference">{{ scope.row.合同编号 }}</el-tag>
            </template>
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目名称" sortable prop="项目名称" min-width="150">
          <template slot-scope="scope">
            {{ scope.row.项目名称 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="项目下达日期" sortable prop="合同签订日期" width="150">
          <template slot-scope="scope">
            {{ scope.row.合同签订日期 }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="合同信息备注" min-width="200">
          <template slot-scope="scope">
            {{ scope.row.合同信息备注 }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="350">
          <template slot-scope="scope">
            <el-tooltip :open-delay="1200" effect="dark" content="增加机床" placement="top">
              <el-button
                size="mini"
                type="warning"
                plain
                icon="el-icon-plus"
                @click.stop="addMachine(scope.row);flag = 1">机床</el-button>
            </el-tooltip>
            <el-button
              size="mini"
              plain
              type="primary"
              icon="el-icon-edit"
              @click.stop="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              plain
              icon="el-icon-delete"
              @click.stop="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 10px;">
        <el-pagination
          :current-page="PageCurrent"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="PageSize"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </el-card>
    <!--新增合同信息对话框-->
    <el-dialog v-el-drag-dialog :title="title" :visible.sync="dialogFormVisible1" center style="min-height: 300px" width="400px">
      <el-form ref="form" :model="form" :rules="rules" label-position="left" label-width="140px" class="demo-ruleForm">
        <el-row>
          <el-col :span="24">
            <el-form-item label="项目名称" prop="EntryName">
              <el-input v-model="form.EntryName" size="small" placeholder="请输入项目名称" style="width:200px"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="项目下达日期" prop="contractSigningDate">
              <el-date-picker
                v-model="form.contractSigningDate"
                style="width: 200px"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
                size="small"
                type="date"
                placeholder="请选择项目下达日期"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                style="width: 200px"
                type="textarea"
                autosize
                placeholder="请输入项目介绍"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff('form')">取消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog v-el-drag-dialog :title="title1" :visible.sync="dialogFormVisible2" center style="min-height: 300px" width="450px">
      <el-form ref="form1" :model="form1" :rules="rules1" label-width="140px">
        <el-form-item v-show="flag != 2" label="项目名称" prop="项目名称">
          <el-input v-model="form1.项目名称" size="small" style="width:200px"/>
        </el-form-item>
        <el-form-item label="设备型号" prop="设备型号">
          <el-input v-model="form1.设备型号" size="small" placeholder="请输入设备型号" style="width:200px"/>
        </el-form-item>
        <el-form-item label="设备名称" prop="设备名称">
          <el-input v-model="form1.设备名称" size="small" placeholder="请输入设备名称" style="width:200px"/>
        </el-form-item>
        <el-form-item label="机床数量" prop="机床数量">
          <el-input-number :min="1" v-model="form1.机床数量" controls-position="right" style="width:200px" size="small"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="callOff2('form1')">取消</el-button>
        <el-button type="primary" @click="submitMachine('form1')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import elInput from 'element-ui/packages/input'
import { getNowTime } from '@/utils/tool'
import { initProject, searchTable1, selectMachine, addList, editchar, delList, submitMachine, editMachine, delMachine } from '@/api/MarketingCenter/CreateProject'
export default {
  components: {
    elInput
  },
  data() {
    return {
      entryName: [],
      entryNameValue: '',
      machine: '',
      startTime: '',
      endTime: '',
      PageCurrent: 1,
      PageSize: 10,
      total: null,
      listLoading: false,
      entryNameValue_check: 0,
      startTime_check: 0,
      endTime_check: 0,
      machine_check: 0,
      a: 1,
      b: 1,
      c: 1,
      tableData1: [],
      expands: [],
      tableData2: [],
      projectNum: '',
      hetongID: '',
      form: {
        EntryName: '', // 项目名称
        contractSigningDate: '',
        remark: ''
      },
      dialogFormVisible1: false,
      title: '',
      rules: { // 表单验证规则
        EntryName: [{ required: true, message: '请输入项目名称', trigger: 'blur' }],
        contractSigningDate: [{ required: true, message: '请选择项目下达日期', trigger: 'change' }]
      },
      dialogFormVisible2: false,
      title1: '',
      form1: {
        项目名称: '',
        设备型号: '',
        设备名称: '',
        机床数量: 1
      },
      rules1: { // 表单验证规则
        设备型号: [{ required: true, message: '请输入设备型号', trigger: 'blur' }],
        设备名称: [{ required: true, message: '请输入设备名称', trigger: 'blur' }]
      },
      temp: '',
      flag: ''
    }
  },
  created() {
    this.init()
    this.fetchTableData1()
  },
  methods: {
    init() {
      this.getSelect(initProject, ['项目名称', '项目流水号'], 'entryName')
    },
    handleSizeChange(val) {
      this.PageCurrent = 1
      this.PageSize = val
      this.fetchTableData1()
    },
    handleCurrentChange(val) {
      this.PageCurrent = val
      this.fetchTableData1()
    },
    fetchTableData1() {
      if (this.entryNameValue === '' || this.entryNameValue === null) { this.entryNameValue_check = 0 } else { this.entryNameValue_check = 1 }
      if (this.startTime === '' || this.startTime === null) { this.startTime_check = 0 } else { this.startTime_check = 1 }
      if (this.endTime === '' || this.endTime === null) { this.endTime_check = 0 } else { this.endTime_check = 1 }
      if (this.machine === '' || this.machine === null) { this.machine_check = 0 } else { this.machine_check = 1 }
      this.listLoading = true
      this.getTable(searchTable1, [this.PageCurrent, this.PageSize, this.entryNameValue, this.entryNameValue_check, this.startTime_check, this.endTime_check, this.startTime, this.endTime, this.machine_check, this.machine], ['tableData1', 'total', 'listLoading'])
    },
    selectMachine(row) {
      this.projectNum = row.项目流水号
      if (this.a === 1) {
        this.expands.push(row.项目流水号)
        this.b = row.项目流水号
        this.a = 0
      } else {
        if (this.b === row.项目流水号) {
          this.expands = []
          this.a = 1
        } else {
          this.expands = []
          this.expands.push(row.项目流水号)
          this.a = 0
          this.b = row.项目流水号
        }
      }
      selectMachine(row.项目流水号).then(response => {
        this.tableData2 = response.data
      })
    },
    getRowKeys(row) {
      return row.项目流水号
    },
    ADD() {
      this.addForm('form')
      this.dialogFormVisible1 = true
      this.form.contractSigningDate = getNowTime()// new Date()
      this.temp = 1
      this.title = '新增项目'
    },
    // 提交添加或者修改
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.temp === 1) {
            this.addTableData1()
            this.temp = null
          } else if (this.temp === 2) {
            this.chaEdit()
            this.temp = null
          }
        }
      })
    },
    addTableData1() {
      addList(this.form.EntryName, this.form.remark, this.form.contractSigningDate).then(response => {
        this.dialogFormVisible1 = false
        if (response.data[0].result === '1') {
          this.$message.success('添加成功')
          this.title = ''
          this.fetchTableData1()
        } else {
          this.$message.error('添加失败')
        }
      })
    },
    callOff() {
      this.dialogFormVisible1 = false
      this.dialogFormVisible2 = false
    },
    handleEdit(row) {
      this.editForm(row, 'form')
      this.title = '编辑项目'
      this.dialogFormVisible1 = true
      this.form.contractSigningDate = row.合同签订日期
      this.form.contractNumber = row.合同编号
      this.form.EntryName = row.项目名称
      this.hetongID = row.项目流水号
      this.form.remark = row.合同信息备注
      this.temp = 2
    },
    chaEdit() {
      editchar(this.form.EntryName, this.form.remark, this.hetongID, this.form.contractSigningDate).then(response => {
        this.dialogFormVisible1 = false
        if (response.data[0].result === '1') {
          this.$message.success('信息更新成功')
          this.fetchTableData1()
        } else {
          this.$message.error('信息更新失败')
        }
      })
    },
    handleDelete(row) {
      this.deleteRow(delList, row.项目流水号, function() { this.fetchTableData1() })
    },
    addMachine(row) {
      this.projectNum = row.项目流水号
      this.dialogFormVisible2 = true
      this.title1 = '新增机床'
      this.addForm('form1')
      this.form1.项目名称 = row.项目名称
    },
    submitMachine() {
      function a() {
        selectMachine(this.projectNum).then(response => {
          this.tableData2 = response.data
        })
        this.dialogFormVisible2 = false
      }
      if (this.flag === 1) {
        this.addTable(submitMachine, [this.projectNum, this.form1.设备型号, this.form1.设备名称, this.form1.机床数量], 'form1', a)
      } else {
        this.editTable(editMachine, [this.c, this.form1.设备型号, this.form1.设备名称, this.form1.机床数量], 'form1', a)
      }
    },
    deleteMachine(row) {
      function a() {
        selectMachine(this.projectNum).then(response => {
          this.tableData2 = response.data
        })
      }
      this.deleteRow(delMachine, row.机床流水号, a)
    },
    editMachine(row) {
      this.flag = 2
      this.editForm(row, 'form1')
      this.dialogFormVisible2 = true
      this.title1 = '编辑机床'
      this.c = row.机床流水号
      this.form1.设备型号 = row.机床图号
      this.form1.设备名称 = row.机床名称
    },
    callOff2() {
      this.dialogFormVisible2 = false
    }
  }
}
</script>

<style scoped>

</style>
<style lang="scss">
  .subTableHeader {
    th {
      background: #8ff4ea !important;
      color: black;
    }

    tr.el-table__row td {
      background-color: #d8e5f6 !important;
    }

    tr.el-table__row.el-table__row--striped td {
      background: #e9f0f9 !important;
    }
  }
</style>
