<template>
  <el-dialog :title="title" :visible.sync="visible" width="80%" append-to-body custom-class="custom-modal">
    <div class="app-container">
      <div class="filter-container">
        <el-input
          v-model="listQuery.keyWord"
          placeholder="关键字"
          style="width: 200px"
          class="filter-item"
          :maxlength="30"
          @keyup.enter.native="handleFilter"
        />
        <el-button
          v-waves
          style="margin-left: 40px"
          class="filter-item"
          type="primary"
          @click="handleFilter"
        >
          查询
        </el-button>
        <el-button v-waves class="filter-item" style="margin-left: 10px" type="primary" @click="handleCreate">
          新增
        </el-button>
      </div>

      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        :max-height="tableHeight"
      >
        <el-table-column label="序号" align="center" width="80" type="index">
          <template scope="scope">
            <span>{{ (listQuery.pageNum - 1) * 10 + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="工序名称" align="center" prop="gxName" />
        <el-table-column label="计划开始时间" align="center" prop="planStartTime" />
        <el-table-column label="计划结束时间" align="center" prop="planEndTime" />
        <el-table-column label="实际开始时间" align="center" prop="startTime" />
        <el-table-column label="实际结束时间" align="center" prop="endTime" />
        <el-table-column label="进度占比" align="center" prop="gxWeight" />
        <el-table-column label="延迟原因" align="center" prop="delaReason" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
          <template slot-scope="{ row, $index }">
            <el-button v-waves type="text" size="mini" @click="handleUpdate(row, $index)">编辑</el-button>
            <el-button v-waves type="text" size="mini" @click="handleDelete(row, $index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.pageNum"
        :limit.sync="listQuery.pageSize"
        @pagination="getList"
      />
    </div>
    <el-dialog append-to-body :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="630px" @closed="handleClosedModal">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="right"
        label-width="120px"
      >
        <el-form-item :label="`${title}：`" prop="gxId">
          <el-select v-model="temp.gxId" placeholder="请选择" style="width: 100%" clearable>
            <el-option v-for="item in typeList" :key="item.id" :value="item.id" :label="item.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="计划开始时间：" prop="planStartTime">
          <el-date-picker
            v-model="temp.planStartTime"
            style="width: 100%"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="计划结束时间：" prop="planEndTime">
          <el-date-picker
            v-model="temp.planEndTime"
            style="width: 100%"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期"
          />
        </el-form-item>
        <el-form-item label="作业权重(%)：" prop="gxWeight">
          <el-input v-model="temp.gxWeight" placeholder="进度占比" :maxlength="50" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-waves @click="dialogFormVisible = false"> 关闭 </el-button>
        <el-button v-waves type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">
          提交
        </el-button>
      </div>
    </el-dialog>
  </el-dialog>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

import _ from 'lodash/fp'
import {
  addProcessItem,
  deleteProcessItem,
  getProcessItemDetail,
  getProcessList, getTowerList,
  updateProcessItem
} from '@/api/basic/project'

const defaultTmp = {
  gxId: '',
  // gxName: '',
  planStartTime: '',
  planEndTime: '',
  startTime: '',
  endTime: '',
  gxWeight: '',
  bidCode: ''
}
export default {
  name: 'ProcessTable',
  components: { Pagination },
  directives: { waves },
  props: ['bidCode', 'processType', 'componentVisible', 'currentFormData'],
  data() {
    return {
      tableKey: 0,
      list: [],
      typeList: [],
      total: 0,
      listLoading: false,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        keyword: '',
        bidCode: '',
        proType: ''
      },
      tableHeight: 650,
      showReviewer: false,
      temp: _.cloneDeep(defaultTmp),
      dialogFormVisible: false,
      dialogStatus: '',
      downloadLoading: false,
      textMap: {
        update: '编辑',
        create: '新增'
      },
      processModalVisible: false,
      rules: {
        gxName: [{ required: true, message: '请选择', trigger: 'change' }],
        planStartTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        planEndTime: [{ required: true, message: '不能为空', trigger: 'blur' }],
        gxWeight: [{ required: true, message: '不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    visible: {
      get() {
        return this.componentVisible
      },
      set(val) {
        this.$emit('update:componentVisible', val)
      }
    },
    title() {
      return this.processType === '线路' ? '杆塔' : '工序计划'
    }
  },
  watch: {
    visible(val) {
      if (val) {
        this.getList()
        this.getTowerList()
      }
    }
  },
  created() {
    // this.getList()
  },
  methods: {
    getTowerList() {
      getTowerList({ param: this.processType === '线路' ? this.bidCode : '400', proType: this.processType === '线路' ? 2 : 1 }).then(res => {
        this.typeList = res.data
      })
    },
    getList() {
      this.listLoading = true
      this.listQuery.bidCode = this.bidCode
      this.listQuery.proType = this.processType
      getProcessList(this.listQuery).then((response) => {
        this.list = response.rows
        this.total = response.total
      }).finally(() => {
        this.listLoading = false
      })
    },
    // 查询
    handleFilter() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    // 新增
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.temp.bidCode = this.bidCode
          addProcessItem(this.temp).then((response) => {
            this.$message({
              showClose: true,
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).finally(() => {
            this.dialogFormVisible = false
          })
        }
      })
    },
    // 编辑
    handleUpdate(row) {
      getProcessItemDetail({ planId: row.planId, bidCode: row.bidCode }).then((res) => {
        this.temp = Object.assign({}, res.data)
      })
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateProcessItem(this.temp).then((response) => {
            this.$message({
              showClose: true,
              message: response.msg,
              type: 'success',
              duration: 2000
            })
            this.getList()
          }).finally(() => {
            this.dialogFormVisible = false
          })
        }
      })
    },
    // 删除数据
    handleDelete(row, index) {
      this.$confirm(`确定要删除该数据吗？`, {
        type: 'warning',
        title: '操作提示',
        beforeClose: async(action, instance, done) => {
          if (action === 'confirm') {
            deleteProcessItem({ planId: row.planId }).then((response) => {
              done()
              this.$message({
                showClose: true,
                message: response.msg,
                type: 'success',
                duration: 2000
              })
              this.getList()
            })
          } else {
            done()
          }
        }
      })
    },
    handleClosedModal() {
      this.$refs['dataForm'].resetFields()
      this.temp = _.cloneDeep(defaultTmp)
    }
  }
}
</script>
