<template>
  <div>
    <!--    <el-form-item :label="i18nt('有流程可编辑')">
      <el-switch v-model="optionModel.enabledByWf" @change="changeEnabledByWf"></el-switch>
    </el-form-item>
    <el-form-item :label="i18nt('有流程隐藏')">
      <el-switch v-model="optionModel.hiddenByWf" @change="changeHiddenByWf"></el-switch>
    </el-form-item>-->
    <el-form-item :label="i18nt('特定流程节点设置')">
      <el-switch v-model="optionModel.wfEdit" @change="changeWfEdit"></el-switch>
    </el-form-item>
    <el-form-item :label="i18nt('流程节点信息')">
      <a href="javascript:void(0);" class="a-link link-oneLind" @click="openDialog">
        <span>{{
          optionModel.wfConfigData && optionModel.wfConfigData.length ? "已维护" : ""
        }}</span>
        <i class="el-icon-edit"></i>
      </a>
    </el-form-item>
    <el-form-item :label="i18nt('过程更改业务数据')">
      <el-switch v-model="optionModel.wfModifyDataEnabled"></el-switch>
    </el-form-item>
    <el-form-item :label="i18nt('更改业务数据信息')">
      <a href="javascript:void(0);" class="a-link link-oneLind" @click="openDialog2">
        <span>{{
          optionModel.wfModifyDataConfig && optionModel.wfModifyDataConfig.length
            ? "已维护"
            : ""
        }}</span>
        <i class="el-icon-edit"></i>
      </a>
    </el-form-item>
    <el-dialog
      v-if="dialogVisible"
      custom-class="dialog-style list-dialog"
      :title="i18nt('流程节点信息')"
      :visible.sync="dialogVisible"
      :show-close="!0"
      :append-to-body="true"
      :modal="false"
      :close-on-click-modal="!1"
      :close-on-press-escape="!1"
      :destroy-on-close="!0"
      width="1220px"
      top="5vh"
      v-dialog-drag
      :fullscreen="true"
    >
      <div class="cont" style="padding-bottom: 8px">
        <el-table
          ref="singleTable"
          width="100%"
          :data="tableData"
          height="100%"
          border=""
          row-key="columnId"
          stripe=""
          style="margin-bottom: 0px"
        >
          <el-table-column type="index" width="35" fixed="left"></el-table-column>
          <el-table-column :label="i18nt('组织编码')" width="250">
            <template slot-scope="{ row, $index }">
              <el-input
                class="search-input"
                max="200"
                v-model="row.companyCodes"
                @clear="clearBdCompanyEnv(row)"
                v-el-readonly
                clearable
              >
                <i
                  slot="suffix"
                  class="el-input__icon el-icon-search"
                  @click="openBdCompanyEnvDialog($index)"
                ></i>
              </el-input>
            </template>
          </el-table-column>
          <el-table-column :label="i18nt('服务名称')" width="150">
            <template slot-scope="{ row }">
              {{ row.serveName }}
            </template>
          </el-table-column>
          <!-- <el-table-column :label="i18nt('流程模板编码')" width="150">
            <template slot-scope="{ row }">
              <el-input v-model="row.modelKey" clearable></el-input>
            </template>
          </el-table-column> -->
          <el-table-column :label="i18nt('流程模板序号')" width="150">
            <template slot-scope="{ row }">
              <base-input-number v-model="row.modelOrders" clearable></base-input-number>
            </template>
          </el-table-column>
          <el-table-column :label="i18nt('节点步骤（多个值用“,”隔开）')" width="250">
            <template slot-scope="{ row }">
              <el-input v-model="row.taskSteps" clearable></el-input>
            </template>
          </el-table-column>
          <el-table-column :label="i18nt('设置')" width="150">
            <template slot-scope="{ row }">
              <el-select v-model="row.type">
                <el-option :value="1" label="可编辑"></el-option>
                <!--                <el-option :value="2" label="仅显示"></el-option>
                <el-option :value="3" label="隐藏"></el-option>-->
              </el-select>
            </template>
          </el-table-column>
          <el-table-column
            :label="i18nt('designer.setting.actionColumn')"
            width="100"
            align="center"
          >
            <template #header>
              <span>{{ i18nt("designer.setting.actionColumn") }}</span>
              <el-button
                :title="i18nt('designer.setting.addTableColumn')"
                size="mini"
                type=""
                circle=""
                icon="el-icon-plus"
                @click="addItem"
              ></el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                :title="i18nt('designer.setting.deleteTableColumn')"
                size="mini"
                type=""
                circle=""
                icon="el-icon-minus"
                @click="tableData.splice(scope.$index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="dialog-footer" slot="footer">
        <el-button @click="dialogVisible = false" class="button-sty" icon="el-icon-close">
          {{ i18nt("designer.hint.cancel") }}
        </el-button>
        <el-button
          type="primary"
          @click="colSubmit"
          class="button-sty"
          icon="el-icon-check"
        >
          {{ i18nt("designer.hint.confirm") }}
        </el-button>
      </div>
    </el-dialog>
    <el-dialog
      v-if="dialogVisible2"
      custom-class="dialog-style list-dialog"
      :title="i18nt('过程更改业务数据')"
      :visible.sync="dialogVisible2"
      :show-close="!0"
      :append-to-body="true"
      :modal="false"
      :close-on-click-modal="!1"
      :close-on-press-escape="!1"
      :destroy-on-close="!0"
      width="1220px"
      top="5vh"
      v-dialog-drag
    >
      <div class="cont" style="padding-bottom: 8px">
        <el-table
          ref="singleTable"
          width="100%"
          :data="tableData2"
          height="500px"
          border=""
          row-key="columnId"
          stripe=""
          style="margin-bottom: 0px"
        >
          <el-table-column type="index" width="35" fixed="left"></el-table-column>
          <el-table-column :label="i18nt('流程动作')" width="150">
            <template slot-scope="{ row }">
              <el-select v-model="row.action" @change="row.toValue = null">
                <el-option value="start" label="流程启动"></el-option>
                <el-option value="interrupt" label="流程中断"></el-option>
                <el-option value="end" label="流程完成"></el-option>
                <el-option value="specificAction" label="流程特定"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <!--          <el-table-column :label="i18nt('值类型')" width="250">
                      <template slot-scope="{row}">
                        <el-select v-model="row.valueType" @change="changeValueType">
                          <el-option :value="1" label="文本"></el-option>
                          <el-option :value="2" label="数值"></el-option>
                          <el-option :value="3" label="布尔值"></el-option>
                        </el-select>
                      </template>
                    </el-table-column>-->
          <el-table-column :label="i18nt('设置的值')" width="650">
            <template slot-scope="{ row }">
              <el-select
                v-model="row.toValue"
                clearable
                style="width: 100%"
                v-if="row.action === 'specificAction'"
              >
                <el-option value="wfCreateDate" label="流程创建(启动)时间"></el-option>
                <el-option value="wfModifyDate" label="流程更新时间"></el-option>
                <el-option value="wfInterruptDate" label="流程中断时间"></el-option>
                <el-option value="wfEndDate" label="流程完成时间"></el-option>
                <el-option value="wfStarterName" label="启动人姓名"></el-option>
                <el-option value="wfName" label="流程模板名称"></el-option>
                <el-option value="wfUuid" label="流程实例uuid"></el-option>
                <el-option value="objTypeCode" label="流程单据类型编码"></el-option>
                <el-option value="wfCandidateNames" label="当前审批人名"></el-option>
                <el-option value="wfTaskName" label="当前审批节点名"></el-option>
                <el-option value="wfStat" label="流程状态"></el-option>
                <el-option value="wfSubjectName" label="流程主题名称"></el-option>
              </el-select>
              <el-input v-model="row.toValue" clearable v-else></el-input>
              <!--              <el-input v-model="row.toValue" clearable v-if="row.valueType==1"></el-input>
                            <base-input-number v-model="row.toValue" clearable v-else-if="row.valueType==2"></base-input-number>
                            <el-radio-group v-model="row.toValue" v-else-if="row.valueType==3">
                              <el-radio :label="true">true</el-radio>
                              <el-radio :label="false">false</el-radio>
                            </el-radio-group>-->
            </template>
          </el-table-column>
          <el-table-column
            :label="i18nt('designer.setting.actionColumn')"
            width="100"
            align="center"
          >
            <template #header>
              <span>{{ i18nt("designer.setting.actionColumn") }}</span>
              <el-button
                :title="i18nt('designer.setting.addTableColumn')"
                size="mini"
                type=""
                circle=""
                icon="el-icon-plus"
                @click="addItem2"
              ></el-button>
            </template>
            <template slot-scope="scope">
              <el-button
                :title="i18nt('designer.setting.deleteTableColumn')"
                size="mini"
                type=""
                circle=""
                icon="el-icon-minus"
                @click="tableData2.splice(scope.$index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="dialog-footer" slot="footer">
        <el-button
          @click="dialogVisible2 = false"
          class="button-sty"
          icon="el-icon-close"
        >
          {{ i18nt("designer.hint.cancel") }}
        </el-button>
        <el-button
          type="primary"
          @click="colSubmit2"
          class="button-sty"
          icon="el-icon-check"
        >
          {{ i18nt("designer.hint.confirm") }}
        </el-button>
      </div>
    </el-dialog>
    <companyInfoDialog
      v-if="showCompanyInfoDialog"
      :visiable.sync="showCompanyInfoDialog"
      @confirm="confirmCompanyDialog"
      :multi="true"
      :allCompany="true"
      :rows="currentCompanyInfos"
      fieldKey="companyCode"
    />
    <bdCompanyEnvDialog
      v-if="showBdCompanyEnvDialog"
      :visiable.sync="showBdCompanyEnvDialog"
      @confirm="confirmBdCompanyEnvDialog"
      :multi="false"
    />
  </div>
