<template>
  <div>
    <div class="my-title">{{ !id ? "新增" : "修改" }}</div>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="180px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="dataForm.name" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="主管" prop="workshopManager">
            <UserComponent v-model="dataForm.workshopManager" :user-id="dataForm.workshopManager" :displayStar="true" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="描述" prop="description">
            <el-input v-model="dataForm.description" placeholder="请输入" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注说明" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="请输入" type="textarea"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="id">
        <el-col :span="24">
          <el-form-item label="工种列表">
            <el-table :data="dataForm.proWorkTypeList" border style="width: 100%;margin-top: 10px">
              <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
              <el-table-column prop="name" label="工种名称" wimin-widthdth="150"></el-table-column>
              <el-table-column prop="levelValue" label="工种级别" min-width="120"></el-table-column>
              <el-table-column prop="requirement" label="工种要求" min-width="120"></el-table-column>
              <el-table-column prop="quotedPrice" label="工时单价" min-width="100"></el-table-column>
              <el-table-column prop="masterNames" label="掌握人" min-width="120"
                :show-tooltip-when-overflow="true"></el-table-column>
              <el-table-column prop="workshopManagerName" label="负责人" min-width="120"
                :show-tooltip-when-overflow="true"></el-table-column>
              <el-table-column prop="createTime" label="创建时间" min-width="160"></el-table-column>
              <el-table-column prop="notes" label="备注" min-width="160"
                :show-tooltip-when-overflow="true"></el-table-column>
              <el-table-column fixed="right" label="操作" width="50">
                <template slot-scope="{ row, $index }">
                  <el-button style="color: red" type="text" size="small"
                    @click="removeWorkType(row.typeId, $index)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="onChose">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
    </span>
  </div>
</template>

<script>
import { getDetail } from '@/api/workshop'
import { removeWorkType } from '@/api/worktype'
import UserComponent from '../common/user-component.vue'
export default {
  name: 'workshop-add-or-update',
  components: {
    UserComponent
  },
  data() {
    return {
      id: 0,
      dataForm: {
        proWorkTypeList: [],
      },
      materialList: [],
      dataRule: {
        name: [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符之间', trigger: 'blur' }
        ],
        workshopManager: [
          { required: true, message: '请选择主管', trigger: 'change' }
        ]
      }
    }
  },
  created() {
  },
  methods: {
    init(id) {
      this.id = id || 0
      if (!id) return
      this.getDetail()
    },
    onChose() {
      this.$emit('onChose')
    },
    getDetail() {
      getDetail(this.id).then(({ data }) => {
        if (data && data.code === '200') {
          this.dataForm = data.data || {}
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    dataFormSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: !this.id
              ? this.$http.adornUrl(`/biz-service/pro-workshop/save`)
              : this.$http.adornUrl(`/biz-service/pro-workshop/update`),
            method: 'post',
            data: this.$http.adornData({ ...this.dataForm, orgId: this.orgId })
          }).then(({ data }) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.onChose()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    removeWorkType(typeId, $index) {
      this.$confirm('确定要删除该工种记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.dataListLoading = true
        let ids = []
        ids.push(typeId)
        // 调用删除接口
        removeWorkType(ids).then(({ data }) => {
          this.dataListLoading = false
          if (data && data.code === '200') {
            this.$message.success('删除成功')
            // 删除这一行
            this.dataForm.proWorkTypeList.splice($index, 1)

          } else {
            this.$message.error(data && data.msg ? data.msg : '删除失败')
          }
        }).catch(() => {
          this.dataListLoading = false
        })
      }).catch(() => { })
    }
  }
}
</script>

<style></style>
