<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType !='view'" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mes:pro:proroute:add']"
        >{{ $t('common.add') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:pro:proroute:edit']"
        >{{ $t('common.edit') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mes:pro:proroute:remove']"
        >{{ $t('common.del') }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeprocessList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column :label="$t('role.idx')" align="center" fixed prop="orderNum" />
      <el-table-column :label="$t('工序编码')" width="120px" fixed align="center" prop="processCode" />
      <el-table-column :label="$t('工序名称')" width="150px" fixed align="center" prop="processName" />
      <el-table-column :label="$t('下一道工序')" width="150px"  align="center" prop="nextProcessName" />
      <el-table-column :label="$t('与下一道工序关系')" width="150px" align="center" prop="linkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_link_type" :value="scope.row.linkType"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('关键工序')" width="100px" align="center" prop="keyFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.keyFlag"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('质检确认')" width="100px" align="center" prop="isCheck">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.isCheck"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('准备时间')" align="center" prop="defaultPreTime" />
      <el-table-column :label="$t('等待时间')" align="center" prop="defaultSufTime" />
      <el-table-column :label="$t('甘特图显示颜色')" align="center" prop="colorCode" >
        <template slot-scope="scope">
            <el-color-picker v-model="scope.row.colorCode" disabled></el-color-picker>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.option')" align="center" fixed="right" width="150px" v-if="optType !='view'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:proroute:edit']"
          >{{ $t('common.edit') }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:proroute:remove']"
          >{{ $t('common.del') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工艺组成对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1100px" append-to-body>    
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
            <el-col :span="8">
                <el-form-item :label="$t('role.idx')" width="120px" prop="orderNum">
                    <el-input-number :min="1" v-model="form.orderNum" />
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('工序')" prop="processId">
                    <el-select v-model="form.processId" @change="changeProcessId">
                        <el-option
                        v-for="item in processOptions"
                        :key="item.processId"
                        :label="item.processName"
                        :value="item.processId"
                        ></el-option>
                    </el-select>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('与下一道工序关系')" prop="linkType">
                    <el-tooltip effect="dark" placement="right">
                    <div slot="content">{{$t('S-to-S：当前工序开始生产，下一道工序才可开始生产')}}</br>{{$t('F-to-F：当前工序结束生产，下一道工序才可结束生产')}}</br>{{$t('S-to-F：当前工序开始生产，下一道工序才可结束生产')}}</br>{{$t('F-to-S：当前工序结束生产，下一道工序才可开始生产')}}</div>
                    <el-select v-model="form.linkType">
                        <el-option
                        v-for="dict in dict.type.mes_link_type"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        ></el-option>
                    </el-select>
                    </el-tooltip>
                </el-form-item>
            </el-col>
            <el-col :span="5">
                <el-form-item :label="$t('甘特图显示颜色')" prop="colorCode">
                    <el-color-picker v-model="form.colorCode"></el-color-picker>                        
                </el-form-item>
            </el-col>
            <el-col :span="2">
                    <el-input v-model="form.colorCode" style="left: 5px" maxlength="7" />                
            </el-col>
            <el-col :span="7">
              <el-form-item :label="$t('是否关键工序')" prop="keyFlag">
                    <el-tooltip effect="dark" placement="right">
                    <div slot="content">{{$t('是：整个工单的生产进度将根据当前工序的生产报工数量进行更新')}}</br>{{$t('每个工艺流程只能有一个关键工序')}}</div>
                    <el-select v-model="form.keyFlag">
                        <el-option
                        v-for="dict in dict.type.sys_yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        ></el-option>
                    </el-select>
                    </el-tooltip>
                </el-form-item>
            </el-col>
            <el-col :span="10">
              <el-form-item :label="$t('是否需要质检确认')" prop="isCheck">
                    <el-tooltip effect="dark" placement="right">
                    <div slot="content">{{$t('是：当前工序报工时需要进行质检确认')}}</br>{{$t('质检合格数量作为最终生产数量')}}</div>
                    <el-select v-model="form.isCheck">
                        <el-option
                        v-for="dict in dict.type.sys_yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                        ></el-option>
                    </el-select>
                    </el-tooltip>
                </el-form-item>
            </el-col>
          <el-col :span="8">
                <el-form-item :label="$t('准备时间')" prop="defaultPreTime">
                    <el-tooltip effect="dark" :content="$t('当前系统支持的最小时间粒度为1小时')" placement="right">
                        <el-input-number :min="0" :step="1" v-model="form.defaultPreTime" />
                    </el-tooltip>
                </el-form-item>
            </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('等待时间')" prop="defaultSufTime">
                    <el-input-number :min="0" :step="1" v-model="form.defaultSufTime" />
                </el-form-item>
            </el-col>
            <el-col :span="8">
            <el-form-item :label="$t('common.workstation')" prop="workstationId">
              <el-input
                v-model="form.workstationName"
                disabled
               
              >
                <el-button
                  slot="append"
                  icon="el-icon-search"
                  @click="handleWorkstationSelect"
                ></el-button>
              </el-input>
              <WorkstationSelect ref="WorkstationSelect" :processId="form.processId" @onSelected="onWorkstationSelected"></WorkstationSelect>
            </el-form-item>
          </el-col>
            <el-col :span="8">
                <el-form-item :label="$t('标准工时')" prop="stdWorkingTime">
                    <el-input-number :min="0" :step="0.1" v-model="form.stdWorkingTime" />
                </el-form-item>
            </el-col>
            <el-col :span="24">
                <el-form-item :label="$t('common.remark')" prop="remark">
                    <el-input v-model="form.remark" type="textarea" />
                </el-form-item>
            </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="cancel" v-if="optType =='view'">{{ $t('common.back') }}</el-button>
        <el-button type="primary" @click="submitForm" v-else>{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>


import { listRouteprocess, getRouteprocess, delRouteprocess, addRouteprocess, updateRouteprocess } from "@/api/mes/pro/routeprocess";
import {listAllProcess} from "@/api/mes/pro/process";
import WorkstationSelect from "@/components/workstationSelect/simpletableSingle.vue";
import { log } from 'three';
export default {
  name: "Routeprocess",
  dicts: ['mes_link_type','sys_yes_no'],
  components: {WorkstationSelect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺组成表格数据
      routeprocessList: [],
      //工序选项
      processOptions:[],    
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        routeId: this.routeId,
        processId: null,
        processCode: null,
        processName: null,
        orderNum: null,
        nextProcessId: null,
        nextProcessCode: null,
        nextProcessName: null,
        linkType: null,
        defaultPreTime: null,
        defaultSufTime: null,
        colorCode: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeId: [
          { required: true, message: this.$t('工艺路线ID不能为空'), trigger: "blur" }
        ],
        processId: [
          { required: true, message: this.$t('工序ID不能为空'), trigger: "blur" }
        ],
        nextProcessId: [
          { required: true, message: this.$t('工序ID不能为空'), trigger: "blur" }
        ],
        keyFlag: [
        { required: true, message: this.$t('请指定当前工序是否关键工序'), trigger: "blur" }
        ],
        isCheck: [
        { required: true, message: this.$t('请指定当前工序是否需要质检确认'), trigger: "blur" }
        ],
        workstationId: [
        { required: true, message: this.$t('请选择工作中心'), trigger: "blur" }
        ]
      }
    };
  },
  props :{
      routeId: undefined,
      optType: undefined
  },
  created() {
    this.getList();
    this.getProcess();
  },
  methods: {
    /** 查询工艺组成列表 */
    getList() {
      this.loading = true;
      listRouteprocess(this.queryParams).then(response => {
        this.routeprocessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //查询工序信息
    getProcess(){
      listAllProcess().then( response =>{
        console.log(response.data)
        this.processOptions = response.data;
        console.log(this.processOptions.processId)
      });
    },
    // 查询工作中心信息
    handleWorkstationSelect() {
      this.$refs.WorkstationSelect.showFlag = true;
    },
    onWorkstationSelected(row) {
      if (row != undefined && row != null) {
        this.form.workstationId = row.workstationId;
        this.form.workstationName = row.workstationName;
        this.form.stdWorkingTime = row.stdWorkingTime;
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        routeId: this.routeId,
        processId: null,
        processCode: null,
        processName: null,
        isCheck: 'N',
        orderNum: 1,
        nextProcessId: null,
        nextProcessCode: null,
        nextProcessName: null,
        linkType: null,
        defaultPreTime: 0,
        defaultSufTime: 0,
        colorCode: '#00AEF3',
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        workstationId: null,
        workstationName: null,
        stdWorkingTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('添加工艺组成');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getRouteprocess(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('修改工艺组成');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.recordId != null) {
            updateRouteprocess(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.edit_success'));
              this.open = false;
              this.getList();
            });
          } else {
            addRouteprocess(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.add_success'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 工序改变
    changeProcessId(){
         
        this.form.workstationId = ''
        this.form.workstationName = ''
        this.form.stdWorkingTime = ''
    // watch:{
    //   processId(newValue){
    //      alert("工序发生改变,请重新选择工作中心");
    //      this.form={
    //       workstationId : null,
    //       workstationName: null,
    //       stdWorkingTime: null
    //      }
    //   }

    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm(this.$t('common.confirm_del_data')).then(function() {
        return delRouteprocess(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.del_success'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/pro/routeprocess/export', {
        ...this.queryParams
      }, `routeprocess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
