<template>
    <div>
        <el-form
            :model="inputQuery"
            :rules="rules"
            ref="inputQuery"
            label-width="100px"
            style="margin-top: -50px"
        >
            <el-card class="el-card">
                <div style="display: flex">
                    <el-button
                        type="primary"
                        size="small"
                        icon="search"
                        style="margin: 0 100px 20px auto; width: 100px"
                        @click="onModifySubmit('inputQuery')"
                        :loading="loading"
                    >保存
                    </el-button
                    >
                </div>
                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="企业名称:" label-width="150px" prop="companyName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.companyName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="统一社会信用代码:" label-width="150px" prop="creditCode">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.creditCode"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="企业类型:" label-width="150px" prop="companyType">
                            <el-select
                                size="small"
                                v-model="inputQuery.companyType"
                                placeholder="企业类型"
                                style="width: 100%;"
                            >
                                <el-option label="注册人/备案人" value="1"></el-option>
                                <el-option label="代理人" value="2"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="登记状态:" label-width="150px" prop="registerStatus">
                            <el-select
                                size="small"
                                v-model="inputQuery.registerStatus"
                                placeholder="登记状态"
                                style="width: 100%;"
                            >
                                <el-option label="续存（在营，开业，在册）" value="1"></el-option>
                                <el-option label="吊销" value="2"></el-option>
                                <el-option label="注销" value="3"></el-option>
                                <el-option label="迁出" value="4"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="所在地区:" label-width="150px" prop="placeArea">
                            <el-cascader
                                size="small"
                                :options="options"
                                v-model="selectedOptions"
                                placeholder="请选择所在地区"
                                style="width: 100%"
                                @change="handleChange"
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产地区:" label-width="150px" prop="productionArea">
                            <el-cascader
                                size="small"
                                :options="options"
                                v-model="selectedOptions2"
                                placeholder="请选择生产地区"
                                style="width: 100%"
                                @change="handleChange2"
                            >
                            </el-cascader>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="所在地址:" label-width="150px" prop="placeAddress">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.placeAddress"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产地址:" label-width="150px" prop="productionAddress">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionAddress"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="法人姓名:" label-width="150px" prop="legalPersonName">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.legalPersonName"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="法人证件类型:" label-width="150px" prop="legalPersonPapersType">
                            <el-select
                                size="small"
                                v-model="inputQuery.legalPersonPapersType"
                                placeholder="法人证件类型"
                                style="width: 100%;"
                            >
                                <el-option label="身份证" value="1"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="法人证件号码:" label-width="150px" prop="legalPersonPapersCode">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.legalPersonPapersCode"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产许可证号:" label-width="150px" prop="productionLicenceNum">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionLicenceNum"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产许可证有效期至:" label-width="150px" prop="productionLicenceDate">
<!--                            <el-input-->
<!--                                size="small"-->
<!--                                placeholder="请输入内容"-->
<!--                                v-model="inputQuery.productionLicenceDate"-->
<!--                                @input="change()"-->
<!--                            ></el-input>-->
                            <el-date-picker
                                style="width: 100%"
                                v-model="inputQuery.productionLicenceDate"
                                type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="10" class="el-col">
                        <el-form-item label="生产备案凭证号:" label-width="150px" prop="productionRecordNum">
                            <el-input
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionRecordNum"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="10" class="el-col">
                        <el-form-item label="经营备案发证日期:" label-width="150px" prop="productionRecordDate">
<!--                            <el-input-->
<!--                                size="small"-->
<!--                                placeholder="请输入内容"-->
<!--                                v-model="inputQuery.productionRecordDate"-->
<!--                                @input="change()"-->
<!--                            ></el-input>-->
                            <el-date-picker
                                style="width: 100%"
                                v-model="inputQuery.productionRecordDate"
                                type="date"
                                placeholder="选择日期">
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="20" class="el-col">
                        <el-form-item label="凭证号备案部门:" label-width="150px" prop="productionRecordSection">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.productionRecordSection"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>

                <el-row :gutter="20" class="el-row" type="flex">
                    <el-col :span="20" class="el-col">
                        <el-form-item label="备注:" label-width="150px" prop="remark">
                            <el-input
                                type="textarea"
                                size="small"
                                placeholder="请输入内容"
                                v-model="inputQuery.remark"
                                @input="change()"
                            ></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-card>

            <el-card class="el-card">
                <div style="display: flex">
                    <el-button
                        type="primary"
                        size="small"
                        icon="search"
                        style="margin: 0 100px 20px auto; width: 100px"
                        @click="addCert()"
                    >添加电子档
                    </el-button
                    >
                </div>
                <el-table v-loading="certLoading"
                          :data="certList"
                          style="width: 100%">
                    <el-table-column type="index" label="序号" width="50"></el-table-column>
                    <el-table-column label="电子档名称" prop="name"></el-table-column>
                    <el-table-column label="备注" prop="remark"></el-table-column>
                    <el-table-column label="时间" prop="create_time" show-overflow-tooltip>
                        <template slot-scope="scope">
                            <i class="el-icon-time"></i>
                            <span>{{ scope.row.create_time }}</span>
                        </template>
                    </el-table-column>

                    <el-table-column label="操作" fixed="right" width="250">
                        <template slot-scope="scope">
                            <el-button
                                type="text"
                                size="small"
                                @click.native="toViewCompanyCert(scope.row)"
                            >查看文件
                            </el-button
                            >
                            <el-button
                                type="text"
                                size="small"
                                @click.native="deleteCompanyCert(scope.row)"
                            >删除
                            </el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination
                    :page-size="certQuery.limit"
                    @current-change="certHandleCurrentChange"
                    layout="prev, pager, next"
                    :total="certTotal"
                >
                </el-pagination>
            </el-card>

            <el-dialog
                title="添加电子档"
                :visible.sync="addCertVisible"
                :close-on-click-modal="false"
                :close-on-press-escape="false"
                width="70%"
                v-if="addCertVisible"
                @close='closeDialog'
                :append-to-body='true'
            >
                <suppliersEnterpriseBasicAddCert
                    :closeDialog="closeDialog"
                    :idQuery="idQuery"
                    :enterpriseId="enterpriseId"
                ></suppliersEnterpriseBasicAddCert>
            </el-dialog>

        </el-form>
    </div>
