<template>
<!--  <div v-if="visitor">-->
    <a-modal
      :title="title"
      :width="800"
      :visible="visitor!==0"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-form-model ref="form" :model="model" :rules="validatorRules" >
        <a-row>
          <a-col :span="24">
            <a-form-model-item v-if="visitor===1" label="班组/部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="createDept">
              <a-tree-select v-model="model.createDept" placeholder="请选择班组/部门" :tree-data="departTree" ></a-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="visitor===1" label="工单负责人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="coordinator">
              <a-select v-model="model.coordinator" placeholder="请选择工单负责人" :options="userList" ></a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="visitor===1" label="工单配合人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executor">
              <a-select v-model="model.executor" placeholder="请选择工单配合人" :options="userList" ></a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="visitor===2" label="开工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="startTime">
              <j-date placeholder="请选择开工时间" v-model="model.startTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="visitor===3" label="完工时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="finishTime">
              <j-date placeholder="请选择完工时间"  v-model="model.finishTime" :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </a-modal>
<!--  </div>-->
</template>

<script>
import { queryIdTree, queryUserByDepId } from '@api/api'
import { putAction } from '@api/manage'

export default {
  name: 'WorkOrderManageForm',
  components: {},
  props: {
    visitor: {
      type: Number,
      default: 0,
    },
    title: {
      type: String,
    },
    workOrder: {
      type: String,
    }
  },
  data () {
    return {
      model:{},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        coordinator: [
          { required: true, message: '请选择工单负责人!'},
        ],
        executor: [
          { required: true, message: '请选择工单配合人!'},
        ],
        startTime: [
          { required: true, message: '请选择开工时间!'},
        ],
        finishTime: [
          { required: true, message: '请选择完工时间!'},
        ],
      },
      departTree: [],
      userList: [],
    }
  },
  watch: {
    visitor(newVal,oldVal) {
      if(newVal===1){
        this.queryDepartTree();
      }
    },
    'model.createDept' (newVal, oldVal) {
      this.queryUserByDepartId(newVal);
    }
  },
  // created() {
  //   if(this.visitor===1){
  //     this.queryDepartTree();
  //     console.log("******************111", this.departTree)
  //   }
  // },
  methods: {
    queryDepartTree() {
      queryIdTree().then((res)=>{
        if(res.success){
          this.departTree = res.result;
        }
      })
    },
    queryUserByDepartId(id) {
      queryUserByDepId({id:id}).then((res)=>{
        if(res.success && res.result.length>0){
          this.userList = res.result.map(item => ({
            value: item.id,
            label: item.realname
          }));
        }
      })
    },
    handleOk() {
      this.model.id = this.workOrder;
      if (this.visitor === 1) {
        if (!this.model.createDept) {
          this.$message.warning("请确认班组、负责人、配合人是否分配！");
          return;
        }
        this.model.status = this.model.coordinator ? 2 : 1;
      } else if (this.visitor === 2) {
        this.model.status = 3;
      } else if (this.visitor === 3) {
        this.model.status = 4;
      } else if (this.visitor === 7) {
        this.model.status = 7;
      } else if (this.visitor === 4) {
        if (!this.model.remark) {
          this.$message.warning("撤销工单时，必须填写备注！");
          return;
        }
        this.model.status = 0;
      }

      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true;
          putAction("/workOrder/workOrder/edit",this.model).then((res)=>{
            if(res.success){
              that.$message.success(res.message);
              that.$emit('close');
            }else{
              that.$message.warning(res.message);
            }
          }).finally(() => {
            that.confirmLoading = false;
          })
        }
      })
    },
    handleCancel() {
      this.departTree = []
      this.userList = []
      this.model = {}
      this.$emit('close')
    },
  }
}

</script>

<style scoped>

</style>