</template>

<script>
import i18n from "../../../../../components/xform/utils/i18n";
import companyInfoDialog from "@base/views/user/company_info/dialog.vue";
import bdCompanyEnvDialog from "@base/views/bd/setting/bd_company_env/dialog";

export default {
  name: "wfFlag-editor",
  components: { companyInfoDialog, bdCompanyEnvDialog },
  mixins: [i18n],
  props: {
    designer: Object,
    selectedWidget: Object,
    optionModel: Object,
  },
  inject: ["getReportTemplate"],
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      wfDefItems: [],
      wfNodeMap: {},

      currentCompanyInfos: [],
      operateIndex: 0,
      showCompanyInfoDialog: false,

      dialogVisible2: false,
      tableData2: [],

      showBdCompanyEnvDialog: false,
    };
  },
  computed: {
    current_prefix() {
      let reportTemplate = this.getReportTemplate();
      return "/" + reportTemplate.serviceName;
    },
  },
  methods: {
    openDialog() {
      let reportTemplate = this.getReportTemplate();
      let objTypeCode = reportTemplate.objTypeCode;
      let wfConfigData = this.optionModel.wfConfigData || [];
      this.dialogVisible = true;
      this.tableData = this.$baseLodash.cloneDeep(wfConfigData);
      /*wfConfigData.forEach(item => {
        this.getWfObjConfigItem(item.modelId)
      })*/

      /*this.$http({
        url: this.current_prefix + "/wf_obj_config_item/list",
        data: {
          objTypeCode: objTypeCode,
          enabled: true
        },
        method: "post",
        isLoading: true,
        success: (resultMsg) => {
          this.wfDefItems = resultMsg.objx || [];
          this.dialogVisible = true;
          this.tableData = this.$baseLodash.cloneDeep(wfConfigData)
        },
      });*/
    },
    getWfObjConfigItem(modelId) {
      let reportTemplate = this.getReportTemplate();
      let objTypeCode = reportTemplate.objTypeCode;
      this.$http({
        url: this.current_prefix + "/wf/getModelNodes",
        data: {
          stringOne: modelId,
        },
        method: "post",
        async: false,
        isLoading: true,
        success: (res) => {
          let rows = res.objx || [];
          // this.wfNodeMap[modelId] = rows
          this.$set(this.wfNodeMap, modelId, rows);
        },
      });
    },
    addItem() {
      let newItem = {
        type: 1,
        serveType: 3,
        serveName: null,
        modelKey: null,
        modelOrders: undefined,
        taskSteps: null,
        companyCodes: null,
      };
      this.tableData.push(newItem);
    },
    colSubmit() {
      let searveNameList = ["prod", "uat", "dev"];
      let tableData = this.tableData;
      let map = {};
      for (let i = 0; i < tableData.length; i++) {
        let item = tableData[i];
        if (!item.serveName) {
          this.$message({
            type: "error",
            message: "服务名称不能为空",
          });
          return;
        }
        if (!item.companyCodes) {
          this.$message({
            type: "error",
            message: "组织编码不能为空",
          });
          return;
        }
        let modelOrders = item.modelOrders ?? "";

        let serveName = item.serveName;
        let companyCodes = item.companyCodes;
        let key = serveName + "_" + companyCodes + "_" + modelOrders;
        if (!map[key]) {
          map[key] = true;
        } else {
          this.$message({
            type: "error",
            message: "[服务+组织+流程模板序号]不能重复",
          });
          return;
        }
      }

      this.dialogVisible = !1;
      this.optionModel.wfConfigData = this.tableData;
    },
    changeModelId(val) {
      if (val) {
        this.getWfObjConfigItem(val);
      }
    },
    changeEnabledByWf(val) {
      if (val) {
        this.optionModel.hiddenByWf = false;
        // this.optionModel.wfEdit = false;
      }
    },
    changeHiddenByWf(val) {
      if (val) {
        this.optionModel.enabledByWf = false;
      }
    },
    changeWfEdit(val) {
      if (val) {
        this.optionModel.enabledByWf = false;
      }
    },
    changeServeType(row) {
      // modelKey
      let map = {
        1: "prod",
        2: "uat",
        3: "dev",
      };
      let serveName = null;
      let serveType = row.serveType;
      if (serveType && map[serveType]) {
        serveName = map[row.serveType];
      }
      if (serveType === 5) {
        row.companyCodes = null;
      }
      row.serveName = serveName;
    },
    openDialog2() {
      this.tableData2 = this.$baseLodash.cloneDeep(
        this.optionModel.wfModifyDataConfig || []
      );
      this.dialogVisible2 = true;
    },
    addItem2() {
      let newItem = {
        action: "start",
        // valueType: 1,
        toValue: null,
      };
      this.tableData2.push(newItem);
    },
    colSubmit2() {
      let isError = false;
      let map = {};
      this.tableData2.forEach((item) => {
        let count = map[item.action] || 0;
        map[item.action] = count + 1;
        if (map[item.action] > 1) {
          isError = true;
        }
      });
      if (isError) {
        this.$message({
          message: "流程动作不能重复",
          type: "error",
          duration: 2000,
        });
        return;
      }
      this.dialogVisible2 = !1;
      this.$set(this.optionModel, "wfModifyDataConfig", this.tableData2);
    },
    changeValueType(row) {
      row.toValue = null;
    },
    clearBdCompanyEnv(row) {
      row.serveName = null;
      row.companyCodes = null;
    },
    openBdCompanyEnvDialog(rowIndex) {
      this.operateIndex = rowIndex;
      this.showBdCompanyEnvDialog = true;
    },
    confirmBdCompanyEnvDialog(rows) {
      if (rows.length) {
        let row = rows[0];
        let item = this.tableData[this.operateIndex];
        item.serveName = row.bdService;
        item.companyCodes = row.bdCompanyCode;
      }
    },
  },
};
</script>

<style scoped></style>