</template>

<script>
import {getCompanyInfo, addOrModifyCompany} from "../../api/basic/suppliersEnterpriseBasic";
import store from "../../store/index";
import {
    provinceAndCityData,
    regionData,
    provinceAndCityDataPlus,
    regionDataPlus,
    CodeToText,
    TextToCode,
} from "element-china-area-data";
import {BASE_URL} from "../../config/app";
import {filterCompanyCert, deleteCompanyCert} from "../../api/warehouse/suppliersEnterpriseBasicCert";
import suppliersEnterpriseBasicAddCert from "./suppliersEnterpriseBasicAddCert";
import draggable from "vuedraggable";
import {deleteCodesTempById} from "../../api/warehouse/order";

export default {
    name: "suppliersEnterpriseBasicAdd",
    props: {
        addCloseDialog: {
            type: Function,
            required: true,
        },
        enterpriseId: "",
    },
    data() {
        return {
            inputQuery: {
                companyName: "",
                creditCode: "",
                companyType: "",
                registerStatus: "",
                placeArea: "",
                placeAddress: "",
                legalPersonName: "",
                legalPersonPapersType: "",
                legalPersonPapersCode: "",
            },
            loading: false,
            classesDisplay: false,
            selectedOptions: [],
            selectedOptions2: [],
            options: regionDataPlus,
            rules: {
                companyName: [
                    {
                        required: true,
                        message: "请输入企业名称",
                        trigger: "blur"
                    }
                ],
                creditCode: [
                    {
                        required: true,
                        message: "请输入统一社会信用代码",
                        trigger: "blur"
                    }
                ],
                companyType: [
                    {
                        required: true,
                        message: "请选择企业类型",
                        trigger: "change"
                    }
                ],
                registerStatus: [
                    {
                        required: true,
                        message: "请选择登记状态",
                        trigger: "change"
                    }
                ],
                placeArea: [
                    {
                        required: true,
                        message: "请选择所在地区",
                        trigger: "change"
                    }
                ],
                placeAddress: [
                    {
                        required: true,
                        message: "请输入所在地址",
                        trigger: "change"
                    }
                ],
                legalPersonName: [
                    {
                        required: true,
                        message: "请输入法人姓名",
                        trigger: "change"
                    }
                ],
                legalPersonPapersType: [
                    {
                        required: true,
                        message: "请选择法人证件类型",
                        trigger: "change"
                    }
                ],
                legalPersonPapersCode: [
                    {
                        required: true,
                        message: "请输入法人证件号码",
                        trigger: "change"
                    }
                ],
            },
            uploadUrl: "",
            fileList: [],
            fileUrl: "",
            certFileUrl: "",
            url: 'http://127.0.0.1:9996/udiwms/image/register/file/getImage?type=image1&name=t11111.jpg',
            certQuery: {
                page: 1,
                limit: 20
            },
            certList: [],
            certTotal: 0,
            certLoading: false,
            addCertVisible: false,
            idQuery: {
                id: "",
            },
        };
    },
    created() {
        if (this.$isNotBlank(this.enterpriseId)) {
            this.getData();
            this.getCompanyCertList();
        }
        this.uploadUrl = BASE_URL + "/udiwms/upload/register/file";
        this.fileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image1&name=";
        this.certFileUrl = BASE_URL + "/udiwms/image/register/file/getImage?type=image3&name=";
    },
    components: {
        draggable,
        suppliersEnterpriseBasicAddCert,
    },
    methods: {
        onModifySubmit(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    this.loading = true;
                    let urlName = this.$isBlank(this.enterpriseId) ? 'add' : 'update';
                    if (this.$isBlank(this.inputQuery.productionLicenceNum)) {
                        this.inputQuery.productionLicenceDate= null;
                    }
                    if (this.$isBlank(this.inputQuery.productionRecordNum)) {
                        this.inputQuery.productionRecordDate= null;
                    }
                    addOrModifyCompany(this.inputQuery, urlName).then(response => {
                        this.loading = false;
                        if (response.code === 20000) {
                            this.addCloseDialog(true);
                            this.$message.success("保存成功");
                        } else {
                            this.$message.error(response.message);
                        }
                    });
                } else {
                    console.log("error submit!!");
                    return false;
                }
            });
        },
        getData() {
            this.loading = true;
            let tQuery = {
                id: this.enterpriseId,
            };
            getCompanyInfo(tQuery).then((response) => {
                console.log(response)
                this.loading = false;
                this.inputQuery = response.data;
                this.selectedOptions = this.inputQuery.placeAreaCode.split(",");
                this.selectedOptions2 = this.inputQuery.productionAreaCode.split(",");
            });
        },
        openFile() {
            window.open(this.fileUrl + this.inputQuery.filePath);
        },
        certHandleCurrentChange(val) {
            this.certQuery.page = val;
            this.getCompanyCertList();
        },
        getCompanyCertList() {
            this.certLoading = true;
            this.certQuery.enterpriseId = this.enterpriseId;
            filterCompanyCert(this.certQuery)
                .then((response) => {
                    this.certLoading = false;
                    this.certList = response.data.list || [];
                })
                .catch(() => {
                    this.certLoading = false;
                    this.certList = [];
                });
        },
        addCert(index, row) {
            this.idQuery.id = '';
            if (this.$isNotBlank(row) && this.$isNotBlank(row.id)) {
                this.idQuery.id = row.id;
                this.idQuery.formData = row;
            }
            this.addCertVisible = true;
        },
        toViewCompanyCert(row) {
            window.open(this.certFileUrl + row.filePath);
        },
        deleteCompanyCert(row) {
            this.$confirm("是否删除?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            }).then(() => {
                this.certLoading = true;
                let tquery = {
                    id: row.id,
                    filePath: row.filePath,
                };
                deleteCompanyCert(tquery)
                    .then((response) => {
                        this.certLoading = false;
                        if (response.code === 20000) {
                            this.getCompanyCertList();
                            this.$message({
                                type: "success",
                                message: "删除成功！",
                            });
                        } else {
                            this.$message.error(response.message);
                        }
                    })
                    .catch(() => {
                        this.certLoading = false;
                    });
            }).catch(() => {
            });
        },
        closeDialog(type) {
            this.addCertVisible = false;
            this.getCompanyCertList();
        },
        handleChange(value) {
            this.inputQuery.placeArea =
                CodeToText[value[0]] + CodeToText[value[1]] + CodeToText[value[2]];
            console.log(this.inputQuery.placeArea)
            this.inputQuery.placeAreaCode = value.toString();
        },
        handleChange2(value) {
            this.inputQuery.productionArea =
                CodeToText[value[0]] + CodeToText[value[1]] + CodeToText[value[2]];
            this.inputQuery.productionAreaCode = value.toString();
        },
        uploadHandleRemove(file, fileList) {
            console.log(file, fileList);
        },
        uploadHandlePreview(file) {
            console.log(file);
            console.log(this.fileList);
        },
        uploadHandleExceed(files, fileList) {
            this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
        },
        uploadOnchange(file, fileList) {
            let fileName = file.name;
            let uid = file.uid
            let pos = fileName.lastIndexOf(".");
            let lastName = fileName.substring(pos, fileName.length);
            if (lastName.toLowerCase() !== ".jpg" && lastName.toLowerCase() !== ".png") {
                this.$message.error("上传文件只能是 .jpg,.png 格式");
                for (let i = 0; i < fileList.length; i++) {//从list删除
                    if (fileList[i].uid === uid) {
                        fileList.splice(i, 1)
                    }
                }
                return;
            }
            // 限制上传文件的大小
            const isLt = file.size / 1024 / 1024 / 2 <= 1;
            if (!isLt) {
                this.$message.error("上传文件大小不能超过 2MB");
                for (let i = 0; i < fileList.length; i++) {
                    if (fileList[i].uid === uid) {
                        fileList.splice(i, 1)
                    }
                }
            }
            return isLt;
        },
        uploadHandleSuccess(response, file, fileList) {
            console.log(response)
            if (response.code === 20000) {
                this.inputQuery.newFilePath = response.data.name;
                this.onModifySubmit('inputQuery');
            } else {
                this.$message.error("文件上传失败：" + response.message);
            }
        },
        uploadHandleError() {

        },
    }
};
</script>
<style>
.itemTag {
    float: left;
    text-align: left;
    margin-top: 10px;
    width: 120px;
}

.text {
    width: 100%;
    font-size: 13px;
    font-family: "Microsoft YaHei";
}

.el-card {
    margin-right: 20px;
    margin-top: 15px;
    /*transition: all .5s;*/
}

.el-row {
    display: flex;
    flex-wrap: wrap;
}

.el-col {
    border-radius: 4px;
    flex-wrap: wrap;
}
</style>
