<!-- 用户编辑弹窗 -->
<template>
  <el-dialog
    class="ele-dialog-form"
    :title="title"
    :visible.sync="visible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="1200px"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item
            label="厂房编码:"
            prop="code"
            style="margin-bottom: 12px"
          >
            <el-input
              clearable
              :maxlength="20"
              v-model="form.code"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="厂房名称:"
            prop="name"
            style="margin-bottom: 12px"
          >
            <el-input
              clearable
              :maxlength="20"
              v-model="form.name"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item label="状态:" prop="enabled">
            <el-select
              v-model="form.enabled"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in options.enabled"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item label="所属工厂:" prop="parentId">
            <el-select
              v-model="form.parentId"
              placeholder="请选择"
              @change="change_factoryId"
              style="width: 100%"
            >
              <el-option
                v-for="item in options_factory"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item
            label="负责人部门:"
            prop="extInfo.principalDep"
            style="margin-bottom: 12px"
          >
            <ele-tree-select
              clearable
              :data="options.principalDep"
              v-model="form.extInfo.principalDep"
              valueKey="id"
              labelKey="name"
              placeholder="请选择"
              @change="change_principalDep"
              default-expand-all
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item label="负责人:" prop="leaderId">
            <el-select
              v-model="form.leaderId"
              placeholder="请选择"
              @change="change_principal"
              style="width: 100%"
            >
              <el-option
                v-for="item in options.principal"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item label="联系方式:" prop="extInfo.phone">
            <el-input
              clearable
              :maxlength="20"
              v-model="form.extInfo.phone"
              οninput="value=value.replace(/[^\d]/g,'');if(value.length > 11)value = value.slice(0, 11)"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.assetCode" label="房屋资产编码">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.assetCode"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.campus" label="归属园院区">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.campus"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.coveredArea" label="建筑面积">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.coveredArea"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.level" label="楼层">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.level"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.moneyCeiling" label="已配金额上限">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.moneyCeiling"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="margin-bottom: 12px">
          <el-form-item prop="extInfo.useArea" label="使用面积">
            <el-input
              class="form-ipt"
              size="small"
              style="width: 100%"
              placeholder="请输入"
              v-model="form.extInfo.useArea"
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-bottom: 12px">
          <el-form-item label="地址:" prop="extInfo.location">
            <div class="location-warp">
              <el-cascader
                clearable
                style="width: 400px"
                v-model="form.extInfo.location"
                :options="options.cityDataLabel"
              ></el-cascader>
              <el-input
                class="detail"
                clearable
                :maxlength="20"
                v-model="form.extInfo.locationDetail"
                placeholder="请输入详细地址"
              />
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="24" style="margin-bottom: 12px">
          <el-form-item label="备注:" prop="remark">
            <el-input
              clearable
              :maxlength="100"
              type="textarea"
              v-model="form.remark"
              placeholder="请输入"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template v-slot:footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
import { saveOrUpdate, listWorkshopByParentId } from '@/api/factoryModel';
import { getUserPage } from '@/api/system/organization';
import { cityDataLabel } from 'ele-admin/packages/utils/regions';
export default {
  props: {
    options_groupId: {
      type: Array,
      default() {
        return [];
      }
    },
    options_factory: {
      type: Array,
      default() {
        return [];
      }
    }
  },
  watch: {
    options_groupId(nval) {
      this.toTreeData(nval);
    }
  },
  data() {
    const defaultForm = {
      id: '',
      code: '',
      name: '',
      leaderId:'',
      extInfo: {
        principalDep: '', // 负责人部门
        phone: '', // 手机号
        locationDetail: '', // 详细地址
        location: [], // 省市区
        assetCode: '', //房屋资产编码
        campus: '', //归属园院区
        coveredArea: '', //建筑面积
        level: '', //楼层
        moneyCeiling: '', //已配金额上限
        useArea: '' // 使用面积
      },
      groupId: '',
      enabled: 1,
      parentId: '',
      remark: '',
      type: 2 //  FACTORY(1, "工厂"), WORKSHOP_PLAN(2, "厂房"), WORKSHOP(3, "车间"), LINE(4, "厂线");
    };
    return {
      defaultForm,
      // 表单数据
      form: { ...defaultForm },
      // 表单验证规则
      rules: {
        code: [{ required: true, message: '请输入', trigger: 'blur' }],
        name: [{ required: true, message: '请输入', trigger: 'blur' }],
        'extInfo.factoryId': [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        parentId: { required: true, message: '请输入', trigger: 'change' },
        enabled: { required: true, message: '请输入', trigger: 'change' },
        'extInfo.principalDep': [
          { required: true, message: '请输入', trigger: 'change' }
        ],
        leaderId: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        'extInfo.phone': [
          { min: 11, message: '手机号格式不正确', trigger: 'blur' }
        ],


        // 'extInfo.assetCode': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],

        // 'extInfo.campus': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],
        // 'extInfo.coveredArea': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],
        // 'extInfo.level': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],

        // 'extInfo.moneyCeiling': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ],
        // 'extInfo.useArea': [
        //   { required: true, message: '请输入', trigger: 'blur' }
        // ]
      },
      visible: false,
      type: '', // add/edit
      loading: false,
      options: {
        principalDep: [],
        principal: [],
        workshop: [],
        enabled: [
          {
            label: '生效',
            value: 1
          },
          {
            label: '未生效',
            value: 0
          }
        ],
        cityDataLabel
      }
    };
  },
  computed: {
    title() {
      switch (this.type) {
        case 'add':
          return '新增厂房';
          break;
        case 'edit':
          return '编辑厂房';
          break;
        default:
          break;
      }
    }
  },
  methods: {
    open(type, row) {
      this.type = type;
      this.visible = true;
      if (type == 'edit') {
        for (const key of Object.keys(this.form)) {
          if (key !== 'extInfo') {
            this.form[key] = row[key];
          } else {
            for (const el of Object.keys(this.form.extInfo)) {
              this.form.extInfo[el] = row.extInfo[el];
            }
          }
        }
        // 请求下拉数据
        if (this.form.extInfo.factoryId) {
          this.getListWorkshopByParentId();
        }
        if (this.form.extInfo.principalDep) {
          this.getUserPage();
        }
      }else{
        this.form = { ...this.defaultForm };
        const info = {
            principalDep: '', // 负责人部门
            phone: '', // 手机号
            locationDetail: '', // 详细地址
            location: [], // 省市区
            assetCode: '', //房屋资产编码
            campus: '', //归属园院区
            coveredArea: '', //建筑面积
            level: '', //楼层
            moneyCeiling: '', //已配金额上限
            useArea: '' // 使用面积
        }
         this.$set(this.form,'extInfo',info)
         this.$refs.form.resetFields();
      }
    },
    /* 保存编辑 */
    save() {
      this.$refs.form.validate((valid) => {
        if (!valid) {
          return false;
        }
        this.loading = true;

        if (this.type == 'add') {
          delete this.form.id;
        }
        saveOrUpdate(this.form)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.handleClose();
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      });
    },
    restForm() {
      this.$refs.form.clearValidate();
      this.form = { ...this.defaultForm };
    },
    handleClose() {
      this.restForm();
      this.visible = false;
    },
    // 格式化公司数据
    toTreeData(val) {
      this.options.principalDep = this.$util.toTreeData({
        data: val,
        idField: 'id',
        parentIdField: 'parentId'
      });
    },
    // 获取人员
    getUserPage() {
      let par = {
        groupId: this.form.extInfo.principalDep,
        size: 999
      };
      getUserPage(par).then((res) => {
        this.options.principal = res.list;
      });
    },
    // 选择工厂
    change_factoryId() {
      let result = this.options_factory.find((n) => n.id == this.form.parentId);
      this.form.groupId = result.groupId;
    },
    // 选择负责人部门
    change_principalDep() {
      this.form.leaderId = '';
      this.options.principal = [];
      this.getUserPage();
    },
    // 选择负责人
    change_principal() {
      let result = this.options.principal.find(
        (n) => n.id == this.form.leaderId
      );
      this.form.extInfo.phone = result.phone;
    }
  }
};
</script>
<style lang="scss" scoped>
.location-warp {
  display: flex;
  .detail {
    margin-left: 10px;
  }
}
</style>
